Do you use the best trace logging library?

Updated by Brook Jeynes [SSW] 2 years ago. See history

123

Did you know that writing your own logging infrastructure code wastes time? There are awesome logging abstractions in .NET Core and .NET 5+ that you should use instead!

These abstractions allow you to:

  • Create log entries in a predictable and familiar fashion - you use the same patterns for logging in a Background Service as you would in a Blazor WASM app (just some slightly different bootstrapping 😉)
  • Use Dependency Injection; your code doesn't take a dependency on a particular framework (as they are abstractions)
  • Filter output based off severity (Verbose/Debug/Info/Warning/Error) - so you can dial it up or down without changing code
  • Have different logs for different components of your application (e.g. a Customer Log and an Order Log)
  • Multiple logging sinks - where the logs are written to e.g. log file, database, table storage, or Application Insights
  • Supports log message templates allowing logging providers to implement semantic or structured logging
  • Can be used with a range of 3rd party logging providers

Read more at Logging in .NET Core and ASP.NET Core

Image

❌ Figure: Bad example - Using Debug or Trace for logging, or writing hard coded mechanisms for logging does not allow you to configure logging at runtime

Image

❌ Figure: Bad example - Roll your own logging components lack functionality, and have not been tested as thoroughly for quality or performance as log4net

_logger.LogInformation("Getting item {Id} at {RequestTime}", id, DateTime.Now);

✅ Figure: Good example - Using templates allows persisting structured log data (DateTime is a complex object)

Image

✅ Figure: Good example - Seq provides a powerful UI for searching and viewing your structured logs


Acknowledgements

Adam Cogan
Matt Wicks
Brendan Richards
Related rules

Need help?

SSW Consulting has over 30 years of experience developing awesome software solutions.