Excessive logging to Serilog Console Sink negatively affects performance

c-sharp

#1

I have an ASP.NET Core 2.0 project using Serilog configured through appsettings.json. My config looks like this:

  "Serilog": {
    "Using": [ "Serilog.Sinks.Console" ],
    "MinimumLevel": {
      "Default": "Debug",
      "Override": {
        "MediatR.IMediator": "Information", 
        "Microsoft": "Information",
        "Microsoft.AspNetCore.Mvc": "Information",
        "Microsoft.EntityFrameworkCore": "Warning",
        "Foo": "Information"
      }
    },
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          //"formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact",
          "theme": "Serilog.Sinks.SystemConsole.Themes.AnsiConsoleTheme::Code, Serilog.Sinks.Console",
          "outputTemplate": "[{Timestamp:HH:mm:ss} {Level:u3}] {RequestId}-{SourceContext} {$Scope}: {Message:lj}{NewLine}{Exception}"
        },
        "restrictedToMinimumLevel":  "Information" 
      },
      {
        "Name": "ApplicationInsightsTraces",
        "Args": { "instrumentationKey": "XXXXXXX" } 
      }
    ],
    "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId", "WithExceptionDetails" ],
    "Properties": {
      "Application": "Foo"
    }
  },

I find the verbose output to the console very helpful when debugging. Its the “right” level of verbosity, and the ApplicationInsights data isn’t updated real time so I can’t stream the logs from there for debugging. Unfortunately, that level of logging slows the rest calls down A LOT. Is there something I can do to asynchronously write to the console so I don’t sacrifice performance?


#2

Hi @zippy1981!

I’m not sure about asynchronously writing to the console, but could you try writing the log to a file, and monitoring the file for new content in a separate console window? I believe writing to a file should be a lot faster than writing to/rendering on the console, and this approach would decouple the logging I/O from the console.

Assuming you’re on Windows, you should be able to do something like the following in PowerShell to monitor the file my.log, where the -Tail 5 option means only list the most recent 5 lines, instead of the whole file, when you run the command:

Get-Content my.log -Wait -Tail 5

I hope this helps!