Method ConfigureSerilog
- Namespace
- Ploch.CommandLine.Spectre.Serilog
- Assembly
- Ploch.CommandLine.Spectre.Serilog.dll
ConfigureSerilog(LoggerConfiguration, IConfiguration?, string?, string?, string?, CultureInfo?)
Configures a Serilog logger with comprehensive settings for command-line applications.
public static LoggerConfiguration ConfigureSerilog(this LoggerConfiguration loggerConfiguration, IConfiguration? configuration = null, string? template = null, string? logName = null, string? logPath = null, CultureInfo? culture = null)
Parameters
loggerConfigurationLoggerConfigurationThe Serilog logger configuration instance to configure.
configurationIConfigurationOptional configuration instance to read Serilog settings from. If provided, settings from the "Serilog:MinimumLevel:Default" section will be used to determine the minimum log level. Additional configuration will be applied after the predefined setup.
templatestringOptional custom output template for log formatting. If not provided, the default template will be used which includes timestamp, log level, message, and exception information.
logNamestringOptional name for the log files. If not provided, the current process name will be used. This affects both the main log file and the error-specific log file names.
logPathstringOptional directory path where log files will be stored. If not provided, the application's base directory will be used. Both main and error log files will be created in this directory.
cultureCultureInfoOptional culture used to format values in both file sinks (the main log and the error log). When null, InvariantCulture is used, so numbers, dates and other formatted values are written identically whatever the locale of the machine running the application. Pass CurrentCulture to write locale-formatted values instead. Console output from the Spectre.Console sink is not affected by this setting.
Returns
- LoggerConfiguration
The configured Serilog.LoggerConfiguration instance for method chaining.
Examples
// Basic configuration
var logger = new LoggerConfiguration()
.ConfigureSerilog()
.CreateLogger();
// With custom settings
var logger = new LoggerConfiguration()
.ConfigureSerilog(
configuration: config,
template: "{Timestamp:HH:mm:ss} [{Level}] {Message}{NewLine}",
logName: "Ploch.MyApp",
logPath: @"C:\Logs"
)
.CreateLogger();
// Locale-formatted values in the log files instead of the invariant default
var logger = new LoggerConfiguration()
.ConfigureSerilog(culture: CultureInfo.CurrentCulture)
.CreateLogger();
Remarks
This method sets up a comprehensive logging configuration that includes:
- Context and thread enrichment for better log correlation
- File logging with 2MB size limit and automatic rotation
- Separate error log file for warnings, errors, and fatal messages
-
Console output through the Spectre.Console sink, the only console sink the predefined setup
registers (settings read from
configurationcan add more) - Configurable minimum log level from configuration
- File retention policy to prevent disk space issues
- Culture-invariant formatting in both log files by default, because log files are read by tools and aggregated across machines with different locales
The configuration process first applies the predefined settings, then reads additional
configuration from the provided configuration if available. This allows
for both consistent defaults and flexible customisation.