2
votes

I have uploaded a very simple app in Azure on .NET Core 2.1.3.
Configured the App Service logging as follows: enter image description here

the code:

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...");
        });
    }
}

The problem is that the logging works locally, but no in azure. If I open /Log Files/Application/afg4322-201809062011.log my messages OnRequest:::::::::::::::::: and Loading:::::::::::::::::: didn't appear there. Currently the logic catches all requests and simply writes a log message.
Also I've installed Microsoft.Extensions.Logging.AzureAppServices and troubleshot https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/azure-apps/troubleshoot?view=aspnetcore-2.1#aspnet-core-module-stdout-log nothing working.

How to log a message in Azure app ? Maybe I'm missing some simple setting?

The applicationSettings.json:

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

I've checked this articles - Asp.net Core azure web app logging - How to enable Application Logs in Azure for Net Core 2 App?

I'm trying to avoid suggestion from Ilya Chernomordik where he says to set <aspNetCore stdoutLogEnabled="true" /> and modify SourceSwitch. I think it's not the correct solution.

1

1 Answers

3
votes

A found a solution.
The app was published in Release Mode - so I've added an appsettings.Production.json:

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

And changed my code as follows:

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...");

Instead using the field logger, I get a logger from loggerFactory.

Now a receive all messages in Streaming Logs in Azure portal:

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