I'd like to have custom logging in my code, which will be hosted in Azure. I've found two extensions for logging providers, between which I am not sure what is the difference.
The two providers that I'm using in my program.cs
:
builder.Logging.AddAzureWebAppDiagnostics();
builder.Logging.AddApplicationInsights();
I tried to comment either and in Azure's logs the trace is still the same. Any idea what is the difference between the two and which one should I stick to?
builder.Logging.AddAzureWebAppDiagnostics();
By using AddAzureWebAppDiagnostics
Method, we can add an Azure Diagnostics logger and can get the diagnostic logs in Azure App Service.
we need to configure file logger options in Program.cs
file.
Install the NuGet Package Microsoft.Extensions.Logging
.
In Program.cs
file, add the below lines of code to configure diagnostic logs.
using Microsoft.Extensions.Logging.AzureAppServices;
builder.Logging.AddAzureWebAppDiagnostics();
builder.Services.Configure<AzureFileLoggerOptions>(options =>
{
options.FileName = "Diagnostics-Logs";
options.FileSizeLimit = 50 * 1024;
options.RetainedFileCountLimit = 3;
});
Azure App service
=> App Service Logs
=> enable Application logging (Filesystem)
.D:\home\LogFiles\Application
path.builder.Logging.AddApplicationInsights();
AddApplicationInsights() method is to configure Application Insights
for Azuer App Service.
Microsoft.Extensions.Logging.ApplicationInsights
.LogLevel.Warning
.In Program.cs
file, add the below code.
using Microsoft.Extensions.Logging.ApplicationInsights;
builder.Logging.AddApplicationInsights();
Controller
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
_logger.LogTrace("Trace Log");
_logger.LogDebug("Debug Log");
_logger.LogInformation("Information Log");
_logger.LogWarning("Warning Log");
_logger.LogError("Error Log");
_logger.LogCritical("Critical Log");
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
Application Insights
References taken from AddAzureWebAppDiagnostics and AddApplicationInsights