Class InMemoryLogger<T>

Namespace
Wolfgang.Extensions.Logging.InMemoryLogger
Assembly
Wolfgang.Extensions.Logging.InMemoryLogger.dll

An implementation of ILogger<TCategoryName> that logs messages to an in-memory collection.

[SuppressMessage("Style", "MA0049:Type name should not match containing namespace", Justification = "The package, namespace, and type intentionally share a name; the namespace is named after the project's primary type.")]
public class InMemoryLogger<T> : ILogger<T>, ILogger

Type Parameters

T

The type whose name is used for the logger category name.

Inheritance
InMemoryLogger<T>
Implements
Inherited Members

Constructors

InMemoryLogger(LogLevel, int)

Initializes a new instance of the InMemoryLogger<T> class.

public InMemoryLogger(LogLevel minLogLevel = LogLevel.Trace, int capacity = 16)

Parameters

minLogLevel LogLevel

The minimum log level to log.

capacity int

The initial capacity of the log entries collection.

Exceptions

ArgumentOutOfRangeException

Thrown when capacity is less than 1.

Properties

Capacity

The capacity of the internal log entries collection.

public int Capacity { get; }

Property Value

int

LogEntries

Returns a readonly list of log entries that have been logged.

public IReadOnlyList<LogEntry<object>> LogEntries { get; }

Property Value

IReadOnlyList<LogEntry<object>>

MinimumLogLevel

Gets the minimum LogLevel for this instance.

public LogLevel MinimumLogLevel { get; }

Property Value

LogLevel

Scopes

Gets the currently active scopes as an array, from outermost to innermost.

public object[] Scopes { get; }

Property Value

object[]

Methods

BeginScope<TState>(TState)

Begins a logical operation scope.

public IDisposable BeginScope<TState>(TState state) where TState : notnull

Parameters

state TState

The identifier for the scope.

Returns

IDisposable

An IDisposable that ends the logical operation scope on dispose.

Type Parameters

TState

The type of the state to begin scope for.

IsEnabled(LogLevel)

Checks if the given logLevel is enabled.

public bool IsEnabled(LogLevel logLevel)

Parameters

logLevel LogLevel

Level to be checked.

Returns

bool

true if enabled.

Log<TState>(LogLevel, EventId, TState, Exception?, Func<TState, Exception?, string>)

Writes a log entry.

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)

Parameters

logLevel LogLevel

Entry will be written on this level.

eventId EventId

Id of the event.

state TState

The entry to be written. Can be also an object.

exception Exception

The exception related to this entry.

formatter Func<TState, Exception, string>

Function to create a string message of the state and exception.

Type Parameters

TState

The type of the object to be written.