【问题标题】:logging.AddAzureWebAppDiagnostics() does not work in .net core 2.2logging.AddAzureWebAppDiagnostics() 在 .net core 2.2 中不起作用
【发布时间】:2019-07-22 16:00:23
【问题描述】:

我已将我的项目从 .net core 2.1 更新到 2.2,然后 Program.cs 中的 logging.AddAzureWebAppDiagnostics() 不再工作。

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .ConfigureLogging((hostingContext, logging) =>
            {
                logging.AddAzureWebAppDiagnostics();
            })

            .UseStartup<Startup>()
            .Build();
}

“ILoggingBuilder”不包含“AddAzureWebAppDiagnostics”的定义,并且找不到接受“ILoggingBuilder”类型的第一个参数的可访问扩展方法“AddAzureWebAppDiagnostics”(您是否缺少 using 指令或程序集引用?

参考this document

如果面向 .NET Framework 或引用 Microsoft.AspNetCore.App 元包,请将提供程序包添加到项目中。在 ILoggerFactory 实例上调用 AddAzureWebAppDiagnostics:

所以方式可能与前一种方式略有不同。我该如何解决这个问题?

【问题讨论】:

  • 可能是个愚蠢的问题.. 你添加了using Microsoft.Extensions.Logging; 声明吗?
  • 是的,我有声明。它适用于 .net 2.0 和 2.1。
  • 也在链接文档中提到,If targeting .NET Core, note the following points: Don't explicitly call AddAzureWebAppDiagnostics.
  • 我也用Microsoft.AspNetCore.App,这就是为什么我提到了文档中的声明。
  • 谈论在 ILoggerFactory 实例上调用 AddAzureWebAppDiagnostics。这里有 ILoggingBuilder。如您所见here,该方法设置为已过时并带有注释will be removed in a future version.The alternative is AddAzureWebAppDiagnostics(this ILoggingBuilder builder)

标签: c# asp.net-core asp.net-core-2.2


【解决方案1】:

documentation 有点棘手,但如果仔细阅读,就会清楚应该采取以下步骤(对于 NET Core):

  1. 应该安装Microsoft.Extensions.Logging.AzureAppServices

  2. 无需拨打logging.AddAzureWebAppDiagnostics();

  3. 可以使用以下代码配置日志记录

    // file startup.cs
    using Microsoft.Extensions.Logging.AzureAppServices;
    
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            //...
            services.Configure<AzureFileLoggerOptions>(Configuration.GetSection("AzureLogging"));
        } 
    }
    

    文件appsettings.json 应包含

    "AzureLogging": {
         "FileName" : "azure-diagnostics-",
         "FileSizeLimit": 50024,
         "RetainedFileCountLimit": 5
    }
    
  4. 应在 Azure 门户上启用日志记录。启用后,Azure 门户可能会要求安装插件。需要安装插件的消息将出现在日志配置页面上。

  1. 在您的代码中调用logger.LogWarning ("message"); 以写入日志文件。如果您使用LogWarning,请务必将级别设置为警告或更详细(信息或调试)

【讨论】:

  • 感谢您的建议。有空我看看我的代码。
  • 有什么方法可以在本地测试吗?要将日志保存在本地?
  • 我找不到任何表明您不需要致电 logging.AddAzureWebAppDiagnostics(); 的信息。这是基于你读过的东西还是仅仅来自经验?您链接到的文档代码示例确实包含此代码,因此我认为拨打电话可能更安全。
  • @ajbeaven,如果您不想更改代码,Azure 会为您提供自动安装扩展的选项。如果你想明确,那么你可以手动安装 NuGet 并调用AddAzureWebAppDiagnostics()。见:mderriey.com/2020/08/08/…
  • @CrazyTim 太棒了,很好的发现!
猜你喜欢
  • 1970-01-01
  • 2020-04-28
  • 1970-01-01
  • 2020-02-27
  • 1970-01-01
  • 2020-01-25
  • 2021-10-22
  • 2019-12-31
  • 2020-12-06
相关资源
最近更新 更多