FileFormatter Class

Allows custom log messages formatting for the FileLoggerProvider.

public abstract class FileFormatter

Example

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

NameDescription
FileFormatter(string)Initializes a new instance of the FileFormatter class.

FileFormatter(string)

FileFormatter.cs

Initializes a new instance of the FileFormatter class.

protected FileFormatter(string name)

Parameters

name string
The name of the formatter.

Properties

NameDescription
NameGets the name of the formatter.

Gets the name of the formatter.

public string Name { get; }

Property value

string

Methods

NameDescription
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.cs

Gets the four-letter representation of the specified log level.

protected static string GetLogLevelString(LogLevel logLevel)

Parameters

logLevel LogLevel
The log level to format.

Returns

string The four-letter representation of logLevel.

Write<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

logEntry LogEntry<TState>
The log entry to write.
timestamp DateTimeOffset
The date and time at which the message was logged, in the timezone configured by FileLoggerOptions.UseUtcTimestamp.
options FileLoggerOptions
The options of the provider that created the log entry.
scopeProvider IExternalScopeProvider?
The provider of scope data, or null when the scopes must not be written.
textWriter TextWriter
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.