1

I wanted to populate a value for correlationId to my applicationInsights . Right now the value is null. I have installed the serilog.enrichers.correlationId nuget package and have services.AddHTTPContextAccessor in my startup.cs file, but still the value of correlationId is null. Am i Missing something?Any suggestions? I got the source from :- https://github.com/ekmsystems/serilog-enrichers-correlation-id

        Log.Logger = new LoggerConfiguration()
            .ReadFrom.Configuration(Configuration)
            .CreateLogger();

the above is my logger configuration in startup.cs. all the other enrichers that i defined in Enrich tag in appsettings.json are being populated but the correlation id is null.

  • Is there any progress? Could you pls accept my post as the answer if it's helpful to you? Any further questions can freely added here. Thanks in advance : ) – Tiny Wang Jun 04 '21 at 02:15

3 Answers3

1

In Net 6 I configured logging of CorrelationId this way:

  1. Installed the packages Serilog.AspNetCore and Serilog.Enrichers.CorrelationId.

  2. In Program.cs file I added:

Log.Logger = new LoggerConfiguration()
    .CreateLogger();
   
builder.Services.AddHttpContextAccessor();

builder.Host.UseSerilog((ctxt, cfg) => cfg
            .ReadFrom.Configuration(ctxt.Configuration));
  1. And in appsettings.json file:
  "Serilog": {
    "Using": [ "Serilog.Sinks.Console" ],
    "Enrich": [ "WithCorrelationId" ],
    "MinimumLevel": {
      "Default": "Information",
      "Override": {
        "Microsoft": "Warning",
        "System": "Warning"
      }
    },
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "outputTemplate": "[{Timestamp:yyyy-MM-dd HH:mm:ss} | {CorrelationId} | {Level:u3}] {Message:lj}{NewLine}{Exception}"
        }
      }
    ]
  }

Example of using:

Log.Information("Starting of method GetPayments");

Rimi
  • 71
  • 2
0

Here's my test result:

enter image description here

And what I did like below:

In my asp.net core application, install related packages first, then edit the Program.cs -> CreateHostBuilder

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                }).UseSerilog((hostingContext, loggerConfiguration) => loggerConfiguration
                    .ReadFrom.Configuration(hostingContext.Configuration).Enrich.WithCorrelationId()
                    .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {CorrelationId} {Level:u3}] {Message:lj}{NewLine}{Exception}")
                    .WriteTo.ApplicationInsights(new TelemetryConfiguration { InstrumentationKey = "your_instrumentation_key" }, TelemetryConverter.Traces)
             );

My packages:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.17.0" />
    <PackageReference Include="Serilog.AspNetCore" Version="4.1.0" />
    <PackageReference Include="Serilog.Enrichers.CorrelationId" Version="3.0.1" />
    <PackageReference Include="Serilog.Settings.Configuration" Version="3.1.0" />
    <PackageReference Include="Serilog.Sinks.ApplicationInsights" Version="3.1.0" />
  </ItemGroup>

</Project>
Tiny Wang
  • 10,423
  • 1
  • 11
  • 29