Details

Logging with Serilog.AspNetCore

Install the following required packages

  • Serilog.AspNetCore
  • Serilog.Sinks.File
  • Serilog.Formatting.Compact

Add the following line of code to the Program.cs

builder.Host.UseSerilog((context, loggerConfiguration) =>
{
   loggerConfiguration.WriteTo.Console();
   loggerConfiguration.ReadFrom.Configuration(context.Configuration);
});

And then add the configurations to the appsetings.json

  "Serilog": {
   "MinimumLevel": {
     "Default": "Information",
     "Override": {
       "Microsoft": "Warning",
       "Microsoft.AspNetCore.Hosting.Diagnostics": "Error",
       "Microsoft.Hosting.Lifetime": "Information"
     }
   },
   "WriteTo": [
     {
       "Name": "File",
       "Args": {
         "path": "C:\\Users\\Ebee\\Documents\\Project_Name\\Logs\\log.txt",
         "rollingInterval": "Month",
         "rollOnFileSizeLimit": true,
         "formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact"
       }
     }
   ]
 }

Then you log exactly the way you would using Ilogger<T>();