Skip to content

Commit c973382

Browse files
committed
Attach EventId as a structure when present. Fixes #33.
1 parent 9fca46b commit c973382

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Collections.Generic;
2+
using Microsoft.Extensions.Logging;
3+
using Serilog.Core;
4+
using Serilog.Events;
5+
6+
namespace Serilog.Extensions.Logging
7+
{
8+
/// <summary>
9+
/// Attaches <see cref="EventId"/> properties to events that have them. Uses a more compact format than
10+
/// the default destructuring provides (no type tag, default properties omitted).
11+
/// </summary>
12+
class EventIdEnricher : ILogEventEnricher
13+
{
14+
readonly EventId _eventId;
15+
16+
public EventIdEnricher(EventId eventId)
17+
{
18+
_eventId = eventId;
19+
}
20+
21+
public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
22+
{
23+
var properties = new List<LogEventProperty>(2);
24+
25+
if (_eventId.Id != 0)
26+
{
27+
properties.Add(new LogEventProperty("Id", new ScalarValue(_eventId.Id)));
28+
}
29+
30+
if (_eventId.Name != null)
31+
{
32+
properties.Add(new LogEventProperty("Name", new ScalarValue(_eventId.Name)));
33+
}
34+
35+
logEvent.AddOrUpdateProperty(new LogEventProperty("EventId", new StructureValue(properties)));
36+
}
37+
}
38+
}

src/Serilog.Extensions.Logging/Extensions/Logging/SerilogLogger.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Except
9999
if (string.IsNullOrEmpty(messageTemplate))
100100
return;
101101

102-
if (eventId.Id != 0)
103-
logger = logger.ForContext("EventId", eventId);
102+
if (eventId.Id != 0 || eventId.Name != null)
103+
logger = logger.ForContext(new[] { new EventIdEnricher(eventId) });
104104

105105
var parsedTemplate = _messageTemplateParser.Parse(messageTemplate);
106106
var evt = new LogEvent(DateTimeOffset.Now, level, exception, parsedTemplate, Enumerable.Empty<LogEventProperty>());

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Microsoft.Extensions.Logging;
88
using System.Collections.Generic;
99
using System.IO;
10+
using System.Linq;
1011
using Serilog.Debugging;
1112
using Xunit;
1213

@@ -250,6 +251,24 @@ public void CarriesMessageTemplateProperties()
250251
Assert.Empty(selfLog.ToString());
251252
}
252253

254+
[Fact]
255+
public void CarriesEventIdIfNonzero()
256+
{
257+
var t = SetUp(LogLevel.Trace);
258+
var logger = t.Item1;
259+
var sink = t.Item2;
260+
261+
int expected = 42;
262+
263+
logger.Log(LogLevel.Information, expected, "Test", null, null);
264+
265+
Assert.Equal(1, sink.Writes.Count);
266+
267+
var eventId = (StructureValue) sink.Writes[0].Properties["EventId"];
268+
var id = (ScalarValue) eventId.Properties.Single(p => p.Name == "Id").Value;
269+
Assert.Equal(42, id.Value);
270+
}
271+
253272
private class FoodScope : IEnumerable<KeyValuePair<string, object>>
254273
{
255274
readonly string _name;

0 commit comments

Comments
 (0)