Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I lower the log level of the mvc pipeline in aspnetcore?

So in an ASPNETCORE 2.0 project, I added a logging provider (serilog) to the logging factory along with a console sink. It works great, however I noticed that all the framework request pipelines (like http request response) are logging every little detail as INFO.

[17:37:26 INF] Request starting HTTP/1.1 GET http://localhost:5000/test/health
[17:37:26 INF] Executing action method DAS.Gateways.Command.Api.Controllers.TestController.HealthCheck (DAS.Gateways.Command.Api) with arguments (null) - ModelState is Valid
[17:37:26 INF] Health check called.
[17:37:27 INF] Executing ObjectResult, writing value Microsoft.AspNetCore.Mvc.ControllerContext.
[17:37:27 INF] Executed action DAS.Gateways.Command.Api.Controllers.TestController.HealthCheck (DAS.Gateways.Command.Api) in 203.8825ms
[17:37:27 INF] Request finished in 343.9801ms 200 application/json; charset=utf-8
[17:38:07 INF] Request starting HTTP/1.1 GET http://localhost:5000/test/health
[17:38:07 INF] Executing action method DAS.Gateways.Command.Api.Controllers.TestController.HealthCheck (DAS.Gateways.Command.Api) with arguments (null) - ModelState is Valid
[17:38:07 INF] Health check called.
[17:38:07 INF] Executing ObjectResult, writing value Microsoft.AspNetCore.Mvc.ControllerContext.
[17:38:07 INF] Executed action DAS.Gateways.Command.Api.Controllers.TestController.HealthCheck (DAS.Gateways.Command.Api) in 53.5876ms
[17:38:07 INF] Request finished in 60.2195ms 200 application/json; charset=utf-8

Info is the minimum log level we use for production logging and we have a Loggly sink that has a 1GB/day limit so I feel like MVC logging all request information as INFO is a bit obnoxious and I'd like to lower it to DEBUG. To be clear, I don't want to raise my logging level to prevent INFO from reaching the sink, I want .Net to lower it's logging level from INFO to DEBUG.

Is this possible and how?

like image 292
Sinaesthetic Avatar asked Aug 23 '17 00:08

Sinaesthetic


People also ask

What is logger MVC?

It is an open source framework. Log4net provides a simple mechanism for logging information to a variety of sources. Information is logged via one or more loggers. These loggers are provided at the below levels of logging: Debug.

Where are .NET logs stored?

Most . NET programs write logs to a database or Windows Event Log if running in Windows and to the /var/log folder if running in Linux. There are security issues with flat files, but their use is also common. You'll need extra storage space for logging, regardless of the method used.

What is NET Core logger?

In ASP.NET Core, logging providers store the logs. You can configure multiple logging providers for your application. The default ASP.NET Core configures the following logging providers: Console, Debug, EventSource, and EventLog (on Windows).


1 Answers

You need to use log filtering.

You can specify a minimum log level for a specific provider and category or for all providers or all categories. Any logs below the minimum level aren't passed to that provider, so they don't get displayed or stored.

Filter rules may be defined via configuration or in code. Example of how it may look in code:

    // using Serilog.Extensions.Logging;

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .ConfigureLogging(logging => 
            {
                logging.AddFilter<SerilogLoggerProvider>("Microsoft", LogLevel.Warning);
            }
            .UseStartup<Startup>()
            .Build();

Note, that AddFilter in this case applies only to Serilog log provider as we specify a provider type. If you want to define a filter for all providers, use:

logging.AddFilter("Microsoft", LogLevel.Warning);

like image 130
Set Avatar answered Sep 28 '22 07:09

Set