Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Functional logging extensions #1148

Merged
merged 8 commits into from
Aug 9, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
239 changes: 239 additions & 0 deletions src/Whipstaff.Core/Logging/LogExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public static class LogExtensions
private static readonly Action<ILogger, string, Exception?> _traceMethodExitAction =
LoggerMessage.Define<string>(LogLevel.Trace, EventIdFactory.MethodEntryEventId(), "Method Exit: {MethodName}");

private static readonly Action<ILogger, string, Exception?> _traceMethodExceptionAction =
LoggerMessage.Define<string>(LogLevel.Trace, EventIdFactory.MethodEntryEventId(), "Method Exception: {MethodName}");

/// <summary>
/// Traces the method entry.
/// </summary>
Expand Down Expand Up @@ -54,5 +57,241 @@ public static class LogExtensions

_traceMethodExitAction(logger, callerMemberName, exception);
}

/// <summary>
/// Write a trace event if the log level is enabled.
/// </summary>
/// <param name="logger">Logging instance.</param>
/// <param name="messageFunc">Message producing func to evaluate if log level enabled.</param>
public static void TraceIfEnabled(
this ILogger logger,
Func<string> messageFunc)
{
#pragma warning disable CA1062 // Validate arguments of public methods
logger.LogIfEnabled(LogLevel.Trace, messageFunc);
#pragma warning restore CA1062 // Validate arguments of public methods
}

/// <summary>
/// Write a trace event and exception if the log level is enabled.
/// </summary>
/// <param name="logger">Logging instance.</param>
/// <param name="exception">Exception that occurred.</param>
/// <param name="messageFunc">Message producing func to evaluate if log level enabled.</param>
public static void TraceIfEnabled(
this ILogger logger,
Exception exception,
Func<string> messageFunc)
{
#pragma warning disable CA1062 // Validate arguments of public methods
logger.LogIfEnabled(LogLevel.Trace, exception, messageFunc);
#pragma warning restore CA1062 // Validate arguments of public methods
}

/// <summary>
/// Traces an exception in a method.
/// </summary>
/// <param name="logger">Logging instance.</param>
/// <param name="exception">Exception that occurred.</param>
/// <param name="callerMemberName">Name of the method.</param>
public static void TraceMethodException(
this ILogger logger,
Exception exception,
[CallerMemberName] string? callerMemberName = null)
{
if (string.IsNullOrWhiteSpace(callerMemberName))
{
return;
}

_traceMethodExceptionAction(logger, callerMemberName, exception);
}

/// <summary>
/// Write a warning event if the log level is enabled.
/// </summary>
/// <param name="logger">Logging instance.</param>
/// <param name="messageFunc">Message producing func to evaluate if log level enabled.</param>
public static void WarningIfEnabled(
this ILogger logger,
Func<string> messageFunc)
{
#pragma warning disable CA1062 // Validate arguments of public methods
logger.LogIfEnabled(LogLevel.Warning, messageFunc);
#pragma warning restore CA1062 // Validate arguments of public methods
}

/// <summary>
/// Write a warn event and exception if the log level is enabled.
/// </summary>
/// <param name="logger">Logging instance.</param>
/// <param name="exception">Exception that occurred.</param>
/// <param name="messageFunc">Message producing func to evaluate if log level enabled.</param>
public static void WarningIfEnabled(
this ILogger logger,
Exception exception,
Func<string> messageFunc)
{
#pragma warning disable CA1062 // Validate arguments of public methods
logger.LogIfEnabled(LogLevel.Warning, exception, messageFunc);
#pragma warning restore CA1062 // Validate arguments of public methods
}

/// <summary>
/// Write a error event if the log level is enabled.
/// </summary>
/// <param name="logger">Logging instance.</param>
/// <param name="messageFunc">Message producing func to evaluate if log level enabled.</param>
public static void ErrorIfEnabled(
this ILogger logger,
Func<string> messageFunc)
{
#pragma warning disable CA1062 // Validate arguments of public methods
logger.LogIfEnabled(LogLevel.Error, messageFunc);
#pragma warning restore CA1062 // Validate arguments of public methods
}

/// <summary>
/// Write a error event if the log level is enabled.
/// </summary>
/// <param name="logger">Logging instance.</param>
/// <param name="exception">Exception that occurred.</param>
/// <param name="messageFunc">Message producing func to evaluate if log level enabled.</param>
public static void ErrorIfEnabled(
this ILogger logger,
Exception exception,
Func<string> messageFunc)
{
#pragma warning disable CA1062 // Validate arguments of public methods
logger.LogIfEnabled(LogLevel.Error, exception, messageFunc);
#pragma warning restore CA1062 // Validate arguments of public methods
}

/// <summary>
/// Write a information event if the log level is enabled.
/// </summary>
/// <param name="logger">Logging instance.</param>
/// <param name="messageFunc">Message producing func to evaluate if log level enabled.</param>
public static void InformationIfEnabled(
this ILogger logger,
Func<string> messageFunc)
{
#pragma warning disable CA1062 // Validate arguments of public methods
logger.LogIfEnabled(LogLevel.Information, messageFunc);
#pragma warning restore CA1062 // Validate arguments of public methods
}

/// <summary>
/// Write a information event if the log level is enabled.
/// </summary>
/// <param name="logger">Logging instance.</param>
/// <param name="exception">Exception that occurred.</param>
/// <param name="messageFunc">Message producing func to evaluate if log level enabled.</param>
public static void InformationIfEnabled(
this ILogger logger,
Exception exception,
Func<string> messageFunc)
{
#pragma warning disable CA1062 // Validate arguments of public methods
logger.LogIfEnabled(LogLevel.Information, exception, messageFunc);
#pragma warning restore CA1062 // Validate arguments of public methods
}

/// <summary>
/// Write a debug event if the log level is enabled.
/// </summary>
/// <param name="logger">Logging instance.</param>
/// <param name="messageFunc">Message producing func to evaluate if log level enabled.</param>
public static void DebugIfEnabled(
this ILogger logger,
Func<string> messageFunc)
{
#pragma warning disable CA1062 // Validate arguments of public methods
logger.LogIfEnabled(LogLevel.Debug, messageFunc);
#pragma warning restore CA1062 // Validate arguments of public methods
}

/// <summary>
/// Write a debug event if the log level is enabled.
/// </summary>
/// <param name="logger">Logging instance.</param>
/// <param name="exception">Exception that occurred.</param>
/// <param name="messageFunc">Message producing func to evaluate if log level enabled.</param>
public static void DebugIfEnabled(
this ILogger logger,
Exception exception,
Func<string> messageFunc)
{
#pragma warning disable CA1062 // Validate arguments of public methods
logger.LogIfEnabled(LogLevel.Debug, exception, messageFunc);
#pragma warning restore CA1062 // Validate arguments of public methods
}

/// <summary>
/// Write a critical event if the log level is enabled.
/// </summary>
/// <param name="logger">Logging instance.</param>
/// <param name="messageFunc">Message producing func to evaluate if log level enabled.</param>
public static void CriticalIfEnabled(
this ILogger logger,
Func<string> messageFunc)
{
#pragma warning disable CA1062 // Validate arguments of public methods
logger.LogIfEnabled(LogLevel.Critical, messageFunc);
#pragma warning restore CA1062 // Validate arguments of public methods
}

/// <summary>
/// Write a critical event if the log level is enabled.
/// </summary>
/// <param name="logger">Logging instance.</param>
/// <param name="exception">Exception that occurred.</param>
/// <param name="messageFunc">Message producing func to evaluate if log level enabled.</param>
public static void CriticalIfEnabled(
this ILogger logger,
Exception exception,
Func<string> messageFunc)
{
#pragma warning disable CA1062 // Validate arguments of public methods
logger.LogIfEnabled(LogLevel.Critical, exception, messageFunc);
#pragma warning restore CA1062 // Validate arguments of public methods
}

private static void LogIfEnabled(
this ILogger logger,
LogLevel logLevel,
Exception exception,
Func<string> messageFunc)
{
if (!logger.IsEnabled(logLevel))
{
return;
}

var message = messageFunc();
#pragma warning disable CA1848 // Use the LoggerMessage delegates
#pragma warning disable CA2254 // Template should be a static expression
logger.Log(logLevel, exception, message);
#pragma warning restore CA2254 // Template should be a static expression
#pragma warning restore CA1848 // Use the LoggerMessage delegates
}

private static void LogIfEnabled(
this ILogger logger,
LogLevel logLevel,
Func<string> messageFunc)
{
if (!logger.IsEnabled(logLevel))
{
return;
}

var message = messageFunc();
#pragma warning disable CA1848 // Use the LoggerMessage delegates
#pragma warning disable CA2254 // Template should be a static expression
logger.Log(logLevel, message);
#pragma warning restore CA2254 // Template should be a static expression
#pragma warning restore CA1848 // Use the LoggerMessage delegates
}
}
}