Repository navigation
Expand file tree
/
Copy pathTestLogger.cs
More file actions
84 lines (71 loc) · 3.69 KB
/
Copy pathTestLogger.cs
File metadata and controls
84 lines (71 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using FolderDiffIL4DotNet.Services;
namespace FolderDiffIL4DotNet.Tests.Helpers
{
/// <summary>
/// Shared test logger that captures log entries for assertion in tests.
/// テストでログエントリをキャプチャし、アサーションに使用する共有テストロガー。
/// </summary>
internal sealed class TestLogger : ILoggerService
{
private readonly Action<TestLogEntry>? _onEntry;
private readonly ConcurrentQueue<TestLogEntry> _entries = new();
/// <summary>
/// Creates a TestLogger with default settings (no callback, null log file path).
/// デフォルト設定(コールバックなし、ログファイルパス null)で TestLogger を作成します。
/// </summary>
public TestLogger()
: this(onEntry: null, logFileAbsolutePath: null)
{
}
/// <summary>
/// Creates a TestLogger with an optional callback and log file path.
/// オプションのコールバックとログファイルパスを指定して TestLogger を作成します。
/// </summary>
/// <param name="onEntry">Optional callback invoked for each log entry. / 各ログエントリで呼び出されるオプションのコールバック。</param>
/// <param name="logFileAbsolutePath">Simulated log file path (null by default). / シミュレートするログファイルパス(デフォルトは null)。</param>
public TestLogger(Action<TestLogEntry>? onEntry = null, string? logFileAbsolutePath = null)
{
_onEntry = onEntry;
LogFileAbsolutePath = logFileAbsolutePath;
}
/// <inheritdoc />
public string? LogFileAbsolutePath { get; }
/// <inheritdoc />
public LogFormat Format { get; set; } = LogFormat.Text;
/// <inheritdoc />
public string? TraceId => null;
/// <summary>
/// All captured log entries.
/// キャプチャされた全ログエントリ。
/// </summary>
public IReadOnlyList<TestLogEntry> Entries => _entries.ToArray();
/// <summary>
/// Convenience projection of captured messages (for simple string-based assertions).
/// キャプチャされたメッセージの簡易プロジェクション(文字列ベースのアサーション用)。
/// </summary>
public IEnumerable<string> Messages => _entries.Select(e => e.Message);
/// <inheritdoc />
public void Initialize() { }
/// <inheritdoc />
public void CleanupOldLogFiles(int maxLogGenerations) { }
/// <inheritdoc />
public void LogMessage(AppLogLevel logLevel, string message, bool shouldOutputMessageToConsole, Exception? exception = null)
=> LogMessage(logLevel, message, shouldOutputMessageToConsole, consoleForegroundColor: null, exception);
/// <inheritdoc />
public void LogMessage(AppLogLevel logLevel, string message, bool shouldOutputMessageToConsole, ConsoleColor? consoleForegroundColor, Exception? exception = null)
{
var entry = new TestLogEntry(logLevel, message, shouldOutputMessageToConsole, exception);
_entries.Enqueue(entry);
_onEntry?.Invoke(entry);
}
}
/// <summary>
/// Represents a single log entry captured during testing.
/// テスト中にキャプチャされた単一のログエントリを表す。
/// </summary>
internal sealed record TestLogEntry(AppLogLevel LogLevel, string Message, bool ShouldOutputMessageToConsole, Exception? Exception);
}