0

I am using SeriLog(with Loki sink), and I also have an Exception handler lambda-like in here, which catches all exceptions happened during code execution and logs them with appropriate labels.

But, Serilog itself, automatically catches all unhandled exceptions and logs them before me. As a result, an exception is being logged twice.

How can I disable Serilog's automatic logging? This is my current Serilog Config:

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                })
                .ConfigureLogging((logging) =>
                {
                    var log = new LoggerConfiguration()
                            .MinimumLevel.Debug()
                            .Enrich.FromLogContext()
                            .WriteTo.Console()
                            .CreateLogger();
                    logging.AddSerilog(log);
                });
Davud Safarov
  • 498
  • 4
  • 12
  • 1
    This isn't really "Serilog's automatic exception logging feature"... Have you seen the [instructions](https://github.com/serilog/serilog-aspnetcore#instructions) that uses `UseSerilog` instead of `AddSerilog`? I think you might be ending up with the built-in Console logger _and_ Serilog's Console logger, which will end up with double logs. – Kirk Larkin Jan 18 '21 at 19:42
  • 1
    @KirkLarkin Thanks for taking your time and answering. I looked at that now, and I ended up finding that the reason for my case is that, Exception is being logged by 1)Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware, and 2)My own middleware. So, I need to disable ExceptionHandlerMiddleware, I think – Davud Safarov Jan 18 '21 at 20:06

1 Answers1

0

I followed this GitHub issue. The problem is that, both my own logging middleware and Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware logs the same exception.

The solution is to disable ExceptionHandlerMiddleware. And I've done it by https://github.com/serilog/serilog-expressions like:

new LoggerConfiguration().Filter.ByExcluding("SourceContext <> 'Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware'")

Final code:

using System;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Serilog;
using Serilog.Events;
using Serilog.Sinks.Loki;

namespace MyNameSpace
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .Filter.ByExcluding("SourceContext = 'Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware'")
            .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
            .Enrich.FromLogContext()
            .WriteTo.LokiHttp(new NoAuthCredentials("http://localhost:3100"))
            .CreateLogger();

            try
            {
                Log.Information("Starting web host");
                CreateHostBuilder(args).Build().Run();
                return;
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "Host terminated unexpectedly");
                return;
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .UseSerilog()
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }
}

Davud Safarov
  • 498
  • 4
  • 12