问题
I've updated my project from .net core 2.1 to 2.2 and then logging.AddAzureWebAppDiagnostics()
in Program.cs
no longer works.
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddAzureWebAppDiagnostics();
})
.UseStartup<Startup>()
.Build();
}
'ILoggingBuilder' does not contain a definition for 'AddAzureWebAppDiagnostics' and no accessible extension method 'AddAzureWebAppDiagnostics' accepting a first argument of type 'ILoggingBuilder' could be found (are you missing a using directive or an assembly reference?
Referring to this document,
If targeting .NET Framework or referencing the Microsoft.AspNetCore.App metapackage, add the provider package to the project. Invoke AddAzureWebAppDiagnostics on an ILoggerFactory instance:
So the way might be slightly different from the previous one. How do I fix this issue?
回答1:
The documentation is a bit tricky but if read carefully it become clear that following steps should be undertaken (for NET Core):
Microsoft.Extensions.Logging.AzureAppServices
should be installed- There is NO need to call
logging.AddAzureWebAppDiagnostics();
Logging can be configired using following code
// file startup.cs using Microsoft.Extensions.Logging.AzureAppServices; public class Startup { public void ConfigureServices(IServiceCollection services) { //... services.Configure<AzureFileLoggerOptions>(Configuration.GetSection("AzureLogging")); } }
File
appsettings.json
should contain"AzureLogging": { "FileName" : "azure-diagnostics-", "FileSizeLimit": 50024, "RetainedFileCountLimit": 5 }
Logging should be turned on on Azure Portal. After enabling, Azure Portal may ask for installing addon. Message requiring to install addon will appear on logging config page.
- Call
logger.LogWarning ("message");
in your code to write to log file. If you useLogWarning
be sure to set Level to Warning or more detailed (Info or Debug)
来源:https://stackoverflow.com/questions/54930748/logging-addazurewebappdiagnostics-does-not-work-in-net-core-2-2