繁体   English   中英

如何关闭 Azure Function 中的 NLog 日志记录?

[英]How to shutdown NLog logging in an Azure Function?

当 Azure Functions 实例化我的程序集时,我需要设置 NLog。

public class Startup : FunctionsStartup {
    public Startup()
    {        
        var logger = NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
    }
    public override void Configure(IFunctionsHostBuilder builder) {
        ... other setup code
        builder.Services.AddLogging((loggingBuilder) =>
        {
            loggingBuilder.AddNLog();
        });
    }
}

NLog 人建议通过NLog.LogManager.Shutdown()手动关闭记录器。 问题是我不确定如何捕获 Azure 的实例来关闭我的程序集。

Azure 是否公开了某种我没有看到的 Dispose 事件?

看起来您依赖于 Microsoft 扩展日志记录,所以我可能会挂钩它的生命周期逻辑(禁用AutoShutdown并启用ShutdownOnDispose

public class Startup : FunctionsStartup {
    public Startup()
    {
        var logger = LogManager.Setup()
           .SetupExtensions(e => e.AutoLoadAssemblies(false))
           .LoadConfigurationFromFile("nlog.config", optional: false)
           .LoadConfiguration(builder => builder.LogFactory.AutoShutdown = false)
           .GetCurrentClassLogger();
    }

    public override void Configure(IFunctionsHostBuilder builder) {
        ... other setup code
        builder.Services.AddLogging((loggingBuilder) =>
        {
            loggingBuilder.AddNLog(new NLogProviderOptions() { ShutdownOnDispose = true });
        });
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM