Course contentsShow
← C#
Lesson 1797 of 1,90816. Performance and DiagnosticsFree lesson

Filtering Logs at the Source

Learners will configure logging providers to reduce noise before it hits the logging sink.

In this lesson, you will learn how to minimize diagnostic overhead by discarding unnecessary logs directly at the source, preventing them from ever consuming system resources or polluting your telemetry pipeline.

The Cost of Noise

In high-throughput systems, logging is rarely "free." Every log statement incurs a performance penalty related to string interpolation, object allocation, and the I/O cost of pushing data to a sink (like a file, console, or distributed aggregator). When your application generates millions of logs, the CPU cycles spent serializing and transmitting low-priority information become a significant bottleneck.

Filtering at the Source

Rather than relying on your log aggregator to discard logs after they have been processed and shipped, you should configure your ILogger providers to drop messages at the source. This ensures that the application code avoids the overhead of formatting or transmitting messages that will never be used.

In .NET, this is typically handled in your appsettings.json file or via the LoggingBuilder in your Program.cs. By defining a minimum log level for specific namespaces, you effectively turn off the "noise" before the logging framework even begins its work.

Example: Suppressing Noisy Dependencies

Imagine a third-party library that logs every HTTP request at the Information level, overwhelming your logs with mundane diagnostic data. You can filter this at the source:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning",
      "ExternalLibrary.HttpHandler": "Error"
    }
  }
}

By setting ExternalLibrary.HttpHandler to Error, the ILogger implementation checks the level before execution. If the message is only Information, the check returns immediately, bypassing string formatting and memory allocation entirely.

Why This Matters

When you filter at the source, you aren't just saving disk space or cloud storage costs; you are reclaiming CPU cycles and reducing garbage collection pressure that would otherwise be caused by log-related object allocations. It is the most efficient way to keep your diagnostic pipeline focused on meaningful events without incurring the performance cost of processing high-volume, low-value telemetry.

Key Takeaway: Filtering logs at the source prevents unnecessary resource consumption by discarding low-priority events before they are formatted or processed, keeping your application lean under heavy load.