Skip to content

Commit 17d8083

Browse files
committed
Add test to check support for custom destructuring extension Methods
1 parent 930f014 commit 17d8083

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

test/Serilog.Settings.Configuration.Tests/ConfigurationSettingsTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,5 +679,29 @@ private string GetDestructuredProperty(object x, string json)
679679
var result = evt.Properties["X"].ToString();
680680
return result;
681681
}
682+
683+
[Fact]
684+
public void DestructuringWithCustomExtensionMethodIsApplied()
685+
{
686+
var json = @"{
687+
""Serilog"": {
688+
""Using"": [""TestDummies""],
689+
""Destructure"": [
690+
{
691+
""Name"": ""WithDummyHardCodedString"",
692+
""Args"": { ""hardCodedString"": ""hardcoded"" }
693+
}]
694+
}
695+
}";
696+
697+
LogEvent evt = null;
698+
var log = ConfigFromJson(json)
699+
.WriteTo.Sink(new DelegatingSink(e => evt = e))
700+
.CreateLogger();
701+
log.Information("Destructuring with hard-coded policy {@Input}", new { Foo = "Bar" });
702+
var formattedProperty = evt.Properties["Input"].ToString();
703+
704+
Assert.Equal("\"hardcoded\"", formattedProperty);
705+
}
682706
}
683707
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using Serilog.Core;
3+
using Serilog.Events;
4+
5+
namespace TestDummies
6+
{
7+
public class DummyHardCodedStringDestructuringPolicy : IDestructuringPolicy
8+
{
9+
readonly string _hardCodedString;
10+
11+
public DummyHardCodedStringDestructuringPolicy(string hardCodedString)
12+
{
13+
_hardCodedString = hardCodedString ?? throw new ArgumentNullException(nameof(hardCodedString));
14+
}
15+
16+
public bool TryDestructure(object value, ILogEventPropertyValueFactory propertyValueFactory, out LogEventPropertyValue result)
17+
{
18+
result = new ScalarValue(_hardCodedString);
19+
return true;
20+
}
21+
}
22+
}

test/TestDummies/DummyLoggerConfigurationExtensions.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ public static LoggerConfiguration Dummy(
109109
s => new DummyWrappingSink(s),
110110
wrappedSinkAction);
111111
}
112-
112+
113+
public static LoggerConfiguration WithDummyHardCodedString(
114+
this LoggerDestructuringConfiguration loggerDestructuringConfiguration,
115+
string hardCodedString
116+
)
117+
{
118+
return loggerDestructuringConfiguration.With(new DummyHardCodedStringDestructuringPolicy(hardCodedString));
119+
}
120+
113121
}
114122
}

0 commit comments

Comments
 (0)