I'm using Serilog to log to application insights, but I am only getting the log levels warning, error, and critical to be logged. In appsettings.json, I have the minimum loglevel set to verbose, and this logs everything with the file and console sinks.
By default, application insights can only logs Warning and above. In code (in configure logging), I can add a filter to override this default of warning and above. I preferer to do this in appsettings, with the other logging configuration.
How so I use appsettings to allow Serilog to log all levels to Application Insights?
- I am getting some logs in application insights so the connectivity is working.
- I see all loglevels in the logfile and on the console.
Where configuting logging,if I add LogLevel filer (commented out) I can make it work. I needs to work from appsettings.
host.ConfigureLogging(
loggingBuilder =>
{
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
var logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.CreateLogger();
loggingBuilder.AddApplicationInsights();
loggingBuilder.AddSerilog(logger, dispose: true);
//loggingBuilder.AddFilter<ApplicationInsightsLoggerProvider>("", LogLevel.Trace);
}
);
This is my code and the results:
Here is my appsettings:
"Serilog": {
"AllowedHosts": "*",
"Enrich": [ "WithMachineName" ],
"MinimumLevel": {
"Default": "Verbose",
"Override": {
"Microsoft": "Warning",
"System": "Warning"
}
},
"WriteTo": [
{
"Name": "Console"
},
{
"Name": "File",
"Args": {
"fileSizeLimitBytes": "1048576",
"formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact , Version=1.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10",
"path": "c:\\LogFiles\\Publisher\\Test1\\_log.txt",
"retainedFileCountLimit": null,
"rollingInterval": "Day",
"rollOnFileSizeLimit": "true"
}
},
{
"Name": "ApplicationInsights",
"Args": {
"restrictedToMinimumLevel": "Verbose",
"telemetryConverter": "Serilog.Sinks.ApplicationInsights.Sinks.ApplicationInsights.TelemetryConverters.TraceTelemetryConverter, Serilog.Sinks.ApplicationInsights"
}
}
]
},