Reputation: 1037
The following boilerplate, Microsoft-provided C# code for ASP.NET Core 2.1 should prevent merely informational logs from being written. But it doesn't.
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
var env = hostingContext.HostingEnvironment;
config.AddEnvironmentVariables();
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConsole();
logging.AddDebug();
logging.AddEventSourceLogger();
})
.ConfigureLogging(logging => logging.SetMinimumLevel(LogLevel.Warning))
.UseStartup<Startup>();
I still see messages like:
MyApp> info: Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker[2]
and
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker:Information: Executed action /Index in 11.2286ms
I have deliberately removed (for now) the code to call AppSettings.json. Why am I still seeing information-severity log items?
Upvotes: 1
Views: 10914
Reputation: 11
For me it worked using the AddFilter<>() generic method (I'm working with ASP.Net Core 3.1). I'm logging to the windows event log and I just want warnings and errors going there. So, I added a filter to the EventLogLoggerProvider to only log warnings from "Microsoft" category.
services.AddLogging(logging =>
{
logging.AddEventLog(eventLogSettings =>
{
eventLogSettings.LogName = Constants.Service.EventLogName;
eventLogSettings.SourceName = Constants.Service.EventSourceName;
});
logging.AddFilter<EventLogLoggerProvider>("Microsoft", LogLevel.Warning);
});
Upvotes: 1
Reputation: 12725
Try the following change in your code, set the category of the first AddFilter is null
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
var env = hostingContext.HostingEnvironment;
config.AddEnvironmentVariables();
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConsole();
logging.AddFilter(null, LogLevel.Warning);
})
.UseStartup<Startup>()
.ConfigureLogging(logging => logging.AddFilter("Microsoft", LogLevel.Warning));
Add logWarning to test in Home/Index
private readonly ILogger _logger;
public IndexModel(ILogger<IndexModel> logger)
{
_logger = logger;
}
public void OnGet()
{
_logger.LogInformation("Log Information");
_logger.LogWarning("Log Warning");
}
The screenshot of result
Upvotes: 3