简体   繁体   English

如何查看 Azure 应用服务日志文件?

[英]How can I view the Azure App Service log files?

The default API example in Visual Studio 2019 instantiates an ILogger<T> . Visual Studio 2019 中的默认 API 示例实例化ILogger<T> If I invoke it via _logger.Log(LogLevel.Information, "hello") how can I view the log file?如果我通过_logger.Log(LogLevel.Information, "hello")调用它,我如何查看日志文件? This question assumes use of Azure App Service.此问题假设使用 Azure 应用服务。

namespace WebApplication1.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }
    }
}
  1. You can stream logs live in Cloud Shell, use the following command:您可以在云 Shell 中实时记录 stream 日志,使用以下命令:

    az webapp log tail --name appname --resource-group myResourceGroup az webapp log tail --name appname --resource-group myResourceGroup

  2. Or you can navigate to your app and select Log stream.或者您可以导航到您的应用程序和 select 日志 stream。

在此处输入图像描述

I am sharing the Azure portal steps in Azure App Service and also the code required for it.我正在共享 Azure 应用服务中的 Azure 门户步骤以及它所需的代码。

  1. First of all enable logging via App Service Logs Options首先通过应用服务日志选项启用日志记录在此处输入图像描述

  2. Then check out the live log via 'Log Stream' Option然后通过“日志流”选项查看实时日志在此处输入图像描述

  3. Also sharing my code change for .netcore 3.1还分享了我对 .netcore 3.1 的代码更改

    using Microsoft.Extensions.Configuration;使用 Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting;使用 Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging;使用 Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.EventLog;使用 Microsoft.Extensions.Logging.EventLog;

     namespace MPRC.Common.SAMPLE { public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args).ConfigureLogging((hostingContext, logging) => { logging.ClearProviders(); logging.AddConfiguration(..sample...); logging.AddEventLog(new EventLogSettings() { //........... }); logging.AddConsole(); logging.AddAzureWebAppDiagnostics(); }).ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
  4. Just take a note of logging.AddAzureWebAppDiagnostics();只需记下 logging.AddAzureWebAppDiagnostics(); in above code在上面的代码中

Try to use the methods from namespace System.Diagnostics.Trace to register info about diagnostic尝试使用命名空间 System.Diagnostics.Trace 中的方法来注册有关诊断的信息

eg: System.Diagnostics.Trace.TraceError("If you're seeing this, something bad happened");

By Default, .NET use this provider for logs:默认情况下,.NET 使用此提供程序进行日志:

Microsoft.Extensions.Logging.AzureAppServices.

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

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