首页 > 解决方案 > 如何配置 .NET Core Azure 日志记录

问题描述

我在 .NET Core 2.1.3 上的 Azure 中上传了一个非常简单的应用程序。
如下配置应用服务日志记录: 在此处输入图像描述

编码:

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

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

public class Startup
{
    private readonly ILogger<Startup> logger;
    public Startup(IConfiguration configuration, ILogger<Startup> logger)
    {
        this.Configuration = configuration;
        this.logger = logger;
    }

    public IConfiguration Configuration { get; }


    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(IServiceCollection services)
    {
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        // Commented lines doesn't affect ...              
        //loggerFactory.AddConsole(this.Configuration.GetSection("Logging"));
        // loggerFactory.AddDebug();
        Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

        loggerFactory.AddAzureWebAppDiagnostics();

        this.logger.LogInformation("Loading::::::::::::::::::");


        app.Run(async (context) =>
        {
            this.logger.LogInformation("OnRequest::::::::::::::::::");
            await context.Response.WriteAsync("Loading...");
        });
    }
}

问题是日志记录在本地工作,但在天蓝色没有。如果我打开/Log Files/Application/afg4322-201809062011.log我的消息OnRequest::::::::::::::::::Loading::::::::::::::::::没有出现在那里。目前,该逻辑捕获所有请求并简单地写入一条日志消息。
我还安装Microsoft.Extensions.Logging.AzureAppServices并解决了问题https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/azure-apps/troubleshoot?view=aspnetcore-2.1#aspnet-core-module-stdout -记录没有任何工作。

如何在 Azure 应用中记录消息?也许我错过了一些简单的设置?

applicationSettings.json: _

{
  "Logging": {
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    },
    "Console": {
      "IncludeScopes": "true"
    }
  }
}

我查看了这篇文章 - Asp.net Core azure web app logging - How to enable Application Logs in Azure for Net Core 2 App?

我试图避免来自Ilya Chernomordik他所说的设置<aspNetCore stdoutLogEnabled="true" />和修改的建议SourceSwitch。我认为这不是正确的解决方案。

标签: c#azureasp.net-core

解决方案


A找到了解决办法。
该应用程序以Release模式发布 - 所以我添加了一个appsettings.Production.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Trace",
      "System": "Information",
      "Microsoft": "Information"
    }
  }
}

并将我的代码更改如下:

var l = loggerFactory.CreateLogger<Startup>();
l.LogInformation("OnRequest:::::::::Info:::::::::");
l.LogDebug("OnRequest:::::::::Debug:::::::::");
l.LogError("OnRequest::::::::::::::Error::::");
l.LogWarning("OnRequest::::::::::::Warning::::::");
l.LogTrace("OnRequest:::::::::::::Trace:::::");
l.LogCritical("OnRequest::::::::::::::Critical::::");
await context.Response.WriteAsync("Loading...");

而是使用该字段logger,我从loggerFactory.

Streaming Logs现在在 Azure 门户中接收所有消息:

2018-09-06 19:10:01.449 +00:00 [Information] WebApp.Startup: OnRequest:::::::::Info::::::::: 2018-09-06 19:10:01.449 +00:00 [Debug] WebApp.Startup: OnRequest:::::::::Debug::::::::: 2018-09-06 19:10:01.449 +00:00 [Error] WebApp.Startup: OnRequest::::::::::::::Error:::: 2018-09-06 19:10:01.449 +00:00 [Warning] WebApp.Startup: OnRequest::::::::::::Warning:::::: 2018-09-06 19:10:01.449 +00:00 [Trace] WebApp.Startup: OnRequest:::::::::::::Trace::::: 2018-09-06 19:10:01.449 +00:00 [Critical] WebApp.Startup: OnRequest::::::::::::::Critical::::


推荐阅读