azure - NLog 自定义目标在 Azure .NET Core 2.1 中失败

标签 azure nlog asp.net-core-2.1

我已经使用 NLog 与 .NET Core 2.0 和自定义目标成功写入 Azure Blob 存储一段时间了。

我现在已升级到 .NET Core 2.1,但部署到 Azure Web App 的解决方案失败,因为根据 Kudu 事件日志,NLog 无法找到 NLog 配置文件中定义的自定义目标,尽管它似乎在本地工作得很好

我的主机构建器如下:

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseUnityServiceProvider()
            .UseNLog()
            .UseStartup<Startup>();

我的 NLog 目标是在启动类中定义的:

        public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", false, true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", true)
            .AddJsonFile("appsettings.local.json", true)
            .AddEnvironmentVariables();

        Configuration = builder.Build();
        HostingEnvironment = env;

        NLogRegistry.Register(env, new ConfigurationAdapter(Configuration));
    }

NLog 注册表只是基于 Custom target with injected services for NLog with .net core 的解决方案的包装器

    public class NLogRegistry
{
    public static void Register(IHostingEnvironment env, IConfigurationAdapter configuration)
    {
        //Setup custom NLog Azure Blob logger with injected configuration
        Target.Register<AzureBlobStorageTarget>("AzureBlobStorage");

        var nlog = ConfigurationItemFactory.Default.CreateInstance;
        ConfigurationItemFactory.Default.CreateInstance = type =>
        {
            try
            {
                return nlog(type);
            }
            catch (Exception)
            {
            }

            return new AzureBlobStorageTarget(configuration);
        };

        env.ConfigureNLog("nlog.config");
    }
}

我认为正在发生的情况是 .NET Core 管道的行为发生了一些变化,因此 NLog 在 Startup 方法之前被调用。由于 NLog 配置为“自动发现”nlog.config,因此它会在我有机会正确配置目标之前尝试进行设置。

如果我重命名 nlog.config 文件,则自动发现不会发生,并且 NLog 必须等到我在寄存器类中运行 ConfigureNLog 方法。然后,一切正常。

有谁知道 Azure 调用的 ASP.NET Core 2.1 管道中的正确位置是什么,以确保我可以在 NLog 尝试自动配置自身之前正确配置 NLog 目标?

最佳答案

现在,您可以仅使用 AzureBlobStorageTarget 属性的 NLog 布局类型,而不是在构造函数中将 IConfiguration 注入(inject)到 AzureBlobStorageTarget。

然后使用 NLog.Extension.Logging 版本引入的 ${configsetting} 配置布局。 1.4.0:

https://github.com/NLog/NLog/wiki/ConfigSetting-Layout-Renderer

也许还可以考虑更改为这个 NLog-target:

https://github.com/JDetmar/NLog.Extensions.AzureStorage#blob-configuration

但是如果您坚持使用依赖于构造函数参数的依赖注入(inject)的自定义目标:

https://github.com/NLog/NLog/wiki/Dependency-injection-with-NLog

关于azure - NLog 自定义目标在 Azure .NET Core 2.1 中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51060539/

相关文章:

c# - 将自托管 SignalR 后端部署到 Azure

c# - 如何在运行时更改 NLog 布局?

asp.net-core - 删除 Kestrel 绑定(bind)警告

c# - 使用 NLog 在 ASP.NET Core 中记录异常

c# - 如何将 Microsoft Application Insights 与 NLog 结合使用(找不到目标 : 'ApplicationInsights' )

c# - Razor Pages Web 应用程序 (.Net core 2.1) - 如何处理从一个区域页面到另一个区域页面的重定向

asp.net-core-2.1 - 使用 AddDistributedRedisCache 时为 IDistributedCache.SetAsync 设置过期时间

azure - Pyspark - 数据透视表

azure - 如何在Azure Web App盒子上执行appcmd?

Azure机器学习工作室: cannot create Datastore from Azure SQL Database