C#C
C#3y 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());
         }
       });
     });
    }
  }

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
Was this page helpful?