0

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);
    }
}

S. ten Brinke
  • 2,557
  • 4
  • 25
  • 50
  • I think you want Serilog's sub-logger feature. But having not used it I can't say for certain – pinkfloydx33 Feb 26 '21 at 12:43
  • Does this answer your question? [Filter Serilog logs to different sinks depending on context source?](https://stackoverflow.com/questions/46516359/filter-serilog-logs-to-different-sinks-depending-on-context-source) – pinkfloydx33 Feb 26 '21 at 12:44
  • @pinkfloydx33 Hm, that is something I could attempt, although it seems rather cumbersome. I will give this a try! If someone else encounters this issue and I do not respond if that solution works, give me a reminder to respond :). – S. ten Brinke Feb 26 '21 at 13:39
  • Does the below answer help? – Harshita Singh Mar 02 '21 at 11:41
  • Hello. Thank you for your assistance. I have decided to not continue using Serilog; it's gains were minimal since improved logging in .net 5.0 so I switched to this. It seems so much more complex to set up different logging levels for namespaces for different sinks than for basic .net 5.0 logging. Furthermore, I have used https://blog.steadycoding.com/azure-application-insights-without-noise-from-health-checks-and-swagger/ to prevent App Insights from logging requests that I don't need. – S. ten Brinke Mar 26 '21 at 15:53

1 Answers1

1

Sink configuration methods usually provide a restrictedToMinimumLevel parameter that can be passed. (it cannot enable levels that are disabled at the global Serilog pipeline level, though)

This parameter is also available through the JSON configuration.

More info in the wiki: https://github.com/serilog/serilog/wiki/Configuration-Basics#overriding-per-sink

You can find an example here: https://github.com/tsimbalar/serilog-settings-comparison/blob/master/docs/README.md#sinks---restrictedtominimumlevel

GH issue: https://github.com/serilog/serilog-settings-configuration/issues/135

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Harshita Singh
  • 4,590
  • 1
  • 10
  • 13
  • Hello. Thank you for your assistance. I have decided to not continue using Serilog; it's gains were minimal since improved logging in .net 5.0 so I switched to this. It seems so much more complex to set up different logging levels for namespaces for different sinks than for basic .net 5.0 logging. Furthermore, I have used https://blog.steadycoding.com/azure-application-insights-without-noise-from-health-checks-and-swagger/ to prevent App Insights from logging requests that I don't need. – S. ten Brinke Mar 26 '21 at 15:53