Skip to content

Commit ebdfbb4

Browse files
authored
Fix #356 add support for serilog extensions logging bridge and eventid (#393)
1 parent 6ce0252 commit ebdfbb4

File tree

5 files changed

+97
-2
lines changed

5 files changed

+97
-2
lines changed

src/Elastic.CommonSchema.Serilog/LogEventConverter.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ private static bool PropertyAlreadyMapped(string property)
149149
case SpecialKeys.ActionId:
150150
case SpecialKeys.ActionKind:
151151
case SpecialKeys.ActionSeverity:
152+
case SpecialKeys.EventId:
152153
case SpecialKeys.ApplicationId:
153154
case SpecialKeys.ApplicationName:
154155
case SpecialKeys.ApplicationType:
@@ -272,6 +273,19 @@ private static Event GetEvent(LogEvent e)
272273
Duration = elapsedMs != null ? (long)(elapsedMs * 1000000) : null
273274
};
274275

276+
if (e.Properties.TryGetValue(SpecialKeys.EventId, out var eventData) && eventData is StructureValue dv)
277+
{
278+
var idProp = dv.Properties.FirstOrDefault(p => p.Name == "Id");
279+
var eventId = idProp?.Value is ScalarValue i ? i.Value as int? : null;
280+
if (eventId != null)
281+
evnt.Code = eventId.ToString();
282+
283+
var nameProp = dv.Properties.FirstOrDefault(p => p.Name == "Name");
284+
var eventAction = nameProp?.Value is ScalarValue n ? n.Value as string : null;
285+
if (eventAction != null)
286+
evnt.Action = eventAction;
287+
}
288+
275289
return evnt;
276290
}
277291

src/Elastic.CommonSchema.Serilog/SpecialKeys.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ internal static class SpecialKeys
1616
public const string ActionId = nameof(ActionId);
1717
public const string ActionKind = nameof(ActionKind);
1818
public const string ActionSeverity = nameof(ActionSeverity);
19+
public const string EventId = nameof(EventId);
1920
public const string ApplicationId = nameof(ApplicationId);
2021
public const string ApplicationName = nameof(ApplicationName);
2122
public const string ApplicationType = nameof(ApplicationType);

tests/Elastic.CommonSchema.Serilog.Tests/EcsFieldsInTemplateTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ namespace Elastic.CommonSchema.Serilog.Tests
1111
{
1212
public class EcsFieldsInTemplateTests : LogTestsBase
1313
{
14-
public EcsFieldsInTemplateTests(ITestOutputHelper output) : base(output) =>
15-
LoggerConfiguration = LoggerConfiguration;
14+
public EcsFieldsInTemplateTests(ITestOutputHelper output) : base(output) { }
1615

1716
[Fact]
1817
public void EcsFieldsDoNotEndUpAsLabelsOrMetadata() => TestLogger((logger, getLogEvents) =>
@@ -82,5 +81,6 @@ public void SupportsStructureCapturing() => TestLogger((logger, getLogEvents) =>
8281
structured!["y"].Should().Be(2);
8382
});
8483

84+
8585
}
8686
}

tests/Elastic.CommonSchema.Serilog.Tests/Elastic.CommonSchema.Serilog.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.1.3" />
1111
<PackageReference Include="Serilog.Enrichers.Process" Version="2.0.1" />
1212
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.2.0-dev-00747" />
13+
<PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
1314
<PackageReference Include="Serilog.Sinks.ColoredConsole" Version="3.0.1" />
1415
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />
1516
<PackageReference Include="Serilog.Sinks.TestCorrelator" Version="3.2.0" />
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using System.Linq;
6+
using FluentAssertions;
7+
using Microsoft.Extensions.Logging;
8+
using Serilog;
9+
using Xunit;
10+
using Xunit.Abstractions;
11+
12+
namespace Elastic.CommonSchema.Serilog.Tests.ExtensionsLogging
13+
{
14+
public class SerilogWithExtensionsLoggerAdapter : LogTestsBase
15+
{
16+
public SerilogWithExtensionsLoggerAdapter(ITestOutputHelper output) : base(output) { }
17+
18+
[Fact]
19+
public void EcsFieldsDoNotEndUpAsLabelsOrMetadata() => TestLogger((serilogLogger, getLogEvents) =>
20+
{
21+
ILoggerFactory loggerFactory = new LoggerFactory();
22+
loggerFactory.AddSerilog(serilogLogger);
23+
var logger = loggerFactory.CreateLogger<SerilogWithExtensionsLoggerAdapter>();
24+
logger.LogInformation("Info {TraceId} {FaasColdstart}", "trace-123", true);
25+
26+
var logEvents = getLogEvents();
27+
logEvents.Should().HaveCount(1);
28+
29+
var ecsEvents = ToEcsEvents(logEvents);
30+
31+
var (_, info) = ecsEvents.First();
32+
info.TraceId.Should().Be("trace-123");
33+
info.Faas.Coldstart.Should().BeTrue();
34+
});
35+
36+
[Fact]
37+
public void SupportsEventId() => TestLogger((serilogLogger, getLogEvents) =>
38+
{
39+
ILoggerFactory loggerFactory = new LoggerFactory();
40+
loggerFactory.AddSerilog(serilogLogger);
41+
var logger = loggerFactory.CreateLogger<SerilogWithExtensionsLoggerAdapter>();
42+
logger.LogError(new EventId(123, "hello"), "Hello {World}", "Universe");
43+
44+
var logEvents = getLogEvents();
45+
logEvents.Should().HaveCount(1);
46+
47+
var ecsEvents = ToEcsEvents(logEvents);
48+
49+
var (_, error) = ecsEvents.First();
50+
error.Event.Should().NotBeNull();
51+
error.Event.Action.Should().Be("hello");
52+
error.Event.Code.Should().Be("123");
53+
error.Metadata.Should().BeNull();
54+
});
55+
[Fact]
56+
public void SupportsStructureCapturing() => TestLogger((serilogLogger, getLogEvents) =>
57+
{
58+
ILoggerFactory loggerFactory = new LoggerFactory();
59+
loggerFactory.AddSerilog(serilogLogger);
60+
var logger = loggerFactory.CreateLogger<SerilogWithExtensionsLoggerAdapter>();
61+
logger.LogInformation("Info {TraceId} {@FaasColdstart}", new { x = 1 }, new { y = 2 });
62+
63+
var logEvents = getLogEvents();
64+
logEvents.Should().HaveCount(1);
65+
66+
var ecsEvents = ToEcsEvents(logEvents);
67+
68+
var (_, info) = ecsEvents.First();
69+
info.TraceId.Should().NotBeNull();
70+
info.TraceId.Should().Be("{ x = 1 }");
71+
info.Faas.Should().BeNull();
72+
info.Metadata.Should().ContainKey("FaasColdstart");
73+
var structured = info.Metadata["FaasColdstart"] as MetadataDictionary;
74+
structured.Should().NotBeNull();
75+
structured!["y"].Should().Be(2);
76+
});
77+
78+
}
79+
}

0 commit comments

Comments
 (0)