Last active
October 10, 2025 14:45
-
-
Save chrisfcarroll/a0041ae04c6f21849791ca8413c3596e to your computer and use it in GitHub Desktop.
Briefer application log lines automatically adds method name and item descriptions | log.LogAction() | log.LogAction( thisAndthat ) | log.LogAction( thisAndthat, other )
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 = "", | |
| [CallerArgumentExpression("loggableState")]string? stateName=null) | |
| => log.LogAction(loggableState, logLevel: LogLevel.Information,actionDescription, stateName); | |
| /// <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 = "", | |
| [CallerArgumentExpression("loggableState")]string? stateName=null) | |
| => log.LogAction(loggableState, logLevel: LogLevel.Debug,actionDescription,stateName); | |
| /// <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 = "", | |
| [CallerArgumentExpression("loggableState")]string? stateName=null) | |
| => log.LogAction(loggableState: loggableState, logLevel: LogLevel.Trace,actionDescription: actionDescription, stateName: stateName); | |
| /// <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 = "", | |
| [CallerArgumentExpression("loggableState")]string? stateName=null) | |
| => log.LogAction(loggableState, LogLevel.Warning, actionDescription, stateName); | |
| /// <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 = "", | |
| [CallerArgumentExpression("loggableState")]string? stateName=null) | |
| => log.LogError(ex,"{Action}({StateName}{Value})", | |
| actionDescription, | |
| stateNameIfHelpful(actionDescription,stateName), | |
| loggableState); | |
| /// <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 LogActionError(this ILogger log, | |
| object? loggableState = null, | |
| [CallerMemberName] string actionDescription = "", | |
| [CallerArgumentExpression("loggableState")]string? stateName=null) | |
| => log.LogError("{Action}({StateName}{Value})", | |
| actionDescription, | |
| stateNameIfHelpful(actionDescription,stateName), | |
| 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> | |
| /// <param name="stateName"></param> | |
| public static void LogAction(this ILogger log, | |
| object? loggableState = null, | |
| LogLevel logLevel = LogLevel.Debug, | |
| [CallerMemberName] string actionDescription = "", | |
| [CallerArgumentExpression("loggableState")]string? stateName=null) | |
| => log.Log(logLevel,"{Action}({StateName}{Value})", | |
| actionDescription, | |
| stateNameIfHelpful(actionDescription,stateName), | |
| loggableState); | |
| static string stateNameIfHelpful(string actionDescription,string? stateName) | |
| => (stateName is null || stateName==actionDescription)? "" : stateName + ":"; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example Usage: