InMemoryLogCollection Class

Represents a thread-safe collection of log entries captured by in-memory loggers.

public sealed class InMemoryLogCollection : IEnumerable<InMemoryLogEntry>, IEnumerable

Example

var logger = InMemoryLogger.CreateLogger("sample");
logger.LogInformation("Application started");
logger.LogError("Error while processing the request");

// Filter by log level
var errors = logger.Logs.Errors;
var infos = logger.Logs.Informations;
Assert.Single(errors);
Assert.Single(infos);

// Search for specific entries
var entry = logger.Logs.Find(log => log.Message.StartsWith("Error", StringComparison.Ordinal));
Assert.NotNull(entry);
Assert.Equal(LogLevel.Error, entry.LogLevel);
Implements

Nested types

NameDescription
InMemoryLogCollection.EnumeratorEnumerates the elements of a InMemoryLogCollection.

Constructors

NameDescription
InMemoryLogCollection()

InMemoryLogCollection()

InMemoryLogCollection.cs
public InMemoryLogCollection()

No documentation is available.

Properties

NameDescription
CountGets the number of log entries in the collection.
CriticalsGets all log entries with log level LogLevel.Critical.
DebugsGets all log entries with log level LogLevel.Debug.
ErrorsGets all log entries with log level LogLevel.Error.
InformationsGets all log entries with log level LogLevel.Information.
TracesGets all log entries with log level LogLevel.Trace.
WarningsGets all log entries with log level LogLevel.Warning.

Gets the number of log entries in the collection.

public int Count { get; }

Property value

int

Remarks

The count is incremented only after the entry is reachable, so a concurrent reader never sees a count that promises more entries than an enumeration would yield.

Gets all log entries with log level LogLevel.Critical.

public IEnumerable<InMemoryLogEntry> Criticals { get; }

Gets all log entries with log level LogLevel.Debug.

public IEnumerable<InMemoryLogEntry> Debugs { get; }

Gets all log entries with log level LogLevel.Error.

public IEnumerable<InMemoryLogEntry> Errors { get; }

Gets all log entries with log level LogLevel.Information.

public IEnumerable<InMemoryLogEntry> Informations { get; }

Gets all log entries with log level LogLevel.Trace.

public IEnumerable<InMemoryLogEntry> Traces { get; }

Gets all log entries with log level LogLevel.Warning.

public IEnumerable<InMemoryLogEntry> Warnings { get; }

Methods

NameDescription
Clear()Removes all log entries from the collection.
Contains(Func<InMemoryLogEntry, bool>)Determines whether the collection contains any log entry that matches the specified predicate.
Find(Func<InMemoryLogEntry, bool>)Searches for the first log entry that matches the specified predicate.
GetEnumerator()
System.Collections.Generic.IEnumerable<Meziantou.Extensions.Logging.InMemory.InMemoryLogEntry>.GetEnumerator()
System.Collections.IEnumerable.GetEnumerator()
ToString()

Removes all log entries from the collection.

public void Clear()

Remarks

An enumeration already in progress keeps walking the entries it started on; it is not invalidated.

Contains(Func<InMemoryLogEntry, bool>)

InMemoryLogCollection.cs

Determines whether the collection contains any log entry that matches the specified predicate.

public bool Contains(Func<InMemoryLogEntry, bool> predicate)

Parameters

predicate Func<InMemoryLogEntry, bool>
The function to test each log entry for a condition.

Returns

bool true if any log entry matches the predicate; otherwise, false.

Find(Func<InMemoryLogEntry, bool>)

InMemoryLogCollection.cs

Searches for the first log entry that matches the specified predicate.

public InMemoryLogEntry? Find(Func<InMemoryLogEntry, bool> predicate)

Parameters

predicate Func<InMemoryLogEntry, bool>
The function to test each log entry for a condition.

Returns

InMemoryLogEntry? The first log entry that matches the predicate, or null if no match is found.

public InMemoryLogCollection.Enumerator GetEnumerator()

No documentation is available.

System.Collections.Generic.IEnumerable<Meziantou.Extensions.Logging.InMemory.InMemoryLogEntry>.GetEnumerator()

InMemoryLogCollection.cs

No documentation is available.

System.Collections.IEnumerable.GetEnumerator()

InMemoryLogCollection.cs
IEnumerator IEnumerable.GetEnumerator()

No documentation is available.

public override string ToString()

Returns

string

No documentation is available.