Logging

Jiří Urban, Tomáš Juřička, Stanislav Čermák, Karolína Nesrstová, David Długosz, Tomáš Trčka

Introduction to Logging

  • Recording events in an application
  • Important for debugging, auditing, and performance monitoring
  • Types of logging: Text, structured, and event logging

Logging in .NET

  • .NET has a built-in logging framework called Microsoft.Extensions.Logging
  • Framework provides loggers, providers, filters, and formatters
  • Framework is customizable and supports third-party providers such as Serilog and NLog
  • Microsoft.Extensions.Logging.ApplicationInsights package can be added to a .NET 7 WebApi app to provide integration with Azure Application Insights for cloud-based logging and analytics

Logging levels

  • Trace = 0
  • Debug = 1
  • Information = 2
  • Warning = 3
  • Error = 4
  • Critical = 5
  • None = 6

Logging implementation

Logging to Console

Program.cs

 

...

builder.Logging.AddConsole();

...

Logging to console

Logging to file

  • Need of third-party library like Serilog
  • Add following packages to project:
    • Serilog
    • Serilog.AspNetCore
    • Serilog.Sinks.File

Program.cs

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Debug()
    .WriteTo.File("logs/myapp.txt", rollingInterval: RollingInterval.Minute)
    .CreateLogger();

builder.Host.UseSerilog();

Logging to file

Logging to JSON

Program.cs

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Debug()
    .WriteTo.File(new JsonFormatter(), "logs/myapp.json",
        rollingInterval: RollingInterval.Minute)
    .CreateLogger();

builder.Host.UseSerilog();

Logging to JSON

Log search and analytics engines

  • Elastic
  • Splunk
  • Graylog
  • Fluentd
  • Logz.io
  • Papertrail

Log search and analytics engines

Elastic with Kibana

AppInsights

Demo

Made with Slides.com