Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Pooled NLogTestLoggerFactory for low-overhead unit testing

Aperta
#865 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
45/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
csharp
Ambito
testing, tooling

Direzione di ricerca

The issue is about adding a pooled NLogTestLoggerFactory to NLog.Extensions.Logging. Start by examining the existing NLog.Extensions.Logging source, particularly the provider and factory classes. Look at how logging rules and MemoryTargets are configured. Understand the Microsoft.Extensions.Logging.ILogger integration and the existing test infrastructure. 'Done' means the factory can be rented from a pool, captures logs to a MemoryTarget, and cleans up safely, with appropriate exception handling and optional debugger support.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

feature

Add an NLogTestLoggerFactory to NLog.Extensions.Logging that makes it easy for unit tests to use a real Microsoft.Extensions.Logging.ILogger while capturing logs through an NLog MemoryTarget.

The primary goal is to make logging useful as test diagnostics, without encouraging tests to assert on log messages, properties, parameters, or exceptions as a way of verifying application behavior.

using var logging = NLogTestLoggerFactory.Rent();

var sut = new MyService(
    repository,
    logging.CreateLogger<MyService>());

await sut.DoSomething();

Assert.Equal(expected, result);
Assert.Empty(logging.DumpOnError());

The NLogTestLoggerFactory allocates an NLog MemoryTarget that captures all logevents (Bounded ringbuffer can be activated).

The NLogTestLoggerFactory.Rent() will allow unit-tests to execute faster without having the overhead from creating NLog LogFactory / NLog MemoryTarget-Buffer / etc. on every unit-test.

When NLogTestLoggerFactory is returned to pool, then the LoggingRules are cleared, and when calling CreateLogger a dedicated logging-rule is added with atomic-increment (matching only exactly matching that unique-logger-name). This is to protect against rogue background-tasks that continue logging after the unit-test have completed and returned to pool.

Guess the default NLogTestLoggerFactory.Rent() method will return instance-created-by-default-factory. If wanting to setup NLog LogFactory with different configuration, then one should inherit from NLogTestLoggerFactory and create new static Rent()-method that calls NLogTestLoggerFactory.Rent().

The NLogTestLoggerFactory Dispose will by default throw on exception, unless an exception is already in the wind:

private static bool IsExceptionUnwinding()
{
    return Marshal.GetExceptionPointers() != IntPtr.Zero
        || Marshal.GetExceptionCode() != 0;
}

Should also have AddNLogTest() that by default removes all existing logger-providers, and by default uses NLogTestLoggerProvider.Rent(), where NLogTestLoggerProvider is allowed to throw on Dispose when exception/error logged.

Maybe the exception should be called: NLogUnexpectedErrorException

Maybe the NLogTestLoggerFactory should also support checking Debugger.IsAttached, and then setup DebuggerTarget with minLevel = Error (default LogLevel that can be adjusted). Maybe something like this Rent(bool throwOnError = false, LogLevel? debuggerAttachedLevel = null) should control whether to throw exceptions on dispose, or whether the caller handles inspection/checking for errors. And control if wanting to override the LogLevel for output-window when debugger-attached.

There should probably be two NLog MemoryTargets:

  1. Default lighweight NLog MemoryTarget
  2. Error-Only NLog MemoryTarget that includes ${stacktrace} - MaxLogCount = 1

Maybe also steal some ideas from Microsoft FakeLogger ? (Ex. DumpInitialError() to match FakeLogger-LatestRecord)

Lingua principale
C#
Stelle
403
Fork
149
Merge medio
11h 46m
PR unite (30g)
12

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di NLog/NLog.Extensions.Logging

Tutte le issue di NLog/NLog.Extensions.Logging

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.