FileLoggerProvider Class

A logger provider that writes all log messages to a file on disk.

public sealed class FileLoggerProvider : ILoggerProvider, ISupportExternalScope, IAsyncDisposable, IDisposable

Example

using var provider = new FileLoggerProvider(new FileLoggerOptions
{
    Directory = "logs",
    RollInterval = RollInterval.Daily,
    MaxRetainedFiles = 7,
});

using var loggerFactory = LoggerFactory.Create(builder => builder.AddProvider(provider));
Implements

Constructors

NameDescription
FileLoggerProvider(FileLoggerOptions)Initializes a new instance of the FileLoggerProvider class with the specified options.
FileLoggerProvider(FileLoggerOptions, TimeProvider)Initializes a new instance of the FileLoggerProvider class with the specified options.
FileLoggerProvider(IOptionsMonitor<FileLoggerOptions>)Initializes a new instance of the FileLoggerProvider class with the specified options.
FileLoggerProvider(IOptionsMonitor<FileLoggerOptions>, TimeProvider)Initializes a new instance of the FileLoggerProvider class with the specified options.
FileLoggerProvider(string)Initializes a new instance of the FileLoggerProvider class that writes to the specified directory.
FileLoggerProvider(string, TimeProvider)Initializes a new instance of the FileLoggerProvider class that writes to the specified directory.

FileLoggerProvider overloads

FileLoggerProvider(FileLoggerOptions)

FileLoggerProvider.cs

Initializes a new instance of the FileLoggerProvider class with the specified options.

public FileLoggerProvider(FileLoggerOptions options)
Parameters
options FileLoggerOptions
The options of the provider.

FileLoggerProvider(FileLoggerOptions, TimeProvider)

FileLoggerProvider.cs

Initializes a new instance of the FileLoggerProvider class with the specified options.

public FileLoggerProvider(FileLoggerOptions options, TimeProvider timeProvider)
Parameters
options FileLoggerOptions
The options of the provider.
timeProvider TimeProvider
The time provider used to generate the timestamps.

FileLoggerProvider(IOptionsMonitor<FileLoggerOptions>)

FileLoggerProvider.cs

Initializes a new instance of the FileLoggerProvider class with the specified options.

public FileLoggerProvider(IOptionsMonitor<FileLoggerOptions> options)
Parameters
options IOptionsMonitor<FileLoggerOptions>
The options of the provider.

FileLoggerProvider(IOptionsMonitor<FileLoggerOptions>, TimeProvider)

FileLoggerProvider.cs

Initializes a new instance of the FileLoggerProvider class with the specified options.

public FileLoggerProvider(IOptionsMonitor<FileLoggerOptions> options, TimeProvider timeProvider)
Parameters
options IOptionsMonitor<FileLoggerOptions>
The options of the provider.
timeProvider TimeProvider
The time provider used to generate the timestamps.

FileLoggerProvider(string)

FileLoggerProvider.cs

Initializes a new instance of the FileLoggerProvider class that writes to the specified directory.

public FileLoggerProvider(string logsDirectory)
Parameters
logsDirectory string
The directory where log files will be written.

FileLoggerProvider(string, TimeProvider)

FileLoggerProvider.cs

Initializes a new instance of the FileLoggerProvider class that writes to the specified directory.

public FileLoggerProvider(string logsDirectory, TimeProvider timeProvider)
Parameters
logsDirectory string
The directory where log files will be written.
timeProvider TimeProvider
The time provider used to generate the timestamps.

Properties

NameDescription
LogFilePathGets the path of the file the messages are currently written to, or null when the log file could not be created.

Gets the path of the file the messages are currently written to, or null when the log file could not be created.

public string? LogFilePath { get; }

Property value

string?

Remarks

The value changes when the log file is rolled.

Methods

NameDescription
CreateLogger(string)Creates a new ILogger instance.
Dispose()Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
DisposeAsync()Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.
FlushAsync(CancellationToken)Waits for the pending messages to be written to the log file.
SetScopeProvider(IExternalScopeProvider)Sets the external scope provider supplied by the logger factory.

CreateLogger(string)

FileLoggerProvider.cs

Creates a new ILogger instance.

public ILogger CreateLogger(string categoryName)

Parameters

categoryName string
The category name for messages produced by the logger.

Returns

ILogger The instance of ILogger that was created.

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

DisposeAsync()

FileLoggerProvider.cs

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.

net10.0

public ValueTask DisposeAsync()

net11.0

public ValueTask DisposeAsync()

Returns

ValueTask A task that represents the asynchronous dispose operation.

FlushAsync(CancellationToken)

FileLoggerProvider.cs

Waits for the pending messages to be written to the log file.

net10.0

public Task FlushAsync(CancellationToken cancellationToken = default)

net11.0

public Task FlushAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken
A token to cancel the operation.

Returns

Task

SetScopeProvider(IExternalScopeProvider)

FileLoggerProvider.cs

Sets the external scope provider supplied by the logger factory.

public void SetScopeProvider(IExternalScopeProvider scopeProvider)

Parameters

scopeProvider IExternalScopeProvider
The scope provider the factory uses to track scopes.

Remarks

Implementing ISupportExternalScope is what makes the factory route its scopes through this provider, including the ones it synthesises from the current activity when LoggerFactoryOptions.ActivityTrackingOptions is set.