Logging for C# (.NET): Best Solutions and Libraries

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Logging for C# (.NET): Overview of the Best Solutions



Logging is a critically important aspect of developing any serious application in C#. It allows you to track system state, diagnose errors, analyze user behavior, and collect performance metrics. In the .NET ecosystem, there are several mature logging libraries, each with its own strengths. In this article, we will look at the three most popular solutions: Serilog, NLog, and log4net, as well as the standard abstraction Microsoft.Extensions.Logging.



Why do you need a separate logging library?



The built-in System.Diagnostics.Debug class or Console.WriteLine is only suitable for the simplest scenarios. Professional libraries provide:

  • Log levels (Trace, Debug, Information, Warning, Error, Fatal) — flexible message filtering.
  • Structured logging — saving data in JSON format with the ability for subsequent analysis.
  • Multiple sinks/targets — writing to a file, database, console, ELK stack, Sentry, Application Insights, etc.
  • Dynamic configuration — changing the log level without restarting the application.
  • Performance — asynchronous writing, buffering, minimal impact on execution speed.


1. Serilog — The Modern Standard



Serilog is the most popular library for .NET, which has bet on structured logging. Instead of formatting strings with string.Format, you use named placeholders:



using Serilog;

Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .WriteTo.Console() .WriteTo.File("logs/app-.log", rollingInterval: RollingInterval.Day) .WriteTo.ApplicationInsights(TelemetryConfiguration.Active, TelemetryConverter.Traces) .CreateLogger();

// Example of structured loggingLog.Information("User {UserId} completed order {OrderId} for {Total:C}", userId, orderId, total);


Serilog is ideal for microservices, cloud applications, and projects where deep log analysis via Kibana or Grafana is important.



2. NLog — Flexibility and Performance



NLog is a powerful library with rich routing capabilities. It supports both XML configuration and programmatic setup. A feature of NLog is its built-in asynchronous writing mechanism and support for conditional rules.



using NLog;

private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

public void ProcessOrder(Order order){ Logger.Info("Starting order processing {OrderId}", order.Id); try { // business logic Logger.Debug("Order {OrderId} successfully processed", order.Id); } catch (Exception ex) { Logger.Error(ex, "Error processing order {OrderId}", order.Id); throw; }}


NLog is often chosen for desktop applications (WPF, WinForms) and legacy projects due to the ease of migration from log4net.



3. log4net — The Proven Veteran



log4net is a port of the popular Java library log4j. It is the "oldest" solution, still actively used in enterprise projects. The main advantage is the huge number of adapters and appenders written over the decades.



// Configuration via XML (log4net.config)<log4net>    <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">        <file value="logs/app.log" />        <rollingStyle value="Date" />        <datePattern value="yyyyMMdd" />                    

Recommendations