Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 43 additions & 66 deletions src/bunit/Extensions/WaitForHelpers/WaitForHelperLoggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,48 @@

namespace Bunit.Extensions.WaitForHelpers;

internal static class WaitForHelperLoggerExtensions
internal static partial class WaitForHelperLoggerExtensions
{
private static readonly Action<ILogger, int, Exception?> CheckingWaitCondition
= LoggerMessage.Define<int>(LogLevel.Debug, new EventId(1, "CheckingWaitCondition"), "Checking the wait condition for component {Id}.");

private static readonly Action<ILogger, int, Exception?> CheckCompleted
= LoggerMessage.Define<int>(LogLevel.Debug, new EventId(2, "CheckCompleted"), "The check completed successfully for component {Id}.");

private static readonly Action<ILogger, int, Exception?> CheckFailed
= LoggerMessage.Define<int>(LogLevel.Debug, new EventId(3, "CheckFailed"), "The check failed for component {Id}.");

private static readonly Action<ILogger, int, Exception> CheckThrow
= LoggerMessage.Define<int>(LogLevel.Debug, new EventId(4, "CheckThrow"), "The checker for component {Id} throw an exception.");

private static readonly Action<ILogger, int, Exception?> WaiterTimedOut
= LoggerMessage.Define<int>(LogLevel.Debug, new EventId(10, "WaiterTimedOut"), "The waiter for component {Id} timed out.");

private static readonly Action<ILogger, int, Exception?> WaiterDisposed
= LoggerMessage.Define<int>(LogLevel.Debug, new EventId(20, "WaiterDisposed"), "The waiter for component {Id} disposed.");

internal static void LogCheckingWaitCondition(this ILogger logger, int componentId)
{
if (logger.IsEnabled(LogLevel.Debug))
{
CheckingWaitCondition(logger, componentId, null);
}
}

internal static void LogCheckCompleted(this ILogger logger, int componentId)
{
if (logger.IsEnabled(LogLevel.Debug))
{
CheckCompleted(logger, componentId, null);
}
}

internal static void LogCheckFailed(this ILogger logger, int componentId)
{
if (logger.IsEnabled(LogLevel.Debug))
{
CheckFailed(logger, componentId, null);
}
}

internal static void LogCheckThrow(this ILogger logger, int componentId, Exception exception)
{
if (logger.IsEnabled(LogLevel.Debug))
{
CheckThrow(logger, componentId, exception);
}
}

internal static void LogWaiterTimedOut(this ILogger logger, int componentId)
{
if (logger.IsEnabled(LogLevel.Debug))
{
WaiterTimedOut(logger, componentId, null);
}
}

internal static void LogWaiterDisposed(this ILogger logger, int componentId)
{
if (logger.IsEnabled(LogLevel.Debug))
{
WaiterDisposed(logger, componentId, null);
}
}
[LoggerMessage(
EventId = 1,
EventName = "CheckingWaitCondition",
Level = LogLevel.Debug,
Message = "Checking the wait condition for component {ComponentId}.")]

internal static partial void LogCheckingWaitCondition(this ILogger logger, int componentId);

[LoggerMessage(
EventId = 2,
EventName = "CheckCompleted",
Level = LogLevel.Debug,
Message = "The check completed successfully for component {ComponentId}.")]
internal static partial void LogCheckCompleted(this ILogger logger, int componentId);

[LoggerMessage(
EventId = 3,
EventName = "CheckFailed",
Level = LogLevel.Debug,
Message = "The check failed for component {ComponentId}.")]
internal static partial void LogCheckFailed(this ILogger logger, int componentId);

[LoggerMessage(
EventId = 4,
EventName = "CheckThrow",
Level = LogLevel.Debug,
Message = "The checker for component {ComponentId} throw an exception.")]
internal static partial void LogCheckThrow(this ILogger logger, int componentId, Exception exception);

[LoggerMessage(
EventId = 10,
EventName = "WaiterTimedOut",
Level = LogLevel.Debug,
Message = "The waiter for component {ComponentId} timed out.")]
internal static partial void LogWaiterTimedOut(this ILogger logger, int componentId);

[LoggerMessage(
EventId = 20,
EventName = "WaiterDisposed",
Level = LogLevel.Debug,
Message = "The waiter for component {ComponentId} disposed.")]
internal static partial void LogWaiterDisposed(this ILogger logger, int componentId);
}
84 changes: 14 additions & 70 deletions src/bunit/Rendering/BunitRendererLoggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,77 +3,21 @@

namespace Bunit.Rendering;

internal static class BunitRendererLoggerExtensions
internal static partial class BunitRendererLoggerExtensions
{
private static readonly Action<ILogger, int, Exception?> ComponentDisposed
= LoggerMessage.Define<int>(
LogLevel.Debug,
new EventId(11, "ComponentDisposed"),
"Component {Id} has been disposed.");

internal static void LogComponentDisposed(this ILogger<BunitRenderer> logger, int componentId)
{
if (logger.IsEnabled(LogLevel.Debug))
{
ComponentDisposed(logger, componentId, null);
}
}

private static readonly Action<ILogger, int, Exception?> ComponentRendered
= LoggerMessage.Define<int>(
LogLevel.Debug,
new EventId(12, "ComponentRendered"),
"Component {ComponentId} has been rendered.");

internal static void LogComponentRendered(this ILogger<BunitRenderer> logger, int componentId)
{
if (logger.IsEnabled(LogLevel.Debug))
{
ComponentRendered(logger, componentId, null);
}
}

private static readonly Action<ILogger, int, int, Exception?> DisposedChildInRenderTreeFrame
= LoggerMessage.Define<int, int>(
LogLevel.Warning,
new EventId(14, "DisposedChildInRenderTreeFrame"),
"A parent components {ParentComponentId} has a disposed component {ComponentId} as its child.");

internal static void LogDisposedChildInRenderTreeFrame(this ILogger<BunitRenderer> logger, int parentComponentId, int componentId)
{
if (logger.IsEnabled(LogLevel.Warning))
{
DisposedChildInRenderTreeFrame(logger, parentComponentId, componentId, null);
}
}

private static readonly Action<ILogger, Exception?> AsyncInitialRender
= LoggerMessage.Define(
LogLevel.Debug,
new EventId(20, "AsyncInitialRender"),
"The initial render task did not complete immediately.");

internal static void LogAsyncInitialRender(this ILogger<BunitRenderer> logger)
{
if (logger.IsEnabled(LogLevel.Debug))
{
AsyncInitialRender(logger, null);
}
}

private static readonly Action<ILogger, int, Exception?> InitialRenderCompleted
= LoggerMessage.Define<int>(
LogLevel.Debug,
new EventId(21, "InitialRenderCompleted"),
"The initial render of component {ComponentId} is completed.");

internal static void LogInitialRenderCompleted(this ILogger<BunitRenderer> logger, int componentId)
{
if (logger.IsEnabled(LogLevel.Debug))
{
InitialRenderCompleted(logger, componentId, null);
}
}
[LoggerMessage(
EventId = 20,
EventName = "AsyncInitialRender",
Level = LogLevel.Debug,
Message = "The initial render task did not complete immediately.")]
internal static partial void LogAsyncInitialRender(this ILogger<BunitRenderer> logger);

[LoggerMessage(
EventId = 21,
EventName = "InitialRenderCompleted",
Level = LogLevel.Debug,
Message = "The initial render of component {ComponentId} is completed.")]
internal static partial void LogInitialRenderCompleted(this ILogger<BunitRenderer> logger, int componentId);

private static readonly Action<ILogger, string, string, Exception> UnhandledException
= LoggerMessage.Define<string, string>(
Expand Down
15 changes: 11 additions & 4 deletions src/bunit/TestDoubles/ErrorBoundary/BunitErrorBoundaryLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ namespace Bunit.TestDoubles;
/// Default implementation of an IErrorBoundaryLogger (needed for ErrorBoundary component).
/// It delegates the implementation of LogErrorAsync to an instance created from ILoggerFactory.
/// </summary>
internal class BunitErrorBoundaryLogger : IErrorBoundaryLogger
internal partial class BunitErrorBoundaryLogger : IErrorBoundaryLogger
{
private static readonly Action<ILogger, string, Exception> ExceptionCaughtByErrorBoundary
= LoggerMessage.Define<string>(LogLevel.Warning, new EventId(100, "ExceptionCaughtByErrorBoundary"), "Unhandled exception rendering component: {Message}");

private readonly ILogger logger;

/// <summary>
Expand All @@ -29,4 +26,14 @@ public ValueTask LogErrorAsync(Exception exception)
ExceptionCaughtByErrorBoundary(logger, exception.Message, exception);
return ValueTask.CompletedTask;
}

[LoggerMessage(
EventId = 100,
EventName = "ExceptionCaughtByErrorBoundary",
Level = LogLevel.Warning,
Message = "Unhandled exception rendering component: {Message}")]
private static partial void ExceptionCaughtByErrorBoundary(
ILogger logger,
string message,
Exception exception);
}