.NET 6 APM with Serilog throws exception inside Elastic.CommonSchema.Serilog

Kibana version:

Elasticsearch version:

APM Server version:

APM Agent language and version:
.NET Core - Elastic.Apm.NetCoreAll v. 1.22.0

Original install method (e.g. download page, yum, deb, from source, etc.) and version:
Downloaded from NugetPackage

Fresh install or upgraded from other version?
Fresh install

Further explanation:
I'm having some issues to Setup the log correlation with Serilog, my stack is .net 6 web api.

Before starting to use Elastic.Apm.NetCoreAll, I had Serilog sinking logs straight to Elasticsearch and was working well I could see logs in Kibana and build some small dashboards, I'm using Elastic Cloud from Azure to host our containers.

My program class for the initial Setup.

public class Program
        public static void Main(string[] args)
            var builder = WebApplication.CreateBuilder(args);


            var startup = new Startup(builder.Configuration, builder.Environment);


            var app = builder.Build();

            startup.Configure(app, app.Environment);


        public static void ConfigureLogging()
            var configuration = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                    optional: true)

            var environment = configuration["Environment"];

            Log.Logger = new LoggerConfiguration()
                .WriteTo.Async(x => x.Elasticsearch(ConfigureElasticSink(configuration, environment)))
                .Enrich.WithProperty("Environment", environment)

        public static ElasticsearchSinkOptions ConfigureElasticSink(IConfigurationRoot configuration, string environment)
            var uri = configuration["Serilog:ElasticConfiguration:Uri"];
            var indexFormat =
                $"{Assembly.GetExecutingAssembly().GetName().Name?.ToLower().Replace(".", "-")}-{environment?.ToLower().Replace(".", "-")}-{DateTime.UtcNow:yyyy-MM}";
            return new ElasticsearchSinkOptions(new Uri(uri))
                AutoRegisterTemplate = true,
                BufferLogShippingInterval = TimeSpan.FromMinutes(5),
                BatchAction = ElasticOpType.Create,
                ModifyConnectionSettings = x => x.BasicAuthentication("xxx", "xxxx"),
                IndexFormat = indexFormat


"Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
  "AllowedHosts": "*",
  "Serilog": {
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Information",
        "Microsoft.Hosting.Lifetime": "Information",
        "System": "Warning"
    "ElasticConfiguration": {
      "Uri": "xxx"
    "AllowedHosts": "*",
    "WriteTo": [
        "Name": "Console",
        "Args": {
          "outputTemplate": "{Timestamp} [{Level}] {Message}{NewLine:1}"
    "Properties": {
      "Application": "Solution.Api"

Updates to enable APM:
I installed these other nuget packages for the APM
Elastic.Apm.NetCoreAll v1.22
Elastic.Apm.SerilogEnricher v8.6
Elastic.CommonSchema.Serilog v.8.6

After installing the APM, I added "Elastic.Apm": "Debug" to the Logging -> Debug, and also the APM settings

  "ElasticApm": {
    "ServerUrl": "xxx",
    "SecretToken": "xxxx",
    "ServiceName": "esg-api",
    "ServiceVersion": "1.0",
    "TransactionSampleRate": 0.2,
    "SpanStackTraceMinDuration": "100ms",
    "MetricsInterval": "100s"

1- Added to the ElasticsearchSinkOptions the CustomFormatter = new EcsTextFormatter()
2- Added to the LoggerConfiguration the .Enrich.WithElasticApmCorrelationInfo()

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

This is my first exposure to Elasticsearch and also to Serilog.
APM itself is working well, also the serilog is sinking data to my Elasticsearch, however when using both together, I'm getting an exception inside the Elastic.CommonSchema.Serilog,

What am I missing?
I tried to Enrich manually the ElasticApmServiceName, ElasticApmServiceVersion and ElasticApmServiceNodeName but I'm getting the same exception.

Hi @guisantos,

I looked into this and to me this seems like a bug in Elastic.CommonSchema.Serilog. The good new is that this null-check is already improved - that was done in this PR, the bad news is that the latest version (which you use) does not yet have this PR.

So I think this will be fixed once we have the next release out.

In case you want to make sure we don't forget, feel free to file an issue in the GitHub repository - that's what the dev-team looks at. Please also link to this discussion in case you file a GitHub issue to have the full context.

1 Like

Thx Greg!

I was pulling my hair off hahahahah I'll fill a bug there as well just in case.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.