C
C#2y ago
Aljo_M

❔ Serilog duplicate log with global exception handling

hey i am building an asp.net core web api and i implemented global exception handling like this:
public static class GlobalExceptionExtensions
{
public static void ConfigureExceptionHandler(this WebApplication app)
{
var logger = app.Services.GetRequiredService<ILogger<Program>>();

app.UseExceptionHandler(appError =>
{
appError.Run(async context =>
{
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
context.Response.ContentType = "application/json";
var contextFeature = context.Features.Get<IExceptionHandlerFeature>();
if (contextFeature != null)
{
logger.LogError($"Something went wrong: {contextFeature.Error}");
await context.Response.WriteAsync(new ErrorDetails()
{
StatusCode = context.Response.StatusCode,
Message = "Internal Server Error.",
}.ToString());
}
});
});
}
}
public static class GlobalExceptionExtensions
{
public static void ConfigureExceptionHandler(this WebApplication app)
{
var logger = app.Services.GetRequiredService<ILogger<Program>>();

app.UseExceptionHandler(appError =>
{
appError.Run(async context =>
{
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
context.Response.ContentType = "application/json";
var contextFeature = context.Features.Get<IExceptionHandlerFeature>();
if (contextFeature != null)
{
logger.LogError($"Something went wrong: {contextFeature.Error}");
await context.Response.WriteAsync(new ErrorDetails()
{
StatusCode = context.Response.StatusCode,
Message = "Internal Server Error.",
}.ToString());
}
});
});
}
}
however in the console i still get unhandled exception log message in adition to log message that this produces. what is the problem? i am using serilog, if you need some extra information i will gladly provide. thank you
1 Reply
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server