Skip to content

Commit a0eb422

Browse files
committed
Added functionality to support the destructurer operator in BeginScope.
1 parent 95f35b2 commit a0eb422

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLoggerScope.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,16 @@ public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
5858
if (stateProperty.Key == SerilogLoggerProvider.OriginalFormatPropertyName && stateProperty.Value is string)
5959
continue;
6060

61-
var property = propertyFactory.CreateProperty(stateProperty.Key, stateProperty.Value);
61+
var key = stateProperty.Key;
62+
var destructureObject = false;
63+
64+
if (key.StartsWith("@"))
65+
{
66+
key = key.Substring(1);
67+
destructureObject = true;
68+
}
69+
70+
var property = propertyFactory.CreateProperty(key, stateProperty.Value, destructureObject);
6271
logEvent.AddPropertyIfAbsent(property);
6372
}
6473
}

test/Serilog.Extensions.Logging.Tests/SerilogLoggerTests.cs

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,82 @@ public void WhenDisposeIsTrueProvidedLoggerIsDisposed()
288288
Assert.True(logger.IsDisposed);
289289
}
290290

291+
[Fact]
292+
public void BeginScopeDestructuresObjectsWhenDestructurerIsUsedInMessageTemplate()
293+
{
294+
var t = SetUp(LogLevel.Trace);
295+
var logger = t.Item1;
296+
var sink = t.Item2;
297+
298+
using (logger.BeginScope("{@Person}", new Person { FirstName = "John", LastName = "Smith" }))
299+
{
300+
logger.Log(LogLevel.Information, 0, TestMessage, null, null);
301+
}
302+
303+
Assert.Equal(1, sink.Writes.Count);
304+
Assert.True(sink.Writes[0].Properties.ContainsKey("Person"));
305+
306+
var person = (StructureValue)sink.Writes[0].Properties["Person"];
307+
var firstName = (ScalarValue)person.Properties.Single(p => p.Name == "FirstName").Value;
308+
var lastName = (ScalarValue)person.Properties.Single(p => p.Name == "LastName").Value;
309+
Assert.Equal("John", firstName.Value);
310+
Assert.Equal("Smith", lastName.Value);
311+
}
312+
313+
[Fact]
314+
public void BeginScopeDestructuresObjectsWhenDestructurerIsUsedInDictionary()
315+
{
316+
var t = SetUp(LogLevel.Trace);
317+
var logger = t.Item1;
318+
var sink = t.Item2;
319+
320+
using (logger.BeginScope(new Dictionary<string, object> {{ "@Person", new Person { FirstName = "John", LastName = "Smith" }}}))
321+
{
322+
logger.Log(LogLevel.Information, 0, TestMessage, null, null);
323+
}
324+
325+
Assert.Equal(1, sink.Writes.Count);
326+
Assert.True(sink.Writes[0].Properties.ContainsKey("Person"));
327+
328+
var person = (StructureValue)sink.Writes[0].Properties["Person"];
329+
var firstName = (ScalarValue)person.Properties.Single(p => p.Name == "FirstName").Value;
330+
var lastName = (ScalarValue)person.Properties.Single(p => p.Name == "LastName").Value;
331+
Assert.Equal("John", firstName.Value);
332+
Assert.Equal("Smith", lastName.Value);
333+
}
334+
335+
[Fact]
336+
public void BeginScopeDoesNotModifyKeyWhenDestructurerIsNotUsedInMessageTemplate()
337+
{
338+
var t = SetUp(LogLevel.Trace);
339+
var logger = t.Item1;
340+
var sink = t.Item2;
341+
342+
using (logger.BeginScope("{FirstName}", "John"))
343+
{
344+
logger.Log(LogLevel.Information, 0, TestMessage, null, null);
345+
}
346+
347+
Assert.Equal(1, sink.Writes.Count);
348+
Assert.True(sink.Writes[0].Properties.ContainsKey("FirstName"));
349+
}
350+
351+
[Fact]
352+
public void BeginScopeDoesNotModifyKeyWhenDestructurerIsNotUsedInDictionary()
353+
{
354+
var t = SetUp(LogLevel.Trace);
355+
var logger = t.Item1;
356+
var sink = t.Item2;
357+
358+
using (logger.BeginScope(new Dictionary<string, object> { { "FirstName", "John"}}))
359+
{
360+
logger.Log(LogLevel.Information, 0, TestMessage, null, null);
361+
}
362+
363+
Assert.Equal(1, sink.Writes.Count);
364+
Assert.True(sink.Writes[0].Properties.ContainsKey("FirstName"));
365+
}
366+
291367
private class FoodScope : IEnumerable<KeyValuePair<string, object>>
292368
{
293369
readonly string _name;
@@ -327,5 +403,11 @@ IEnumerator IEnumerable.GetEnumerator()
327403
return GetEnumerator();
328404
}
329405
}
406+
407+
private class Person
408+
{
409+
public string FirstName { get; set; }
410+
public string LastName { get; set; }
411+
}
330412
}
331413
}

0 commit comments

Comments
 (0)