Skip to content

Repository files navigation

Wolfgang.Extensions.Logging.InMemoryLogger

An implementation of ILogger and ILogger<T> from Microsoft.Extensions.Logging that writes log entries to an in-memory collection. Designed for unit and integration tests where you need to assert against logged messages.

NuGet NuGet downloads PR build Release License: MIT .NET GitHub


📦 Installation

dotnet add package Wolfgang.Extensions.Logging.InMemoryLogger

NuGet Package: Wolfgang.Extensions.Logging.InMemoryLogger


📄 License

This project is licensed under the MIT License. See the LICENSE file for details.


📚 Documentation


✨ Features

  • Thread-safe in-memory log entry storage.
  • Configurable minimum log level (passed to the constructor; defaults to LogLevel.Trace).
  • Configurable initial capacity (defaults to 16).
  • Supports all .NET log levels (Trace through Critical).
  • Captures scope state via BeginScope for assertion in tests.
  • Returns log entries as IReadOnlyList<LogEntry<object>> for easy LINQ + assertion patterns.
  • Ships both a non-generic InMemoryLogger (constructed with a category string) and a generic InMemoryLogger<T> (category derived from typeof(T)), matching the standard ILogger / ILogger<T> shape.

🚀 Quick Start

using Microsoft.Extensions.Logging;
using Wolfgang.Extensions.Logging.InMemoryLogger;

// Generic form — category is "MyApp.MyService"
var logger = new InMemoryLogger<MyService>(LogLevel.Information);

// Use the logger in code under test
var service = new MyService(logger);
service.DoWork();

// Assert against captured log entries
Assert.Single(logger.LogEntries);
Assert.Equal(LogLevel.Information, logger.LogEntries[0].LogLevel);

The non-generic form takes a category string directly:

var logger = new InMemoryLogger("MyCategory", LogLevel.Warning, capacity: 32);

🎯 Supported Frameworks

This library targets:

  • .NET Framework: 4.6.2
  • .NET Standard: 2.0, 2.1
  • .NET: 8.0, 10.0

See the NuGet package page for the authoritative per-TFM compatibility matrix.

🔍 Code Quality & Static Analysis

This project is held to the canonical analyzer set used across all Chris-Wolfgang .NET libraries. Analyzers run on every build and are treated as errors in Release.

Analyzers in Use

  1. Microsoft.CodeAnalysis.NetAnalyzers — built-in .NET analyzers (correctness and performance)
  2. Roslynator.Analyzers — refactoring and code quality
  3. AsyncFixer — async/await best practices
  4. Microsoft.VisualStudio.Threading.Analyzers — thread safety
  5. Microsoft.CodeAnalysis.BannedApiAnalyzers — enforces the BannedSymbols.txt policy
  6. Meziantou.Analyzer — broad code-quality rules
  7. SonarAnalyzer.CSharp — industry-standard analysis
  8. Microsoft.CodeAnalysis.PublicApiAnalyzers — public-API surface tracking via PublicAPI.Shipped.txt / PublicAPI.Unshipped.txt

Banned-API policy

BannedSymbols.txt is the canonical fleet baseline. Banned categories include blocking sync-over-async (Task.Result, Task.Wait()), Thread.Sleep, synchronous file I/O, and several legacy / deprecated APIs.


🤝 Contributing

Contributions are welcome! Please see CONTRIBUTING.md for:

  • Code quality standards
  • Build and test instructions
  • Pull request guidelines
  • Analyzer configuration details

About

An implementation of ILogger and ILogger <T> that logs results in memory for testing.

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages