See my example below, a default ASP.NET Core logging setup where the default loglevel
is "Information" but for ApplicationInsights
the loglevel
for the Microsoft namespace is Warning:
"Logging": {
"LogLevel": {
"Default": "Information"
},
"ApplicationInsights": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning"
}
}
}
This is useful because I don't want to log Health requests to App Insights, for example. Not sending them to App Insights will result in a smaller bill!
Now, I can't really figure out how to do this with Serilog
. I use the console and the app insights sinks. It seems like the minimumLevel
is configured for the entire serilog logger, not on a sink basis.
Edit: I have given up on this matter because it's too complex for my use case!
I have given up on trying to achieve this with Serilog because .net 5.0 logging has improved a lot; I don't need Serilog anymore.
I have achieved my wish to NOT log health requests to App Insights with the following code:
"Logging": {
"LogLevel": {
"Default": "Information"
},
"ApplicationInsights": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information",
"IdentityServer4.AccessTokenValidation": "Warning",
"System.Net.Http.HttpClient": "Warning"
}
},
"Console": {
"FormatterName": "Simple",
"LogLevel": {
"Default": "Information"
},
"FormatterOptions": {
"SingleLine": false,
"TimestampFormat": "[yyyy-MM-ddThh:mm:ss] "
}
}
},
IdentityServer4.AccessTokenValidation
and System.Net.Http.HttpClient
are added because those would log some information that I don't need in App Insights because they are already logged as Request
.
The console will log everything for clarity and deeper debugging if necessary.
Furthermore, to prevent App Insights from still saving the "Health" requests every couple seconds, I have used this blog post from Jim Aho to prevent App Insights from saving certain requests. Please note that I do use some null checks because sometimes the URL can be null depending on the telemetry.
/// <summary>
/// Stops certain requests from being logged to App Insights to prevent noise and save money.
/// </summary>
/// <remarks>
/// Based on https://blog.steadycoding.com/azure-application-insights-without-noise-from-health-checks-and-swagger/
/// </remarks>
public class IgnoreRequestPathsTelemetryProcessor : ITelemetryProcessor
{
private readonly ITelemetryProcessor _next;
private readonly IEnumerable<string> _pathsToIgnore = new[]
{
"/health",
"/swagger"
};
private static readonly StringComparison _stringComparison = StringComparison.InvariantCultureIgnoreCase;
public IgnoreRequestPathsTelemetryProcessor(ITelemetryProcessor next)
{
_next = next;
}
public void Process(ITelemetry item)
{
if (ShouldFilterOutTelemetry(item))
{
return;
}
_next.Process(item);
}
private bool ShouldFilterOutTelemetry(ITelemetry item)
{
return item is RequestTelemetry request && _pathsToIgnore.Any(x => request.Url?.AbsolutePath?.StartsWith(x, _stringComparison) == true);
}
}