Reputation: 103
I have 2 logger providers in my asp.net core project: 1. Console provider. 2. Database provider (custom provider). I do not want to log every time to all the providers, however, sometimes write to the Console provider and sometimes write to Database provider. I saw the filter provider options, but it let you to filter only by log level or category. Do ASP .net core Logging library has an option to write only to one provider? or should I have to write Logging service all by myself and not use ASP .net core Logging library?
Upvotes: 6
Views: 7786
Reputation: 243
Yes, you can use filtering to selectively log to Console vs. Debug. There is an overload for the AddConsole and AddDebug methods that lets you specific the criteria by which to filter to that logging provider.
public void Configure(IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
loggerFactory
.AddConsole(LogLevel.Warning)
.AddDebug((category, logLevel) => (category.Contains("TodoApi") && logLevel >= LogLevel.Trace));
For more info, see Microsoft's documentation: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging#log-filtering
Upvotes: 4