Pooled NLogTestLoggerFactory for low-overhead unit testing
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 45/100
調査の方向性
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.
索引モデルが issue の本文から書いたものです。
説明
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:
- Default lighweight NLog MemoryTarget
- Error-Only NLog MemoryTarget that includes ${stacktrace} - MaxLogCount = 1
Maybe also steal some ideas from Microsoft FakeLogger ? (Ex. DumpInitialError() to match FakeLogger-LatestRecord)
- 主要言語
- C#
- スター
- 403
- フォーク
- 149
- 平均マージ
- 11時間 46分
- マージ済み PR(30日)
- 12
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
NLog/NLog.Extensions.Logging のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 57/100
NLog/NLog.Extensions.Logging#875 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
feature up-for-grabs
難易度 3/5 1〜2日 初心者へのやさしさ 35/100
NLog/NLog.Extensions.Logging#729 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
enhancement up-for-grabs
難易度 4/5 3〜5日 初心者へのやさしさ 25/100
NLog/NLog.Extensions.Logging#667 ·
メンテナーはふだん 1 日以内に返信
NLog/NLog.Extensions.Logging の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 67/100
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 76/100
GameShowLDA/AskMkiM#1075 ·
メンテナーはふだん 1 日以内に返信
-
kind/bug triage/untriaged
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
unoplatform/uno.resizetizer#414 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
メンテナーはふだん 1 日以内に返信