4
votes

I'm trying to configure custom logging in my ASP.NET Core MVC app. The app is hosted on Azure App Service on Linux (free tier). The log files are not appearing, what am I doing wrong?

My configuration:

Following ASP.NET Core logging documentation (https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-3.1#azure-app-service-provider), I've added an Azure App Service logging provider to my app:

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureLogging(logging =>
                {
                    logging.ClearProviders();
                    logging.AddAzureWebAppDiagnostics();
                })
                .ConfigureServices(serviceCollection => serviceCollection
                    .Configure<AzureFileLoggerOptions>(options =>
                    {
                        options.FileName = "azure-diagnostics-";
                        options.FileSizeLimit = 10 * 1024;
                        options.RetainedFileCountLimit = 5;
                    })
                )
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseUrls("http://*:8080");
                    webBuilder.UseStartup<Startup>();
                });

The logging configuration in app.settings:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

I'm logging an info, warning and an error in my controller.

Following the Azure documentation (https://docs.microsoft.com/en-us/azure/app-service/troubleshoot-diagnostic-logs#enable-application-logging-linuxcontainer), I've enabled logging to file system at Azure App -> App Service Logs:

Azure config

My app gets deployed fine and works. I'm expecting to find a log file with names 'azure-diagnostics' somewhere in the file system of the app. I've checked the file system by going to Azure App -> SSH and running:

find . -name '*azure-diagnostics*'

which doesn't return anything. I've also checked the file system using Kudu and VS Cloud Explorer, the files are not there. If I add the console logging provider, the logs appear in the standard Azure log files just fine. What am I missing here?

3

3 Answers

3
votes

I couldn't make it work using AzureWebAppDiagnostics logging provider, but I've managed to solve my problem by using Serilog provider with a file sink.

In program.cs:

public static void Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
                .WriteTo.File(@"../../LogFiles/azure-diagnostics-", fileSizeLimitBytes: 50 * 1024, rollingInterval: RollingInterval.Day, retainedFileCountLimit: 5)
                .CreateLogger();

            try
            {
                CreateHostBuilder(args).Build().Run();
            }
            finally
            {
                Log.CloseAndFlush();
            }
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .UseSerilog()
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseUrls("http://*:8080");
                    webBuilder.UseStartup<Startup>();
                });
    }

UseSeriLog() call replaces the default MS log factory so I could also delete the logging configuration from appsettings.

Now I can see my custom log files appearing in the /home/LogFiles directory as expected.

0
votes

I guess you need to setup the blob for storing the logs as well, can you try setting the file

.ConfigureServices(serviceCollection => serviceCollection
                .Configure<AzureFileLoggerOptions>(options =>
                {
                    options.FileName = "azure-diagnostics-";
                    options.FileSizeLimit = 50 * 1024;
                    options.RetainedFileCountLimit = 5;
                }).Configure<AzureBlobLoggerOptions>(options =>
                {
                    options.BlobName = "log.txt";
                }))

Check SAMPLE app

0
votes

The default location for log files is in the D:\home\LogFiles\Application folder, and the default file name is diagnostics-yyyymmdd.txt.

Add the following provider in the program.cs.

.ConfigureLogging(logging =>
{
    logging.AddAzureWebAppDiagnostics();
    logging.AddConsole();
})

Call logger.LogWarning ("message"); in your code to write to log file. If you use LogWarning be sure to set Level to Warning or more detailed.

Please refer to this issue.