Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why Serilog Enrich WithThreadId throws error?

I have a problem with Serilog throwing Unable to find a method called WithThreadId..

The version of Serilog I’m using is Serilog.AspNetCore 6.1.0. The way I register the logger is as follows:

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .UseSerilog((ctx, cfg) =>
                {
                    cfg.ReadFrom.Configuration(ctx.Configuration);
                    cfg.Enrich.WithProperty("EnvironmentName", ctx.HostingEnvironment.EnvironmentName);
                })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder
                        .UseStartup<Startup>()
                        .UseKestrel()
                        .ConfigureServices((ctx, services) =>
                        {
                            services.Configure<KestrelServerOptions>(ctx.Configuration.GetSection("Kestrel"));
                        });
                });
    }

I have cascading appsettings.json, so in my appsettings.json for serilog I have:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
  "AllowedHosts": "*",
  "Serilog": {
    "Using": [ "Serilog.Sinks.Console" ],
    "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId", "WithEnvironmentUserName" ],
    "Properties": {
      "Application": "STP.IM.DocumentPoolService"
    }
  }
}

In the following appsettings file I have:

{
  "Serilog": {
    "MinimumLevel": "Information",
    "Override": {
      "Microsoft": "Information",
      "System": "Information"
    },
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "outputTemplate": "[{Timestamp:HH:mm:ss.ms} {Level:u3}] {ThreadId} {MachineName} {EnvironmentUserName} {Message:lj} <s:{SourceContext}>{NewLine}{Exception}"
        }
      }
    ]
  },
}

The full error I got is:

SERILOG SELFLOG: 2023-06-16T05:49:29.7322919Z Unable to find a method called WithThreadId. Candidate methods are:
Serilog.LoggerConfiguration WithEnvironmentName(Serilog.Configuration.LoggerEnrichmentConfiguration)
Serilog.LoggerConfiguration WithMachineName(Serilog.Configuration.LoggerEnrichmentConfiguration)
Serilog.LoggerConfiguration WithEnvironmentUserName(Serilog.Configuration.LoggerEnrichmentConfiguration)
Serilog.LoggerConfiguration When(Serilog.Configuration.LoggerEnrichmentConfiguration, System.String, System.Action`1[Serilog.Configuration.LoggerEnrichmentConfiguration])
Serilog.LoggerConfiguration With(Serilog.Configuration.LoggerEnrichmentConfiguration, Serilog.Core.ILogEventEnricher)
Serilog.LoggerConfiguration AtLevel(Serilog.Configuration.LoggerEnrichmentConfiguration, System.Action`1[Serilog.Configuration.LoggerEnrichmentConfiguration], Serilog.Events.LogEventLevel, Serilog.Core.LoggingLevelSwitch)
Serilog.LoggerConfiguration FromLogContext(Serilog.Configuration.LoggerEnrichmentConfiguration)

I’m wondering what I’m doing wrong?

Can somebody give me a hint?

Thanks in advance,

Julian

>Solution :

You should install this nuget package

https://github.com/serilog/serilog-enrichers-thread

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading