elastic / ecs-dotnet

https://www.elastic.co/guide/en/ecs-logging/dotnet/current/setup.html
Apache License 2.0
114 stars 58 forks source link

NullReferenceException #325

Closed sgtobin closed 1 year ago

sgtobin commented 1 year ago

APM Agent version

    <PackageReference Include="Elastic.Apm.SerilogEnricher" Version="8.6.0" />
    <PackageReference Include="Elastic.CommonSchema.Serilog" Version="8.6.0" />
    <PackageReference Include="Serilog" Version="3.0.1" />
    <PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
    <PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
    <PackageReference Include="Elastic.Apm.NetCoreAll" Version="1.22.0" />

Environment

Operating system and version: Windows 11

.NET Framework/Core name and version (e.g. .NET 4.6.2, NET Core 3.1.100) : 7.0.4

Application Target Framework(s) (e.g. net462, netcoreapp3.1): 7.0.4

Describe the bug

When I add .Enrich.WithElasticApmCorrelationInfo() to the LoggerConfiguration, I get the following error.

2023-07-24T22:34:37.2447202Z Caught exception while emitting to sink Serilog.Sinks.SystemConsole.ConsoleSink: System.NullReferenceException: Object reference not set to an instance of an object.
   at Elastic.CommonSchema.Serilog.LogEventConverter.GetService(LogEvent logEvent)
   at Elastic.CommonSchema.Serilog.LogEventConverter.ConvertToEcs[TEcsDocument](LogEvent logEvent, IEcsTextFormatterConfiguration`1 configuration)
   at Elastic.CommonSchema.Serilog.EcsTextFormatter`1.Format(LogEvent logEvent, TextWriter output)
   at Serilog.Sinks.SystemConsole.ConsoleSink.Emit(LogEvent logEvent)
   at Serilog.Core.Sinks.SafeAggregateSink.Emit(LogEvent logEvent)

To Reproduce

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Debug()
    .MinimumLevel.Override("Microsoft", LogEventLevel.Error)
    .MinimumLevel.Override("Elastic", LogEventLevel.Error)
    .Enrich.WithElasticApmCorrelationInfo()
    .WriteTo.Console(new EcsTextFormatter())
    .CreateLogger();

var builder = WebApplication.CreateBuilder(args);
    builder.Host.UseElasticApm();
    builder.Host.UseSerilog();

Expected behavior

A clear and concise description of what you expected to happen.

    var app = builder.Build();
    app.UseElasticApm();

{
  "AllowedHosts": "*",
  "appConfig": {
    "setting1": "setting1",
    "setting2": "setting2"
  },
  "ElasticApm": {
    "LogLevel": "Information",
    "ServerUrl": "https://host.com:8200",
    "TransactionSampleRate": 1.0,
    "ServiceName": "imc.replace.me",
    "CaptureBody": "all",
    "CloudProvider": "none",
    "CentralConfig": false,
    "TraceContinuationStrategy": "continue"
  }
}
Mpdreamz commented 1 year ago

Moved this over to elastic/ecs-dotnet

Thanks for bringing this to our attention @sgtobin!

sgtobin commented 1 year ago

I think the issue is the same as 324

Mpdreamz commented 1 year ago

I believe this was fixed in: https://github.com/elastic/ecs-dotnet/pull/306 will do a 8.6.1 release today.

Many thanks for reporting this!