Skip to main content
Configuration object for creating ILogger instances. Use this class to configure sinks, enrichers, filters, and other logger settings before creating a logger.
var logger = new LoggerConfiguration()
    .MinimumLevel.Debug()
    .WriteTo.Console()
    .WriteTo.File("logs/myapp.txt")
    .Enrich.WithProperty("Application", "MyApp")
    .CreateLogger();

Constructor

LoggerConfiguration()

Construct a LoggerConfiguration.
var config = new LoggerConfiguration();

Configuration Properties

WriteTo

Configures the sinks that log events will be emitted to.
type
LoggerSinkConfiguration
Configuration object for adding sinks.
var logger = new LoggerConfiguration()
    .WriteTo.Console()
    .WriteTo.File("logs/app.txt")
    .CreateLogger();

AuditTo

Configures sinks for auditing, instead of regular (safe) logging. When auditing is used, exceptions from sinks and any intermediate filters propagate back to the caller. Most callers should use WriteTo instead.
type
LoggerAuditSinkConfiguration
Configuration object for adding audit sinks.
Not all sinks are compatible with transactional auditing requirements (many will use asynchronous batching to improve write throughput and latency). Sinks need to opt-in to auditing support by extending LoggerAuditSinkConfiguration.

MinimumLevel

Configures the minimum level at which events will be passed to sinks. If not specified, only events at the Information level and above will be passed through.
type
LoggerMinimumLevelConfiguration
Configuration object allowing method chaining.
var logger = new LoggerConfiguration()
    .MinimumLevel.Debug()
    .CreateLogger();

Enrich

Configures enrichment of LogEvents. Enrichers can add, remove and modify the properties associated with events.
type
LoggerEnrichmentConfiguration
Configuration object for adding enrichers.
var logger = new LoggerConfiguration()
    .Enrich.WithProperty("Version", "1.0.0")
    .Enrich.WithMachineName()
    .CreateLogger();

Filter

Configures global filtering of LogEvents.
type
LoggerFilterConfiguration
Configuration object for adding filters.
var logger = new LoggerConfiguration()
    .Filter.ByExcluding(logEvent => logEvent.Level == LogEventLevel.Verbose)
    .CreateLogger();

Destructure

Configures destructuring of message template parameters.
type
LoggerDestructuringConfiguration
Configuration object for configuring destructuring.
var logger = new LoggerConfiguration()
    .Destructure.ToMaximumDepth(4)
    .Destructure.ToMaximumStringLength(100)
    .Destructure.ToMaximumCollectionCount(10)
    .CreateLogger();

ReadFrom

Apply external settings to the logger configuration.
type
LoggerSettingsConfiguration
Configuration object for reading settings from external sources.
var logger = new LoggerConfiguration()
    .ReadFrom.Configuration(configuration)
    .CreateLogger();

Methods

CreateLogger()

Create a logger using the configured sinks, enrichers and minimum level.
returns
Logger
The configured logger instance.
To free resources held by sinks ahead of program shutdown, the returned logger may be cast to IDisposable and disposed.
Throws InvalidOperationException when the logger is already created. CreateLogger() can only be called once per LoggerConfiguration instance.
var config = new LoggerConfiguration()
    .WriteTo.Console();

var logger = config.CreateLogger();

// This will throw InvalidOperationException:
// var logger2 = config.CreateLogger();

Usage Examples

Basic Configuration

var logger = new LoggerConfiguration()
    .MinimumLevel.Information()
    .WriteTo.Console()
    .CreateLogger();

logger.Information("Application started");

Advanced Configuration

var logger = new LoggerConfiguration()
    .MinimumLevel.Debug()
    .MinimumLevel.Override("Microsoft", LogEventLevel.Information)
    .MinimumLevel.Override("System", LogEventLevel.Warning)
    .Enrich.FromLogContext()
    .Enrich.WithMachineName()
    .Enrich.WithProperty("Environment", "Production")
    .WriteTo.Console(
        outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}")
    .WriteTo.File(
        "logs/app-.txt",
        rollingInterval: RollingInterval.Day,
        retainedFileCountLimit: 7)
    .Destructure.ToMaximumDepth(4)
    .Destructure.ToMaximumStringLength(100)
    .CreateLogger();

Using Level Switches

var levelSwitch = new LoggingLevelSwitch();

var logger = new LoggerConfiguration()
    .MinimumLevel.ControlledBy(levelSwitch)
    .WriteTo.Console()
    .CreateLogger();

// Change the minimum level at runtime
levelSwitch.MinimumLevel = LogEventLevel.Warning;

Filtering Events

var logger = new LoggerConfiguration()
    .WriteTo.Console()
    .Filter.ByExcluding(logEvent => 
        logEvent.Properties.ContainsKey("SensitiveData"))
    .Filter.ByIncludingOnly(logEvent => 
        logEvent.Level >= LogEventLevel.Information)
    .CreateLogger();

Build docs developers (and LLMs) love