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

[BUG] NullReferenceException in LogEventConverter when not setting ElasticApm:ServiceNodeName #303

Closed kaseidu closed 1 year ago

kaseidu commented 1 year ago

ECS integration/library project(s): Elastic.CommonSchema.Serilog ECS schema version: 8.6.0 ECS .NET assembly version: 8.6.0 .NET framework / OS: Windows

Description of the problem, including expected versus actual behavior:

I first encountered this in my own application but it's also present in the example project. Running the aspnetcore-with-serilog example from this repo not setting ElasticApm:ServiceNodeName will throw a NullReferenceException when starting/browsing to the application URL. It occurs in Elastic.CommonSchema.Serilog/LogEventConverter.cs at line 80.

if (e.TryGetScalarPropertyValue(SpecialKeys.ContentType, out var contentType))
{
return new Service
{
    Name = serviceName.Value.ToString(),
    Version = logEvent.TryGetScalarPropertyValue("ElasticApmServiceVersion", out var version) ? version.Value.ToString() : null,
    NodeName = logEvent.TryGetScalarPropertyValue("ElasticApmServiceNodeName", out var name) ? name.Value.ToString() : null //Line 80, name.Value is null
};
}

Steps to reproduce:

  1. Run aspnetcore-with-serilog from this repo with SelfLog.Enable(Console.WriteLine); set or use vs debugger.
  2. Start application.
  3. The exception will be thrown
Mpdreamz commented 1 year ago

Thanks for reporting this issue! will look at rectifying this this week.

jandillenkofer commented 1 year ago

I am facing the same issue in Elastic.Apm.SerilogEnricher when not providing an ElasticApmServiceNodeName. This seems to share the same code.

Mpdreamz commented 1 year ago

Opened to https://github.com/elastic/ecs-dotnet/pull/306 to address this.