Skip to content

Instantly share code, notes, and snippets.

@chrisfcarroll
Last active June 6, 2025 10:35
Show Gist options
  • Save chrisfcarroll/a0041ae04c6f21849791ca8413c3596e to your computer and use it in GitHub Desktop.
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
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);
}
@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