Skip to content

Instantly share code, notes, and snippets.

@chrisfcarroll
Last active October 10, 2025 14:45
Show Gist options
  • Select an option

  • Save chrisfcarroll/a0041ae04c6f21849791ca8413c3596e to your computer and use it in GitHub Desktop.

Select an option

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 )
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 + ":";
}
@chrisfcarroll
Copy link
Author

Example Usage:

class MyClass(ILogger<MyClass> log)
{
  public void MyMethod(MyType param1, MyType2 param2)
  {
    log.LogAction( (param1, param2.ToString()) );
    ... rest of method ...
    log.LogActionDebug( (param1, param2.ToString()) );
    log.LogActionWarning( (param1, param2.ToString()) );
   log.LogActionException(ex, (other relevant state) );
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment