Serilog duplicate log statements

639 views Asked by At

Getting duplicate logs like below for serilog using below nuget packages

  • Serilog.AspNetCore
  • Serilog.Settings.Configuration
  • Serilog.Sinks.Console

Console logs

Hosting environment: Local
15:44:33.961 +05:30 [Information] [] Serilog Initialized.
[15:44:33 INF] Serilog Initialized.
15:44:36.056 +05:30 [Information] [Serilog.AspNetCore.RequestLoggingMiddleware] HTTP "GET" "/swagger/index.html" responded 200 in 188.0470 ms
[15:44:36 INF] HTTP GET /swagger/index.html responded 200 in 188.0470 ms
15:44:36.557 +05:30 [Information] [Serilog.AspNetCore.RequestLoggingMiddleware] HTTP "GET" "/swagger/v1/swagger.json" responded 200 in 326.6425 ms
[15:44:36 INF] HTTP GET /swagger/v1/swagger.json responded 200 in 326.6425 ms

AppSetting.json

 "Serilog": {
    "MinimumLevel": {
      "Default": "Debug",
      "Override": {
        "Microsoft": "Error",
        "Microsoft.AspNetCore": "Error",
        "System": "Error"
      }
    },
    "Enrich": [
      "FromLogContext",
      "WithEnvironmentName",
      "WithMachineName"
    ],
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "outputTemplate": "{Timestamp:HH:mm:ss.fff zzz} [{Level}] [{SourceContext}] {Message}{NewLine}{Exception}"
        }
      }
    ],
    "Properties": {
      "ApplicationName": "MinimalApi-Local"
    }
  },

Program.cs

 builder.Services.AddLogging(logger =>
            {
                logger.ClearProviders();

                Log.Logger = new LoggerConfiguration()
                   .ReadFrom.Configuration(builder.Configuration)
                   .Enrich.FromLogContext()
                   .WriteTo.Console()
                   .CreateLogger();

                logger.AddSerilog();

                Log.Logger.Information("Serilog Initialized.");
            });


 builder.Host.UseSerilog();

 var app = builder.Build();
 app.UseSerilogRequestLogging();

Default logging configuration have been removed from appsetting.json. Please help me to remove default Microsfot logging behavior and allow only Serilog logs

1

There are 1 answers

0
BachDEV On

you need remove WriteTo.Console in Program.cs , output to console duplicate cause you have two WriteTo.Console , One from appSetting.json , one from

builder.Services.AddLogging(logger =>
        {
            logger.ClearProviders();

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

            logger.AddSerilog();

            Log.Logger.Information("Serilog Initialized.");
        });


builder.Host.UseSerilog();

var app = builder.Build();
app.UseSerilogRequestLogging();