I created an exception handling middleware according to this example:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseExceptionHandler("/error");
// ...
}
[AllowAnonymous]
[ApiExplorerSettings(IgnoreApi = true)]
[ApiController]
public class ErrorsController : ControllerBase
{
[Route("error")]
public IActionResult Error()
{
return Problem();
}
}
Now let's say that I threw the following exception: throw new Exception("BAX");
My exception handling middleware catches this exception and works great. But the problem is that in console I see the following log:
|ERROR|Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware|An unhandled exception has occurred while executing the request.|System.Exception: BAX (stack trace goes here)
Note: I removed stack trace to make it a little bit shorter.
Maybe I should also say that I use NLog
for logging. Here is the configuration of it:
<target xsi:
type = "ColoredConsole" name = "colored_console"
layout = "|${level:uppercase=true}|${logger}|${message}|${exception:format=tostring}">
</target >
Question
My exception was caught by Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware
.
It looks like my exception handling middleware didn't handle the exception. Am I right?
My question is why it works so and how can I fix it?