Last active
June 6, 2025 10:35
-
-
Save chrisfcarroll/a0041ae04c6f21849791ca8413c3596e to your computer and use it in GitHub Desktop.
LogAction uses [CallerMemberName] to make application level logging briefer, easier, and more useful
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Runtime.CompilerServices; | |
using Microsoft.Extensions.Logging; | |
public static class LogActions | |
{ | |
/// <summary> | |
/// Log the current Method call and any relevant | |
/// parameter(s) <paramref name="loggableState"/> at log level <see cref="LogLevel.Information"/> | |
/// </summary> | |
/// <param name="log">the ILogger.</param> | |
/// <param name="loggableState">any relevant parameter or value to log</param> | |
/// <param name="actionDescription">compiler generated, no need to specify.</param> | |
public static void LogActionInformation(this ILogger log, | |
object? loggableState = null, | |
[CallerMemberName] string actionDescription = "") | |
=> log.LogAction(loggableState: loggableState, logLevel: LogLevel.Information,actionDescription: actionDescription); | |
/// <summary> | |
/// Log the current Method call and any relevant | |
/// parameter(s) <paramref name="loggableState"/> at log level <see cref="LogLevel.Debug"/> | |
/// </summary> | |
/// <param name="log">the ILogger.</param> | |
/// <param name="loggableState">any relevant parameter or value to log</param> | |
/// <param name="actionDescription">compiler generated, no need to specify.</param> | |
public static void LogActionDebug(this ILogger log, | |
object? loggableState = null, | |
[CallerMemberName] string actionDescription = "") | |
=> log.LogAction(loggableState: loggableState, logLevel: LogLevel.Debug,actionDescription: actionDescription); | |
/// <summary> | |
/// Log the current Method call and any relevant | |
/// parameter(s) <paramref name="loggableState"/> at log level <see cref="LogLevel.Trace"/> | |
/// </summary> | |
/// <param name="log">the ILogger.</param> | |
/// <param name="loggableState">any relevant parameter or value to log</param> | |
/// <param name="actionDescription">compiler generated, no need to specify.</param> | |
public static void LogActionTrace(this ILogger log, | |
object? loggableState = null, | |
[CallerMemberName] string actionDescription = "") | |
=> log.LogAction(loggableState: loggableState, logLevel: LogLevel.Trace,actionDescription: actionDescription); | |
/// <summary> | |
/// Log the current Method call and any relevant | |
/// parameter(s) <paramref name="loggableState"/> at log level <see cref="LogLevel.Warning"/> | |
/// </summary> | |
/// <param name="log">the ILogger.</param> | |
/// <param name="loggableState">any relevant parameter or value to log</param> | |
/// <param name="actionDescription">compiler generated, no need to specify.</param> | |
public static void LogActionWarning(this ILogger log, | |
object? loggableState = null, | |
[CallerMemberName] string actionDescription = "") | |
=> log.LogAction(loggableState: loggableState, logLevel: LogLevel.Warning,actionDescription: actionDescription); | |
/// <summary> | |
/// Log <paramref name="ex"/> and the fact that Method <paramref name="actionDescription"/> was | |
/// called with parameter(s) <paramref name="loggableState"/> at log level <see cref="LogLevel.Error"/> | |
/// </summary> | |
/// <param name="log">the ILogger.</param> | |
/// <param name="loggableState">any relevant parameter or value to log</param> | |
/// <param name="actionDescription">compiler generated, no need to specify.</param> | |
public static void LogActionException(this ILogger log, | |
Exception ex, | |
object? loggableState = null, | |
[CallerMemberName] string actionDescription = "") | |
=> log.LogError(ex,"{Action}({Value})",actionDescription,loggableState); | |
/// <summary> | |
/// Log the current Method call and any relevant | |
/// parameter(s) <paramref name="loggableState"/> at log level <see cref="LogLevel.Information"/> | |
/// </summary> | |
/// <param name="log">the ILogger.</param> | |
/// <param name="loggableState">any relevant parameter or value to log</param> | |
/// <param name="logLevel">Defaults to <see cref="LogLevel.Debug"/>. The log level to use.</param> | |
/// <param name="actionDescription">compiler generated, no need to specify.</param> | |
public static void LogAction(this ILogger log, | |
object? loggableState = null, | |
LogLevel logLevel = LogLevel.Debug, | |
[CallerMemberName] string actionDescription = "") | |
=> log.Log(logLevel,"{Action}({State})",actionDescription,loggableState); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example Usage: