Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling trace-like INFO logging by MVC

When enabling application logging for an ASP.NET Core MVC application, I'm finding a lot of low-level detail is logged for each request made to the API (see below for an example).

I find this is polluting the application log (especially since we receive hundreds of requests for minute) with trace-like details, making it really difficult to work with.

Is there any way to disable this? Or perhaps I should be looking at another approach to logging?

Example INFO logs for an API request

2017-03-14 09:23:46.944 +00:00 [Information] Request starting HTTP/1.1 GET http://example.net/api/something application/json

2017-03-14 09:23:46.944 +00:00 [Information] Successfully validated the token.

2017-03-14 09:23:46.944 +00:00 [Information] HttpContext.User merged via AutomaticAuthentication from authenticationScheme: "Bearer".

2017-03-14 09:23:46.944 +00:00 [Information] AuthenticationScheme: "Bearer" was successfully authenticated.

2017-03-14 09:23:46.944 +00:00 [Information] Authorization was successful for user: null.

2017-03-14 09:23:46.944 +00:00 [Information] Executing action method "Xyz.GetAsync (Xyz.Api)" with arguments (["Xyz.Something", "", "", "", "", "0", "100", "", "2017-06-01T00:00:00Z", "False"]) - ModelState is Valid

2017-03-14 09:23:47.115 +00:00 [Information] Executing JsonResult, writing value "Xyz.Resources.SomeModel[]".

2017-03-14 09:23:47.115 +00:00 [Information] Executed action "Xyz.GetAsync (Xyz.Api)" in 206.5169ms

2017-03-14 09:23:47.115 +00:00 [Information] Request finished in 216.8241ms 200 application/json; charset=utf-8

like image 280
Dave New Avatar asked Nov 14 '25 16:11

Dave New


2 Answers

looks like the hooks for this have changed slightly in netcoreapp2.0:

public virtual void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<ILoggerFactory, LoggerFactory>();
    services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
    services.AddLogging(builder =>
    {
        builder.SetMinimumLevel(LogLevel.Trace);
        builder.AddFilter("Microsoft", LogLevel.Warning);
        builder.AddFilter("System", LogLevel.Error);
        builder.AddFilter("Engine", LogLevel.Debug);
    });

    /// ... etc
}

adding the above did the trick for me.

like image 198
user326608 Avatar answered Nov 17 '25 08:11

user326608


Log Level

These all appear to be Information, you should be able to adjust this in the appsettings.json such that the level of entries that are logged are greater than "info"... see Log Level.

Log Event Id

The log level is application-wide, so all things that use logging are equal in that regard. If you want your logs to stand out and find them to be more important log them as such. Log them as warnings - if appropriate. Also, as an alternative you could use the EventId to start isolating logs. The idea being that you could filter the logs against the "id" to quickly find things of interest.

Filters

Finally, you could apply filters.

You can set filtering rules for all providers that are registered with an ILoggerFactory instance by using the WithFilter extension method. The example below limits framework logs (category begins with "Microsoft" or "System") to warnings while letting the app log at debug level.

public void Configure(IApplicationBuilder app,
    IHostingEnvironment env,
    ILoggerFactory loggerFactory)
{
    loggerFactory
        .WithFilter(new FilterLoggerSettings
        {
            { "Microsoft", LogLevel.Warning },
            { "System", LogLevel.Warning },
            { "ToDoApi", LogLevel.Debug }
        })
        .AddConsole()
        .AddDebug()
}

I think filters is what you'd be looking for here. Specify that both Microsoft and System are at LogLevel.Warning, then you should only see important logs from them.

like image 42
David Pine Avatar answered Nov 17 '25 09:11

David Pine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!