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?
ASP.NetServer Side ProgrammingProgramming. Logging is the process of recording events in software as they happen in real-time, along with other information such as the infrastructure details, time taken to execute, etc. Logging is an essential part of any software application.
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;
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With