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
appBuilderAppBuilderThe builder of the application that opts in to Serilog.
logNamestringOptional 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.
logPathstringOptional 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.templatestringAn optional Serilog output template applied to the rolling log file.
cultureCultureInfoOptional 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
appBuilderis null.