Skip to content

Commit 5ebeef5

Browse files
committed
refactor: Modernize logging approach
1 parent 806afb2 commit 5ebeef5

File tree

3 files changed

+68
-140
lines changed

3 files changed

+68
-140
lines changed

src/bunit/Extensions/WaitForHelpers/WaitForHelperLoggerExtensions.cs

Lines changed: 43 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,48 @@
22

33
namespace Bunit.Extensions.WaitForHelpers;
44

5-
internal static class WaitForHelperLoggerExtensions
5+
internal static partial class WaitForHelperLoggerExtensions
66
{
7-
private static readonly Action<ILogger, int, Exception?> CheckingWaitCondition
8-
= LoggerMessage.Define<int>(LogLevel.Debug, new EventId(1, "CheckingWaitCondition"), "Checking the wait condition for component {Id}.");
9-
10-
private static readonly Action<ILogger, int, Exception?> CheckCompleted
11-
= LoggerMessage.Define<int>(LogLevel.Debug, new EventId(2, "CheckCompleted"), "The check completed successfully for component {Id}.");
12-
13-
private static readonly Action<ILogger, int, Exception?> CheckFailed
14-
= LoggerMessage.Define<int>(LogLevel.Debug, new EventId(3, "CheckFailed"), "The check failed for component {Id}.");
15-
16-
private static readonly Action<ILogger, int, Exception> CheckThrow
17-
= LoggerMessage.Define<int>(LogLevel.Debug, new EventId(4, "CheckThrow"), "The checker for component {Id} throw an exception.");
18-
19-
private static readonly Action<ILogger, int, Exception?> WaiterTimedOut
20-
= LoggerMessage.Define<int>(LogLevel.Debug, new EventId(10, "WaiterTimedOut"), "The waiter for component {Id} timed out.");
21-
22-
private static readonly Action<ILogger, int, Exception?> WaiterDisposed
23-
= LoggerMessage.Define<int>(LogLevel.Debug, new EventId(20, "WaiterDisposed"), "The waiter for component {Id} disposed.");
24-
25-
internal static void LogCheckingWaitCondition(this ILogger logger, int componentId)
26-
{
27-
if (logger.IsEnabled(LogLevel.Debug))
28-
{
29-
CheckingWaitCondition(logger, componentId, null);
30-
}
31-
}
32-
33-
internal static void LogCheckCompleted(this ILogger logger, int componentId)
34-
{
35-
if (logger.IsEnabled(LogLevel.Debug))
36-
{
37-
CheckCompleted(logger, componentId, null);
38-
}
39-
}
40-
41-
internal static void LogCheckFailed(this ILogger logger, int componentId)
42-
{
43-
if (logger.IsEnabled(LogLevel.Debug))
44-
{
45-
CheckFailed(logger, componentId, null);
46-
}
47-
}
48-
49-
internal static void LogCheckThrow(this ILogger logger, int componentId, Exception exception)
50-
{
51-
if (logger.IsEnabled(LogLevel.Debug))
52-
{
53-
CheckThrow(logger, componentId, exception);
54-
}
55-
}
56-
57-
internal static void LogWaiterTimedOut(this ILogger logger, int componentId)
58-
{
59-
if (logger.IsEnabled(LogLevel.Debug))
60-
{
61-
WaiterTimedOut(logger, componentId, null);
62-
}
63-
}
64-
65-
internal static void LogWaiterDisposed(this ILogger logger, int componentId)
66-
{
67-
if (logger.IsEnabled(LogLevel.Debug))
68-
{
69-
WaiterDisposed(logger, componentId, null);
70-
}
71-
}
7+
[LoggerMessage(
8+
EventId = 1,
9+
EventName = "CheckingWaitCondition",
10+
Level = LogLevel.Debug,
11+
Message = "Checking the wait condition for component {ComponentId}.")]
12+
13+
internal static partial void LogCheckingWaitCondition(this ILogger logger, int componentId);
14+
15+
[LoggerMessage(
16+
EventId = 2,
17+
EventName = "CheckCompleted",
18+
Level = LogLevel.Debug,
19+
Message = "The check completed successfully for component {ComponentId}.")]
20+
internal static partial void LogCheckCompleted(this ILogger logger, int componentId);
21+
22+
[LoggerMessage(
23+
EventId = 3,
24+
EventName = "CheckFailed",
25+
Level = LogLevel.Debug,
26+
Message = "The check failed for component {ComponentId}.")]
27+
internal static partial void LogCheckFailed(this ILogger logger, int componentId);
28+
29+
[LoggerMessage(
30+
EventId = 4,
31+
EventName = "CheckThrow",
32+
Level = LogLevel.Debug,
33+
Message = "The checker for component {ComponentId} throw an exception.")]
34+
internal static partial void LogCheckThrow(this ILogger logger, int componentId, Exception exception);
35+
36+
[LoggerMessage(
37+
EventId = 10,
38+
EventName = "WaiterTimedOut",
39+
Level = LogLevel.Debug,
40+
Message = "The waiter for component {ComponentId} timed out.")]
41+
internal static partial void LogWaiterTimedOut(this ILogger logger, int componentId);
42+
43+
[LoggerMessage(
44+
EventId = 20,
45+
EventName = "WaiterDisposed",
46+
Level = LogLevel.Debug,
47+
Message = "The waiter for component {ComponentId} disposed.")]
48+
internal static partial void LogWaiterDisposed(this ILogger logger, int componentId);
7249
}

src/bunit/Rendering/BunitRendererLoggerExtensions.cs

Lines changed: 14 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -3,77 +3,21 @@
33

44
namespace Bunit.Rendering;
55

6-
internal static class BunitRendererLoggerExtensions
6+
internal static partial class BunitRendererLoggerExtensions
77
{
8-
private static readonly Action<ILogger, int, Exception?> ComponentDisposed
9-
= LoggerMessage.Define<int>(
10-
LogLevel.Debug,
11-
new EventId(11, "ComponentDisposed"),
12-
"Component {Id} has been disposed.");
13-
14-
internal static void LogComponentDisposed(this ILogger<BunitRenderer> logger, int componentId)
15-
{
16-
if (logger.IsEnabled(LogLevel.Debug))
17-
{
18-
ComponentDisposed(logger, componentId, null);
19-
}
20-
}
21-
22-
private static readonly Action<ILogger, int, Exception?> ComponentRendered
23-
= LoggerMessage.Define<int>(
24-
LogLevel.Debug,
25-
new EventId(12, "ComponentRendered"),
26-
"Component {ComponentId} has been rendered.");
27-
28-
internal static void LogComponentRendered(this ILogger<BunitRenderer> logger, int componentId)
29-
{
30-
if (logger.IsEnabled(LogLevel.Debug))
31-
{
32-
ComponentRendered(logger, componentId, null);
33-
}
34-
}
35-
36-
private static readonly Action<ILogger, int, int, Exception?> DisposedChildInRenderTreeFrame
37-
= LoggerMessage.Define<int, int>(
38-
LogLevel.Warning,
39-
new EventId(14, "DisposedChildInRenderTreeFrame"),
40-
"A parent components {ParentComponentId} has a disposed component {ComponentId} as its child.");
41-
42-
internal static void LogDisposedChildInRenderTreeFrame(this ILogger<BunitRenderer> logger, int parentComponentId, int componentId)
43-
{
44-
if (logger.IsEnabled(LogLevel.Warning))
45-
{
46-
DisposedChildInRenderTreeFrame(logger, parentComponentId, componentId, null);
47-
}
48-
}
49-
50-
private static readonly Action<ILogger, Exception?> AsyncInitialRender
51-
= LoggerMessage.Define(
52-
LogLevel.Debug,
53-
new EventId(20, "AsyncInitialRender"),
54-
"The initial render task did not complete immediately.");
55-
56-
internal static void LogAsyncInitialRender(this ILogger<BunitRenderer> logger)
57-
{
58-
if (logger.IsEnabled(LogLevel.Debug))
59-
{
60-
AsyncInitialRender(logger, null);
61-
}
62-
}
63-
64-
private static readonly Action<ILogger, int, Exception?> InitialRenderCompleted
65-
= LoggerMessage.Define<int>(
66-
LogLevel.Debug,
67-
new EventId(21, "InitialRenderCompleted"),
68-
"The initial render of component {ComponentId} is completed.");
69-
70-
internal static void LogInitialRenderCompleted(this ILogger<BunitRenderer> logger, int componentId)
71-
{
72-
if (logger.IsEnabled(LogLevel.Debug))
73-
{
74-
InitialRenderCompleted(logger, componentId, null);
75-
}
76-
}
8+
[LoggerMessage(
9+
EventId = 20,
10+
EventName = "AsyncInitialRender",
11+
Level = LogLevel.Debug,
12+
Message = "The initial render task did not complete immediately.")]
13+
internal static partial void LogAsyncInitialRender(this ILogger<BunitRenderer> logger);
14+
15+
[LoggerMessage(
16+
EventId = 21,
17+
EventName = "InitialRenderCompleted",
18+
Level = LogLevel.Debug,
19+
Message = "The initial render of component {ComponentId} is completed.")]
20+
internal static partial void LogInitialRenderCompleted(this ILogger<BunitRenderer> logger, int componentId);
7721

7822
private static readonly Action<ILogger, string, string, Exception> UnhandledException
7923
= LoggerMessage.Define<string, string>(

src/bunit/TestDoubles/ErrorBoundary/BunitErrorBoundaryLogger.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@ namespace Bunit.TestDoubles;
66
/// Default implementation of an IErrorBoundaryLogger (needed for ErrorBoundary component).
77
/// It delegates the implementation of LogErrorAsync to an instance created from ILoggerFactory.
88
/// </summary>
9-
internal class BunitErrorBoundaryLogger : IErrorBoundaryLogger
9+
internal partial class BunitErrorBoundaryLogger : IErrorBoundaryLogger
1010
{
11-
private static readonly Action<ILogger, string, Exception> ExceptionCaughtByErrorBoundary
12-
= LoggerMessage.Define<string>(LogLevel.Warning, new EventId(100, "ExceptionCaughtByErrorBoundary"), "Unhandled exception rendering component: {Message}");
13-
1411
private readonly ILogger logger;
1512

1613
/// <summary>
@@ -29,4 +26,14 @@ public ValueTask LogErrorAsync(Exception exception)
2926
ExceptionCaughtByErrorBoundary(logger, exception.Message, exception);
3027
return ValueTask.CompletedTask;
3128
}
29+
30+
[LoggerMessage(
31+
EventId = 100,
32+
EventName = "ExceptionCaughtByErrorBoundary",
33+
Level = LogLevel.Warning,
34+
Message = "Unhandled exception rendering component: {Message}")]
35+
private static partial void ExceptionCaughtByErrorBoundary(
36+
ILogger logger,
37+
string message,
38+
Exception exception);
3239
}

0 commit comments

Comments
 (0)