Home > Enterprise >  Serilog only log warnings, errors and critical
Serilog only log warnings, errors and critical

Time:09-19

I have the below entry in my appsettings:

{
  "Serilog": {
    "WriteTo": [
      {
        "Name": "Console",
        "LogLevel": {
          "TestProj": "Information"
        }
      },
      {
        "Name": "File",
        "Args": {
          "path": "logs/testlog.txt",
          "rollingInterval": "Day"
        },
        "LogLevel": {
          "TestProj": "Warning",
          "Microsoft.AspNetCore": "Warning"
        }
      }
    ]
  }
}

In my Controller I have the below informational message being logged:

    public WeatherForecastController(ILogger<WeatherForecastController> logger, IWeatherService weatherService)
    {
        _logger = logger;
        _weatherService = weatherService;
    }

        public ActionResult<IEnumerable<WeatherForecast>> Get()
    {
        _logger.LogInformation("Called Get Method");
        return Ok(_weatherService.GetWeatherSummary());
    }

Where WeatherForecastController is in the namespace TestProj.API.Controllers

When I check the testlog.txt I'm seeing the log entry

[INF] Called Get Method

along with a bunch of other Informational logs like:

[INF] Now listening on: http://localhost:5000
[INF] Application started. Press Ctrl C to shut down.

The Console seems to show Information and up, but file seems to be the same. Is there anyway to get File to only show Warning/Error/Critical? And to avoid even the Now listening on...

CodePudding user response:

I got it to work with the below:

{
   "Serilog":{
      "MinimumLevel":"Information",
      "WriteTo":[
         {
            "Name":"Console"
         },
         {
            "Name":"File",
            "Args":{
               "path":"logs/testlog_d.txt",
               "rollingInterval":"Day",
               "restrictedToMinimumLevel":"Warning"
            }
         }
      ]
   }
}
  • Related