vidarw
vidarw

Reputation: 81

How to configure Azure logging after upgrading to ASP.NET Core 2.2

Are upgrading from ASP.NET Core 2.1 to ASP.NET Core 2.2 and following the official documentation guide.

Got a problem with writing the new logging configuration in Startup.cs. Specifically problems on how to handle the AzureWebAppDiagnostics.

The old configuration consisted of the following configuration:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    ...
    loggerFactory.AddApplicationInsights(app.ApplicationServices);
    loggerFactory.AddAzureWebAppDiagnostics(
        new AzureAppServicesDiagnosticsSettings
        {
            OutputTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss zzz} [{Level}] {RequestId}-{SourceContext}: {Message}{NewLine}{Exception}"
        }
    );
}

Both the AddAzureWebAppDiagnostics and the AzureAppServicesDiganosticsSettings is marked as obsolete. The later suggests to use AzureBlobLoggerOptions instead. The guide states that the logging config should be moved to something like this:

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddLogging(builder => builder
        .AddConsole()
        .AddAzureWebAppDiagnostics());
    ...
}

However I have no clue on how to properly add the configuration to the ILoggingBuildler AND as an extra bonus, the AzureBlobLoggerOptions does no allow for a custom OutputTemplate. The AddApplicationInsights is also missing from the ILoggingBuilder.

Any suggestions on how to get this working like it used to be?

Upvotes: 8

Views: 4235

Answers (3)

BiggusNickus
BiggusNickus

Reputation: 31

In 2.2 I got this working by splitting the configuration between Program.cs and Startup.cs:

Add this to CreateWebHostBuilder in Program.cs:

  .ConfigureLogging((hostingContext, logging) =>
    {
        logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
        logging.AddConsole();
        logging.AddDebug();
        logging.AddEventSourceLogger();
        logging.AddAzureWebAppDiagnostics();
    })

Add this to ConfigureServices in Startup.cs:

services.Configure<AzureFileLoggerOptions>(options =>
{
options.FileName = "azure-diagnostics-";
options.FileSizeLimit = 50 * 1024;
options.RetainedFileCountLimit = 5;
});

This was enough for me to start seeing messages in the Azure App Service log; you need the Nuget package Microsoft.Extensions.Logging.AzureAppService and corresponding using statements also:

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Logging.AzureAppServices;

Upvotes: 3

tdykstra
tdykstra

Reputation: 6050

The custom OutputTemplate required a dependency on Serilog, which was removed for 2.2, so it's not in the file or blob options classes.

For 2.2 it's recommended to configure logging in Program.cs rather than Startup.cs; here's an example using AddAzureWebAppDiagnostics and the options classes:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .ConfigureLogging(logging => logging.AddAzureWebAppDiagnostics())
        .ConfigureServices(serviceCollection => serviceCollection
                .Configure<AzureFileLoggerOptions>(options => {
                    options.FileName = "azure-diagnostics-";
                    options.FileSizeLimit = 50 * 1024;
                    options.RetainedFileCountLimit = 5;
                }).Configure<AzureBlobLoggerOptions>(options => {
                    options.BlobName = "log.txt";
                }))
        .UseStartup<Startup>();

This requires the NuGet package Microsoft.Extensions.Logging.AzureAppServices and the following using statements:

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging.AzureAppServices;
using Microsoft.Extensions.Logging;

Upvotes: 1

user1496062
user1496062

Reputation: 1317

Insights is a bit slow to the party. There is a new package with support for the logging builder eg

<PackageReference Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.9.0-beta3" />

Without that you will need to use the Obsolete way.

Upvotes: 2

Related Questions