如何禁用 Serilog 的自动异常记录功能? [英] How can I disable Serilog's automatic exception logging feature?

查看:55
本文介绍了如何禁用 Serilog 的自动异常记录功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SeriLog(带 Loki 接收器),而且我还有一个 Exception handler lambda-就像这里,它捕获代码执行期间发生的所有异常,并用适当的标签记录它们.

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.

但是,Serilog 本身会自动捕获所有未处理的异常并在我之前记录它们.结果,一个异常被记录了两次.

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

如何禁用 Serilog 的自动日志记录?这是我当前的 Serilog 配置:

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);
                });

推荐答案

我关注了 这个 GitHub issue.问题是,我自己的日志中间件和 Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware 都记录了相同的异常.

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

解决方案是禁用ExceptionHandlerMiddleware.我已经通过 https://github.com/serilog/serilog-expressions 做到了喜欢:

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

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

最终代码:

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>();
                });
    }
}

这篇关于如何禁用 Serilog 的自动异常记录功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆