FileFormatter Class
Allows custom log messages formatting for the FileLoggerProvider.
public abstract class FileFormatterExample
internal sealed class CsvFileFormatter() : FileFormatter("csv")
{
public override void Write<TState>(in LogEntry<TState> logEntry, DateTimeOffset timestamp, FileLoggerOptions options, IExternalScopeProvider? scopeProvider, TextWriter textWriter)
{
textWriter.Write(logEntry.LogLevel);
textWriter.Write(',');
textWriter.Write(logEntry.Formatter(logEntry.State, logEntry.Exception));
}
}Constructors
| Name | Description |
|---|---|
| FileFormatter(string) | Initializes a new instance of the FileFormatter class. |
FileFormatter(string)
FileFormatter.csInitializes a new instance of the FileFormatter class.
protected FileFormatter(string name)Parameters
namestring- The name of the formatter.
Properties
| Name | Description |
|---|---|
| Name | Gets the name of the formatter. |
Name
FileFormatter.csMethods
| Name | Description |
|---|---|
| GetLogLevelString(LogLevel) | Gets the four-letter representation of the specified log level. |
| Write<TState>(LogEntry<TState>, DateTimeOffset, FileLoggerOptions, IExternalScopeProvider?, TextWriter) | Writes the log message to the specified TextWriter. The line terminator is written by the provider. |
GetLogLevelString(LogLevel)
FileFormatter.csWrite<TState>(LogEntry<TState>, DateTimeOffset, FileLoggerOptions, IExternalScopeProvider?, TextWriter)
Writes the log message to the specified TextWriter. The line terminator is written by the provider.
public abstract void Write<TState>(in LogEntry<TState> logEntry, DateTimeOffset timestamp, FileLoggerOptions options, IExternalScopeProvider? scopeProvider, TextWriter textWriter)Type parameters
TState- The type of the log entry state.
Parameters
logEntryLogEntry<TState>- The log entry to write.
timestampDateTimeOffset- The date and time at which the message was logged, in the timezone configured by
FileLoggerOptions.UseUtcTimestamp. optionsFileLoggerOptions- The options of the provider that created the log entry.
scopeProviderIExternalScopeProvider?- The provider of scope data, or
nullwhen the scopes must not be written. textWriterTextWriter- The writer the message must be written to.
Remarks
The method is called on the thread that logs the message, so the ambient state such as Activity.Current can be read.