ASP.NET Core应用程序日志未写入Azure App Service中的Blob [英] ASP.NET Core Application Logs Not Written To Blob in Azure App Service

查看:110
本文介绍了ASP.NET Core应用程序日志未写入Azure App Service中的Blob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已启用应用程序登录到Azure上的应用程序服务的Blob.

I've enabled application logging to a blob for an app service on Azure.

  • 我可以从Azure门户查看日志流中的日志
  • 我看到每小时都会在我创建的Azure存储帐户中创建一个类似xxxxx-#####.applicationLog.csv的文件,但是该文件实际上并不包含我的应用程序日志
  • 我尝试将Web服务器日志记录启用到同一帐户上的存储中,并且确实有效-我可以在其他文件中看到HTTP请求的日志
  • 我尝试创建一个新的存储帐户并指向日志,但这并没有改变
  • I can view the logs in the log stream from the Azure portal
  • I can see that a file like xxxxx-#####.applicationLog.csv is being created each hour in the Azure storage account I created, but this file doesn't actually contain the my application logs
  • I tried enabling Web Server logging to storage on the same account, and that did work - I could see the logs for HTTP requests in a different file
  • I tried creating a new storage account and pointing to it for the logs, but it didn't change anything

配置详细信息:

  • 该应用使用在.NET Framework 4.6.1上运行的ASP.NET Core 2
  • 我通过以下方式在Program.cs中配置登录:.ConfigureLogging(log => log.AddAzureWebAppDiagnostics())(在.NET Framework而不是.NET Core运行时上运行时,这显然是必需的)
  • The app uses ASP.NET Core 2, running on .NET Framework 4.6.1
  • I configure logging in Program.cs via: .ConfigureLogging(log => log.AddAzureWebAppDiagnostics()) (which is apparently necessary when running on .NET Framework instead of .NET Core runtime)

摘要:当我在Azure门户中进行配置时,不会在Azure存储中创建包含应用程序日志的文件.

In summary: No files containing my application logs are created in Azure Storage when I configure it that way in the Azure portal.

推荐答案

如果要将应用程序日志写入Azure blob存储,首先需要在Azure门户上启用应用程序日志并为其配置blob存储.

If we want to write Application logs to Azure blob storage ,firstly we need to enable Application log and configurate blob storage for it on the Azure portal.

我们可以关注博客来设置Asp.net核心应用中的日志记录.

We could following the blog to set up logging in the Asp.net core app.

在ASP.NET Core应用中设置日志记录不需要太多代码. ASP.NET Core新项目模板已经使用Startup.Configure方法中的以下代码设置了一些基本的日志记录提供程序:

setting up logging in an ASP.NET Core app doesn’t require much code. ASP.NET Core new project templates already setup some basic logging providers with this code in the Startup.Configure method:

loggerFactory.AddConsole(Configuration.GetSection("Logging")); 
loggerFactory.AddDebug();

我也为此做演示.它在我这边正常工作.

I also do demo for it. It works correctly on my side.

1.在Sartup.cs文件中添加以下代码

1.In the Sartup.cs file add the following code

 loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();
            loggerFactory.AddAzureWebAppDiagnostics(
                new AzureAppServicesDiagnosticsSettings
                {
                    OutputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss zzz} [{Level}] {RequestId}-{SourceContext}: {Message}{NewLine}{Exception}",

                }
            );

2.在HomeController.cs中添加以下代码

2.add following code in the HomeController.cs

 private readonly ILogger _logger;
 public HomeController(ILoggerFactory loggerFactory)
 {
     _logger = loggerFactory.CreateLogger<HomeController>();
 }

 public IActionResult Index()
 {
    _logger.LogInformation("Log information");
    _logger.LogError("Logger error");
    return View();
 }

3.访问主页/索引页面,然后从Azure存储Blob中对其进行检查.日志Blob名称的最后一部分.默认为" applicationLog.txt ".我们也可以将其设置为自己.

3.Visit the home/index page and check it from the Azure storage blob. Last section of log blob name. Defaults to "applicationLog.txt". We also could set it ourseleves.

这篇关于ASP.NET Core应用程序日志未写入Azure App Service中的Blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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