.Net Core EventLog - 覆盖未处理的异常被写入应用程序日志 [英] .Net Core EventLog - override unhandled exception are written to Application log

查看:14
本文介绍了.Net Core EventLog - 覆盖未处理的异常被写入应用程序日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已在我的 .Net Core 5.0 应用程序中配置了 EventLog 以将应用程序事件记录到自定义事件日志:

I'have configured EventLog in my .Net Core 5.0 app to log application events to custom event log:

public Startup (IConfiguration configuration)
    {
        this.configuration = configuration;
        this.logger = LoggerFactory.Create(logging=>{
            logging.AddConsole();
            logging.AddEventLog(settings=> {
                settings.LogName = configuration["EventLogName"];
                settings.SourceName = configuration["EventLogSourceName"];
                settings.Filter = (category, level) =>  level >= LogLevel.Trace;
            });
        }).CreateLogger("Stage server logger");
    }

我在 appsettings 中的日志配置:

My logging configuration in appsettings:

    "Logging": {
    "LogLevel": {
        "Default": "Information",
        "Microsoft": "Warning",
        "Microsoft.Hosting.Lifetime": "Information"
    },
    "EventLog": {
        "LogLevel": {
            "Default": "Information"
        }
    }
}

一切正常,但有些消息(特别是未处理的异常)被写入应用程序"日志而不是配置[EventLogName"] 日志.知道如何配置应用程序以将来自应用程序的所有消息记录到配置 [EventLogName"] 日志中?非常感谢

Everything works just fine but some messages (especialy unhandled exceptions) are written to "Application" log instead of configuration["EventLogName"] log. Any idea how to configure the app to log all messages from the application to configuration["EventLogName"] log? Thanks a lot

推荐答案

我看到您在 startup.cs 中创建了 Logger 的实例.我想你在你的 DI 中注册了它?如果是,您不会看到来自所有来源的日志,因为它们可能没有使用您的 Logger 实例.您只是在配置特定的 Logger,而不是 LoggerFactory.

I see that you made an instance of the Logger in your startup.cs. I suppose you registered it in your DI? If yes, you don't see logs from all the sources because they are probably not using your Logger instance. You're simply configuring a specific Logger, not LoggerFactory.

你能不能在 program.cs 中尝试这样的东西:

Could you try something like this in the program.cs:

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureLogging((hostBuilder, logBuilder) => {
                logBuilder.AddConsole();
                logBuilder.AddEventLog(set => {
                    set.LogName = hostBuilder.Configuration["EventLogName"];
                    set.SourceName = hostBuilder.Configuration["EventLogSource"];
                    set.Filter = (category, level) => level >= LogLevel.Trace;
                });
            })
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });

这样,我还为我没有控制的代码配置了 Logger - 依赖项.

This way, I'm configuring Logger also for the code that I'm not controlling - dependencies.

但是,如果您想要一个最小的解决方案来修复未处理的异常,那么快速的解决方法是创建一个带有 try-catch 的中间件,并通过由 DI 注入的特定 Logger 记录异常重新抛出.

But if you want a minimal effort solution to fix just unhandled exceptions, the quick fix would be creating a middleware with try-catch and rethrow with logging exception by your specific Logger injected by DI.

这篇关于.Net Core EventLog - 覆盖未处理的异常被写入应用程序日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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