-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #356 add support for serilog extensions logging bridge and eventid (
#393)
- Loading branch information
Showing
5 changed files
with
97 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...lastic.CommonSchema.Serilog.Tests/ExtensionsLogging/SerilogWithExtensionsLoggerAdapter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
|
||
} | ||
} |