如何配置.NET Core Azure日志记录

时间:2018-09-06 17:41:50

标签: c# azure asp.net-core

我已经在.NET Core 2.1.3上的Azure中上传了一个非常简单的应用程序。
如下配置App Service日志记录: enter image description here

代码:

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-log,但没有任何效果。

如何在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。我认为这不是正确的解决方案。

1 个答案:

答案 0 :(得分:2)

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

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

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::::