Skip to content

Commit

Permalink
Fix #356 add support for serilog extensions logging bridge and eventid (
Browse files Browse the repository at this point in the history
  • Loading branch information
Mpdreamz authored May 29, 2024
1 parent 6ce0252 commit ebdfbb4
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/Elastic.CommonSchema.Serilog/LogEventConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ private static bool PropertyAlreadyMapped(string property)
case SpecialKeys.ActionId:
case SpecialKeys.ActionKind:
case SpecialKeys.ActionSeverity:
case SpecialKeys.EventId:
case SpecialKeys.ApplicationId:
case SpecialKeys.ApplicationName:
case SpecialKeys.ApplicationType:
Expand Down Expand Up @@ -272,6 +273,19 @@ private static Event GetEvent(LogEvent e)
Duration = elapsedMs != null ? (long)(elapsedMs * 1000000) : null
};

if (e.Properties.TryGetValue(SpecialKeys.EventId, out var eventData) && eventData is StructureValue dv)
{
var idProp = dv.Properties.FirstOrDefault(p => p.Name == "Id");
var eventId = idProp?.Value is ScalarValue i ? i.Value as int? : null;
if (eventId != null)
evnt.Code = eventId.ToString();

var nameProp = dv.Properties.FirstOrDefault(p => p.Name == "Name");
var eventAction = nameProp?.Value is ScalarValue n ? n.Value as string : null;
if (eventAction != null)
evnt.Action = eventAction;
}

return evnt;
}

Expand Down
1 change: 1 addition & 0 deletions src/Elastic.CommonSchema.Serilog/SpecialKeys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ internal static class SpecialKeys
public const string ActionId = nameof(ActionId);
public const string ActionKind = nameof(ActionKind);
public const string ActionSeverity = nameof(ActionSeverity);
public const string EventId = nameof(EventId);
public const string ApplicationId = nameof(ApplicationId);
public const string ApplicationName = nameof(ApplicationName);
public const string ApplicationType = nameof(ApplicationType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ namespace Elastic.CommonSchema.Serilog.Tests
{
public class EcsFieldsInTemplateTests : LogTestsBase
{
public EcsFieldsInTemplateTests(ITestOutputHelper output) : base(output) =>
LoggerConfiguration = LoggerConfiguration;
public EcsFieldsInTemplateTests(ITestOutputHelper output) : base(output) { }

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


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<PackageReference Include="Serilog.Enrichers.Environment" Version="2.1.3" />
<PackageReference Include="Serilog.Enrichers.Process" Version="2.0.1" />
<PackageReference Include="Serilog.Enrichers.Thread" Version="3.2.0-dev-00747" />
<PackageReference Include="Serilog.Extensions.Logging" Version="3.1.0" />
<PackageReference Include="Serilog.Sinks.ColoredConsole" Version="3.0.1" />
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.TestCorrelator" Version="3.2.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

using System.Linq;
using FluentAssertions;
using Microsoft.Extensions.Logging;
using Serilog;
using Xunit;
using Xunit.Abstractions;

namespace Elastic.CommonSchema.Serilog.Tests.ExtensionsLogging
{
public class SerilogWithExtensionsLoggerAdapter : LogTestsBase
{
public SerilogWithExtensionsLoggerAdapter(ITestOutputHelper output) : base(output) { }

[Fact]
public void EcsFieldsDoNotEndUpAsLabelsOrMetadata() => TestLogger((serilogLogger, getLogEvents) =>
{
ILoggerFactory loggerFactory = new LoggerFactory();
loggerFactory.AddSerilog(serilogLogger);
var logger = loggerFactory.CreateLogger<SerilogWithExtensionsLoggerAdapter>();
logger.LogInformation("Info {TraceId} {FaasColdstart}", "trace-123", true);

var logEvents = getLogEvents();
logEvents.Should().HaveCount(1);

var ecsEvents = ToEcsEvents(logEvents);

var (_, info) = ecsEvents.First();
info.TraceId.Should().Be("trace-123");
info.Faas.Coldstart.Should().BeTrue();
});

[Fact]
public void SupportsEventId() => TestLogger((serilogLogger, getLogEvents) =>
{
ILoggerFactory loggerFactory = new LoggerFactory();
loggerFactory.AddSerilog(serilogLogger);
var logger = loggerFactory.CreateLogger<SerilogWithExtensionsLoggerAdapter>();
logger.LogError(new EventId(123, "hello"), "Hello {World}", "Universe");

var logEvents = getLogEvents();
logEvents.Should().HaveCount(1);

var ecsEvents = ToEcsEvents(logEvents);

var (_, error) = ecsEvents.First();
error.Event.Should().NotBeNull();
error.Event.Action.Should().Be("hello");
error.Event.Code.Should().Be("123");
error.Metadata.Should().BeNull();
});
[Fact]
public void SupportsStructureCapturing() => TestLogger((serilogLogger, getLogEvents) =>
{
ILoggerFactory loggerFactory = new LoggerFactory();
loggerFactory.AddSerilog(serilogLogger);
var logger = loggerFactory.CreateLogger<SerilogWithExtensionsLoggerAdapter>();
logger.LogInformation("Info {TraceId} {@FaasColdstart}", new { x = 1 }, new { y = 2 });

var logEvents = getLogEvents();
logEvents.Should().HaveCount(1);

var ecsEvents = ToEcsEvents(logEvents);

var (_, info) = ecsEvents.First();
info.TraceId.Should().NotBeNull();
info.TraceId.Should().Be("{ x = 1 }");
info.Faas.Should().BeNull();
info.Metadata.Should().ContainKey("FaasColdstart");
var structured = info.Metadata["FaasColdstart"] as MetadataDictionary;
structured.Should().NotBeNull();
structured!["y"].Should().Be(2);
});

}
}

0 comments on commit ebdfbb4

Please sign in to comment.