Roadmap › 1. .NET › Step 23
Log

Logging & Custom Logging in .NET — ILogger, Serilog, NLog, log4net and Your Own Logger

Why and what to log, log levels, structured vs plain-text logging, Microsoft.Extensions.Logging (ILogger, providers, categories, filtering, scopes, high-performance LoggerMessage source generators), Serilog (sinks, enrichers, request logging), NLog and log4net (including .NET Framework apps), Trace and Windows Event Log, writing a custom logger provider (file and database), audit logging, correlation ids and distributed tracing with OpenTelemetry and Application Insights, log storage and querying (Seq, ELK, Azure Monitor/KQL), security (PII, secrets) and performance.
Quick revision for interview day. Print it or save it as a PDF.

Levels

Trace < Debug < Information < Warning < Error < Critical · production default Information, framework categories Warning.

Do

Message templates with named properties · LogError(ex, ...) with the exception · scopes/enrichers for context · correlation/trace ids · JSON to stdout in containers · central store + retention + alerts.

Don't

Interpolate log messages · log secrets/PII · log the same exception at every layer · log inside tight loops · block requests on log writes.

Libraries

ILogger (abstraction) · Serilog (structured, sinks, enrichers, UseSerilogRequestLogging) · NLog (nlog.config targets/rules) · log4net (legacy XML appenders) · ELMAH (Framework errors) · OpenTelemetry (logs + traces + metrics).

Custom logger

ILoggerProvider + ILogger · bounded Channel queue · background batch writer · never throw · flush on Dispose · builder.Logging.AddProvider(...).