FileLoggerProvider Class
A logger provider that writes all log messages to a file on disk.
public sealed class FileLoggerProvider : ILoggerProvider, ISupportExternalScope, IAsyncDisposable, IDisposableExample
using var provider = new FileLoggerProvider(new FileLoggerOptions
{
Directory = "logs",
RollInterval = RollInterval.Daily,
MaxRetainedFiles = 7,
});
using var loggerFactory = LoggerFactory.Create(builder => builder.AddProvider(provider));Constructors
| Name | Description |
|---|---|
| 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.csInitializes a new instance of the FileLoggerProvider class with the specified options.
public FileLoggerProvider(FileLoggerOptions options)Parameters
optionsFileLoggerOptions- The options of the provider.
FileLoggerProvider(FileLoggerOptions, TimeProvider)
FileLoggerProvider.csInitializes a new instance of the FileLoggerProvider class with the specified options.
public FileLoggerProvider(FileLoggerOptions options, TimeProvider timeProvider)Parameters
optionsFileLoggerOptions- The options of the provider.
timeProviderTimeProvider- The time provider used to generate the timestamps.
FileLoggerProvider(IOptionsMonitor<FileLoggerOptions>)
FileLoggerProvider.csInitializes a new instance of the FileLoggerProvider class with the specified options.
public FileLoggerProvider(IOptionsMonitor<FileLoggerOptions> options)Parameters
optionsIOptionsMonitor<FileLoggerOptions>- The options of the provider.
FileLoggerProvider(IOptionsMonitor<FileLoggerOptions>, TimeProvider)
FileLoggerProvider.csInitializes a new instance of the FileLoggerProvider class with the specified options.
public FileLoggerProvider(IOptionsMonitor<FileLoggerOptions> options, TimeProvider timeProvider)Parameters
optionsIOptionsMonitor<FileLoggerOptions>- The options of the provider.
timeProviderTimeProvider- The time provider used to generate the timestamps.
FileLoggerProvider(string)
FileLoggerProvider.csInitializes a new instance of the FileLoggerProvider class that writes to the specified directory.
public FileLoggerProvider(string logsDirectory)Parameters
logsDirectorystring- The directory where log files will be written.
FileLoggerProvider(string, TimeProvider)
FileLoggerProvider.csInitializes a new instance of the FileLoggerProvider class that writes to the specified directory.
public FileLoggerProvider(string logsDirectory, TimeProvider timeProvider)Parameters
logsDirectorystring- The directory where log files will be written.
timeProviderTimeProvider- The time provider used to generate the timestamps.
Properties
| Name | Description |
|---|---|
| LogFilePath | Gets the path of the file the messages are currently written to, or null when the log file could not be created. |
LogFilePath
FileLoggerProvider.csMethods
| Name | Description |
|---|---|
| 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.csDispose()
FileLoggerProvider.csPerforms application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()DisposeAsync()
FileLoggerProvider.csFlushAsync(CancellationToken)
FileLoggerProvider.csWaits 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)SetScopeProvider(IExternalScopeProvider)
FileLoggerProvider.csSets the external scope provider supplied by the logger factory.
public void SetScopeProvider(IExternalScopeProvider scopeProvider)Parameters
scopeProviderIExternalScopeProvider- 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.