Whenever an error occurs within our API, we end up getting multiple emails for a single error. Based on the log messages; we can see that these other emails seem to be getting generated because various Microsoft libraries are calling something like _logger.LogError
as well as our own _logger.LogError
that happens when we handle the error.
For example, when there is a database timeout, we see 4 emails from these different classes:
- Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware
- Microsoft.EntityFrameworkCore.Database.Command
- Microsoft.EntityFrameworkCore.Query
- Web.Controllers.ErrorController (Our own exception handler; this is the only one we want to see)
The last one is the only one that has our own error formatting with helpful information in it such as current user, etc. The others just contain a stack trace, which is already within our own formatted email.
We can't figure out for sure where these other log messages are coming from; but the most likely thing I can think of is that within Microsoft's libraries, they are calling _logger.LogError()
, and our own NLog configuration is handling all instances of LogError
; instead of just handling our own.
How can we prevent these other log statements from being logged and especially emailed to us?
This is our setup in Program.cs:
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
})
.UseNLog();