elastic / ecs-dotnet

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

Unable to Create EcsDocument in Powershell 5.1 #294

Closed buehlerfa closed 1 year ago

buehlerfa commented 1 year ago

ECS integration/library project(s) (e.g. Elastic.CommonSchema.Serilog):Elastic.CommonSchema.Serilog,

ECS .NET assembly version (e.g. 1.4.2): 1.5.3

.NET framework / OS: netstandard2.0

Description of the problem, including expected versus actual behavior: I have a Powershell script that I attempt to run that creates an Ecs Text Formatted Log and either outputs that log to the console or to a file. I have found that this package works as expected when I am running in PowerShell 7, but it throws the following error when I attempt to run the same script in PowerShell 5.1.

System.NullReferenceException: Object reference not set to an instance of an object. at Elastic.CommonSchema.EcsDocument.GetAssemblyVersion(Assembly assembly) at Elastic.CommonSchema.EcsDocument.GetService() at Elastic.CommonSchema.EcsDocument.CreateNewWithDefaults[TEcsDocument](Nullable1 timestamp, Exception exception, IEcsDocumentCreationOptions options) at Elastic.CommonSchema.Serilog.LogEventConverter.ConvertToEcs[TEcsDocument](LogEvent logEvent, IEcsTextFormatterConfiguration1 configuration) at Elastic.CommonSchema.Serilog.EcsTextFormatter1.Format(LogEvent logEvent, TextWriter output) at Serilog.Sinks.SystemConsole.ConsoleSink.Emit(LogEvent logEvent)

Digging into the code, the issue originates with this call in the EcsDocument.cs -> GetService() method on the line var entryAssembly = Assembly.GetEntryAssembly();

When calling assembly.GetName(); on this Assembly object in the GetAssemblyVersion(Assembly assembly) method, it throws a null exception.

In PowerShell 7, if I output that value, it will read: pwsh, Version=7.3.3.500 Any idea why the Assembly.GetEntryAssembly(); is not compatible with PowerShell 5.1?

Mpdreamz commented 1 year ago

Thank you for bringing this to my attention!

I created: https://github.com/elastic/ecs-dotnet/pull/295 which will treat Assembly.GetEntryAssembly(); as something that can return null (e.g in IIS it might do as well).

It also brings the default service discovery in line with what we do in https://github.com/elastic/apm-agent-dotnet

buehlerfa commented 1 year ago

@Mpdreamz: Awesome! Any idea when we could expect a release with this change?