-
Notifications
You must be signed in to change notification settings - Fork 767
Expand file tree
/
Copy pathScenarioLogCapture.cs
More file actions
166 lines (148 loc) · 4.24 KB
/
ScenarioLogCapture.cs
File metadata and controls
166 lines (148 loc) · 4.24 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#nullable enable
using System.Text;
using Microsoft.Extensions.Logging;
namespace UICatalog;
/// <summary>
/// An <see cref="ILoggerProvider"/> that captures log output in-memory for scenario debugging.
/// Supports marking a position when a scenario starts and retrieving logs since that point.
/// </summary>
public class ScenarioLogCapture : ILoggerProvider
{
private readonly object _lock = new ();
private readonly StringBuilder _buffer = new ();
private int _scenarioStartPosition;
public int ScenarioStartPosition
{
get
{
lock (_lock)
{
return _scenarioStartPosition;
}
}
private set => _scenarioStartPosition = value;
}
private bool _hasErrors;
/// <summary>
/// Gets whether any Error-level or higher logs have been recorded since the last <see cref="MarkScenarioStart"/> call.
/// </summary>
public bool HasErrors
{
get
{
lock (_lock)
{
return _hasErrors;
}
}
set => _hasErrors = value;
}
/// <summary>
/// Marks the current buffer position as the start of a scenario.
/// Call this before running a scenario to capture only that scenario's logs.
/// </summary>
public void MarkScenarioStart ()
{
lock (_lock)
{
_scenarioStartPosition = _buffer.Length;
_hasErrors = false;
}
}
/// <summary>
/// Gets all log entries since the last <see cref="MarkScenarioStart"/> call.
/// </summary>
/// <returns>The log content for the current scenario.</returns>
public string GetScenarioLogs ()
{
lock (_lock)
{
if (_scenarioStartPosition >= _buffer.Length)
{
return string.Empty;
}
return _buffer.ToString (_scenarioStartPosition, _buffer.Length - _scenarioStartPosition);
}
}
/// <summary>
/// Gets all log entries in the buffer.
/// </summary>
/// <returns>All captured log content.</returns>
public string GetAllLogs ()
{
lock (_lock)
{
return _buffer.ToString ();
}
}
/// <summary>
/// Clears all captured logs and resets state.
/// </summary>
public void Clear ()
{
lock (_lock)
{
_buffer.Clear ();
_scenarioStartPosition = 0;
_hasErrors = false;
}
}
/// <inheritdoc />
public ILogger CreateLogger (string categoryName)
{
return new ScenarioLogger (this);
}
/// <summary>
/// Called by <see cref="ScenarioLogger"/> to append a log entry.
/// </summary>
/// <param name="logLevel">The log level of the entry.</param>
/// <param name="message">The formatted log message.</param>
internal void Log (LogLevel logLevel, string message)
{
lock (_lock)
{
_buffer.AppendLine ($"[{logLevel}] {message}");
if (logLevel >= LogLevel.Error)
{
_hasErrors = true;
}
}
}
/// <inheritdoc />
public void Dispose ()
{
// Nothing to dispose - buffer is managed memory
}
/// <summary>
/// Internal logger implementation that delegates to <see cref="ScenarioLogCapture"/>.
/// </summary>
private sealed class ScenarioLogger : ILogger
{
private readonly ScenarioLogCapture _capture;
public ScenarioLogger (ScenarioLogCapture capture)
{
_capture = capture;
}
/// <inheritdoc />
public IDisposable? BeginScope<TState> (TState state) where TState : notnull
{
return null;
}
/// <inheritdoc />
public bool IsEnabled (LogLevel logLevel)
{
return true;
}
/// <inheritdoc />
public void Log<TState> (
LogLevel logLevel,
EventId eventId,
TState state,
Exception? exception,
Func<TState, Exception?, string> formatter)
{
string message = formatter (state, exception);
_capture.Log (logLevel, message);
}
}
}