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] EcsTextFormatter doesn't use the IHostingEnvironment concept in dotnet core #355

Open HHobeck opened 7 months ago

HHobeck commented 7 months ago

ECS integration/library project(s) (e.g. Elastic.CommonSchema.Serilog): I'm using the Elastic.CommonSchema.Serilog.EcsTextFormatter, Elastic.CommonSchema.Serilog (8.6.1) to output my logs in the ECS standard.

ECS schema version (e.g. 1.4.0): The ecs.version is 8.6.0

ECS .NET assembly version (e.g. 1.4.2): Elastic.CommonSchema.Serilog.EcsTextFormatter, Elastic.CommonSchema.Serilog (8.6.1)

Elasticsearch version (if applicable): n/a

.NET framework / OS: .net 6.0

Description of the problem, including expected versus actual behavior: Depending on the stage I run my application in different environements like Testing, Staging or Production. There are different ways doing this:

Anyway in microsoft dotnet we have the concept of IHostBuilder who is responsible to setup the IHostingEnvironment. I would expect that in the log output the application name and the application environment will be used from IHostingEnvironment if no other variables like ELASTIC_APM_ENVIRONMENT, OTEL_RESOURCE_ATTRIBUTES or deployment.environment variables are existing.

Starting a console application could be look like on windows: $ ./JustAApplication.Console.exe --environment Testing --applicationName JustAnother.Application

Actual behaviour of the log output:

{
    "service": {
        "name": "JustAApplication.Console",
        "type": "dotnet",
        "version": "1.0.0"
    },
...
}

Expected behaviour of the log output:

{
    "service": {
        "name": "JustAnother.Application",
        "environment": "Testing",
        "type": "dotnet",
        "version": "1.0.0"
    },
...
}

Steps to reproduce:

  1. Start a dotnet application using Host.CreateDefaultBuilder(arguments).Run();
  2. Log an output using the ECS standard with Elastic.CommonSchema.Serilog.EcsTextFormatter, Elastic.CommonSchema.Serilog
  3. view the logs and you see that service.environment is not present and the service.name is not like ApplicationName