【问题标题】:Microsoft.Extensions.Logging.AzureAppServices not showing up in Azure log streamsMicrosoft.Extensions.Logging.AzureAppServices 未显示在 Azure 日志流中
【发布时间】:2020-02-04 17:26:10
【问题描述】:

我觉得我已经尝试了所有方法来获取 Azure 门户中显示的日志消息。

我已经在我的项目中安装了相关的 NuGet 包:

我已启用 Azure 门户中的日志记录。

我已将以下内容添加到我的appsettings.json

"AzureLogging": {
    "FileName": "azure-diagnostics-",
    "FileSizeLimit": 50024,
    "RetainedFileCountLimit": 5
  }

以及Startup 中的以下设置代码,以确保它已添加其中之一。

        services.Configure<AzureFileLoggerOptions>(options => 
        {
            options.FileName = "azure-diagnostics-";
            options.FileSizeLimit = 50 * 1024;
            options.RetainedFileCountLimit = 5;
        });

我能从我的日志中得到的唯一信息如下:

【问题讨论】:

    标签: azure asp.net-core logging azure-web-app-service azure-app-service-envrmnt


    【解决方案1】:

    在您的 Startup.cs -> Configure 方法中,您应该添加以下代码行:

    loggerFactory.AddAzureWebAppDiagnostics();
    loggerFactory.AddConsole();
    

    这是我的 Startup.cs 中 Configure 方法的代码:

            public void Configure(IApplicationBuilder app, IHostingEnvironment env,ILoggerFactory loggerFactory)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseExceptionHandler("/Home/Error");
                    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                    app.UseHsts();
                }
    
                //add the following 2 lines of code.
                loggerFactory.AddAzureWebAppDiagnostics();
                loggerFactory.AddConsole();
    
    
                app.UseHttpsRedirection();
                app.UseStaticFiles();
                app.UseCookiePolicy();
    
                app.UseMvc(routes =>
                {
                    routes.MapRoute(
                        name: "default",
                        template: "{controller=Home}/{action=Index}/{id?}");
                });
            }
    

    发布到天蓝色后->启用应用程序日志记录(文件系统),然后在日志流中,我可以看到日志的输出(我在测试中将日志级别设置为详细):

    【讨论】:

    • 好吧,我已经添加了 AddAzureWebAppDiagnostics,但又将它删除了,因为我在 Visual Studio 中收到了关于它已被弃用和过时的通知。但是在根据您的 awnser 再次添加它之后,它就可以正常工作了......非常信任我的 IDE ;)
    • 我写了一篇关于适用于 .NET 5 的相同解决方案的文章:medium.com/swlh/…
    猜你喜欢
    • 2020-12-26
    • 2022-11-11
    • 2021-12-11
    • 1970-01-01
    • 2021-09-21
    • 2012-03-26
    • 1970-01-01
    • 2020-05-24
    • 2020-06-30
    相关资源
    最近更新 更多