Jason Richmeier
Jason Richmeier

Reputation: 1645

MVC 6 Standard Logging Messages

I am implementing logging in my MVC application.

I noticed that the following two messages are being logged automatically:

"Information : Request successfully matched the route with name 'XXX' and template 'XXX'." "Verbose : Executing action"

My question is whether or not it is possible to turn these messages off and how it is accomplished. I am sure it is in the configuration somewhere but I have yet to find it.

Upvotes: 1

Views: 485

Answers (2)

Stafford Williams
Stafford Williams

Reputation: 9806

You need to add a filter that will ignore the logs you don't care about, while continuing to allow the ones you do care about (like the ones your code makes) to continue to be logged - regardless of level.

This will depend on which LoggerProvider you are using, or whether you are rolling your own. As you haven't supplied any code registering your LoggerProvider, I'll use the sample app from Logging/AspNet as an example. Note line #25:

factory.AddConsole((category, logLevel) => logLevel >= LogLevel.Critical &&
    category.Equals(typeof(Program).FullName)); // restricts to Program

The category filter above is restricting the logger to log only items that are produced by the SampleApp.Program object (while additionally only allowing Critical or higher logs, but we don't care about that bit)

Now you could go ahead and whitelist every namespace you want to log, but it's easier to blacklist the ones you don't want. The Microsoft.AspNet.* libraries are pretty noisy, so you could add the following filter to stop them from logging:

factory.AddConsole((category, logLevel) => !category.StartsWith("Microsoft.AspNet"));

If you've rolled your own ILogger, then you'll just need to ensure you've included the filter as a constructor parameter and that you reference it in your implementation of ILogger.IsEnabled.

Upvotes: 2

Joe Audette
Joe Audette

Reputation: 36706

Logging has different levels, you can set it to a higher level and it will eliminate information level logging.

// LogLevels
//Debug = 1,
//Verbose = 2,
//Information = 3,
//Warning = 4,
//Error = 5,
//Critical = 6,
loggerFactory.MinimumLevel = LogLevel.Warning;

you would set this in Startup.cs in the Configure method

Upvotes: 0

Related Questions