Table of Contents

Method UseSerilog

Namespace
Ploch.CommandLine.Spectre.Serilog
Assembly
Ploch.CommandLine.Spectre.Serilog.dll

UseSerilog(AppBuilder, string?, string?, string?, CultureInfo?)

Registers Serilog as the application's logging provider with the predefined command-line configuration.

public static AppBuilder UseSerilog(this AppBuilder appBuilder, string? logName = null, string? logPath = null, string? template = null, CultureInfo? culture = null)

Parameters

appBuilder AppBuilder

The builder of the application that opts in to Serilog.

logName string

Optional name for the log files. If not provided, the current process name is used. This affects both the main log file and the error-specific log file names.

logPath string

Optional directory path where the log files are written. If not provided, the application's base directory is used. Anchor the path to the application (for example Path.Combine(AppContext.BaseDirectory, "logs")); a relative path resolves against the working directory the CLI was launched from.

template string

An optional Serilog output template applied to the rolling log file.

culture CultureInfo

Optional culture used to format values in both log files. When null, the files are written with InvariantCulture, so their contents do not depend on the locale of the machine. Console output is not affected.

Returns

AppBuilder

The same AppBuilder instance, to allow chaining.

Examples

using Ploch.CommandLine.Spectre;
using Ploch.CommandLine.Spectre.Serilog;

using var appBuilder = AppBuilder.Create(args)
                                 .UseSerilog(logName: "my-tool",
                                             logPath: Path.Combine(AppContext.BaseDirectory, "logs"));

// Register your commands with appBuilder.ConfigureCommandApp(...) and run them as usual.

Remarks

Call it once. Every call queues another Serilog registration, as does AddSerilog(IServiceCollection, IConfiguration?, string?, string?, string?, CultureInfo?) on the service collection; an application that already calls services.AddSerilog(context.Configuration, ...) from ConfigureServices does not need this method as well; it only has to reference this package directly, which the core package no longer does on its behalf.

Serilog reads its minimum level and overrides from the Serilog section of the host configuration, that is appsettings.json, environment variables and the command line as Host.CreateDefaultBuilder loads them.

Exceptions

ArgumentNullException

Thrown when appBuilder is null.