InMemoryLogger Class

Provides an in-memory logger implementation that captures log entries for testing purposes.

public class InMemoryLogger : IInMemoryLogger, ILogger

Example

// Create a logger directly
var logger = InMemoryLogger.CreateLogger("MyCategory");
logger.LogInformation("Test message");

// Access captured logs
Assert.Single(logger.Logs.Informations);
Assert.Contains(logger.Logs, log => log.Message == "Test message");
Implements

Properties

NameDescription
LogsGets the collection of log entries captured by this logger.

Gets the collection of log entries captured by this logger.

public InMemoryLogCollection Logs { get; }

Property value

InMemoryLogCollection

Methods

NameDescription
BeginScope<TState>(TState)
CreateLogger(string, InMemoryLogCollection?, IExternalScopeProvider?, TimeProvider?)Creates a new in-memory logger instance with the specified category name.
CreateLogger<T>(InMemoryLogCollection?, IExternalScopeProvider?, TimeProvider?)Creates a new generic in-memory logger instance.
IsEnabled(LogLevel)
Log<TState>(LogLevel, EventId, TState, Exception?, Func<TState, Exception?, string>)

BeginScope<TState>(TState)

InMemoryLogger.cs
public IDisposable? BeginScope<TState>(TState state) where TState : notnull

Type parameters

TState

Parameters

state TState

Returns

IDisposable?

No documentation is available.

CreateLogger overloads

CreateLogger(string, InMemoryLogCollection?, IExternalScopeProvider?, TimeProvider?)

InMemoryLogger.cs

Creates a new in-memory logger instance with the specified category name.

public static IInMemoryLogger CreateLogger(string category, InMemoryLogCollection? logs = null, IExternalScopeProvider? scopeProvider = null, TimeProvider? timeProvider = null)
Parameters
category string
The logger category name.
logs InMemoryLogCollection?
The log collection to use, or null to create a new collection.
scopeProvider IExternalScopeProvider?
The external scope provider to use, or null to create a new instance.
timeProvider TimeProvider?
The time provider to use for timestamping log entries, or null to use the system time provider.
Returns

IInMemoryLogger A new instance of IInMemoryLogger.

CreateLogger<T>(InMemoryLogCollection?, IExternalScopeProvider?, TimeProvider?)

InMemoryLogger.cs

Creates a new generic in-memory logger instance.

public static IInMemoryLogger<T> CreateLogger<T>(InMemoryLogCollection? logs = null, IExternalScopeProvider? scopeProvider = null, TimeProvider? timeProvider = null)
Type parameters
T
The type whose name is used for the logger category name.
Parameters
logs InMemoryLogCollection?
The log collection to use, or null to create a new collection.
scopeProvider IExternalScopeProvider?
The external scope provider to use, or null to create a new instance.
timeProvider TimeProvider?
The time provider to use for timestamping log entries, or null to use the system time provider.
Returns

IInMemoryLogger<T> A new instance of IInMemoryLogger<T>.

IsEnabled(LogLevel)

InMemoryLogger.cs
public bool IsEnabled(LogLevel logLevel)

Parameters

logLevel LogLevel

Returns

bool

No documentation is available.

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

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

Type parameters

TState

Parameters

logLevel LogLevel
eventId EventId
state TState
exception Exception?
formatter Func<TState, Exception?, string>

No documentation is available.