© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
1 reply
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
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

✅ Exception handling..
C#CC# / help
17mo ago
✅ Exception handling
C#CC# / help
17mo ago
❔ Exception Handling
C#CC# / help
3y ago
❔ Exception Handling
C#CC# / help
3y ago