Skip to content

Instantly share code, notes, and snippets.

@dpavsrtrl
Created June 1, 2018 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpavsrtrl/5ff3c7d2034cce3daf853eb346534b8a to your computer and use it in GitHub Desktop.
Save dpavsrtrl/5ff3c7d2034cce3daf853eb346534b8a to your computer and use it in GitHub Desktop.
using System.Runtime.CompilerServices;
using NLog;
namespace NLogCallerName
{
class Program
{
static void Main(string[] args)
{
var logger = LogManager.GetLogger("My Log");
logger.LogCaller(LogLevel.Trace, "Gues who");
}
}
public static class NLogExtensions
{
public static void LogCaller(this ILogger logger, LogLevel level, string message, [CallerMemberName] string caller = null)
{
var header = caller == null ? "" : $"{caller}: ";
logger.Log(level, $"{header}{message}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment