Skip to content

Instantly share code, notes, and snippets.

@jmangelo
Created June 9, 2012 14:51
Show Gist options
  • Save jmangelo/2901284 to your computer and use it in GitHub Desktop.
Save jmangelo/2901284 to your computer and use it in GitHub Desktop.
Extended Logging Sample with .NET 4.5 Caller Info Attributes and NLog
using System;
using System.IO;
using System.Runtime.CompilerServices;
using NLog;
public sealed class CallerInfo
{
private CallerInfo(string filePath, string memberName, int lineNumber)
{
this.FilePath = filePath;
this.MemberName = memberName;
this.LineNumber = lineNumber;
}
public static CallerInfo Create(
[CallerFilePath] string filePath = "",
[CallerMemberName] string memberName = "",
[CallerLineNumber] int lineNumber = 0)
{
return new CallerInfo(filePath, memberName, lineNumber);
}
public string FilePath { get; private set; }
public string FileName
{
get
{
return this.fileName ?? (this.fileName = Path.GetFileName(this.FilePath));
}
}
public string MemberName { get; private set; }
public int LineNumber { get; private set; }
public override string ToString()
{
return string.Concat(this.FilePath, "|", this.MemberName, "|", this.LineNumber);
}
private string fileName;
}
public static class LoggerExtensions
{
public static void TraceMemberEntry(
this Logger logger,
[CallerFilePath] string filePath = "",
[CallerMemberName] string memberName = "",
[CallerLineNumber] int lineNumber = 0)
{
LogMemberEntry(logger, LogLevel.Trace, filePath, memberName, lineNumber);
}
public static void TraceMemberExit(
this Logger logger,
[CallerFilePath] string filePath = "",
[CallerMemberName] string memberName = "",
[CallerLineNumber] int lineNumber = 0)
{
LogMemberExit(logger, LogLevel.Trace, filePath, memberName, lineNumber);
}
public static void DebugMemberEntry(
this Logger logger,
[CallerFilePath] string filePath = "",
[CallerMemberName] string memberName = "",
[CallerLineNumber] int lineNumber = 0)
{
LogMemberEntry(logger, LogLevel.Debug, filePath, memberName, lineNumber);
}
public static void DebugMemberExit(
this Logger logger,
[CallerFilePath] string filePath = "",
[CallerMemberName] string memberName = "",
[CallerLineNumber] int lineNumber = 0)
{
LogMemberExit(logger, LogLevel.Debug, filePath, memberName, lineNumber);
}
public static void LogMemberEntry(
this Logger logger,
LogLevel logLevel,
[CallerFilePath] string filePath = "",
[CallerMemberName] string memberName = "",
[CallerLineNumber] int lineNumber = 0)
{
const string MsgFormat = "Entering member {1} at line {2}";
InternalLog(logger, logLevel, MsgFormat, filePath, memberName, lineNumber);
}
public static void LogMemberExit(
this Logger logger,
LogLevel logLevel,
[CallerFilePath] string filePath = "",
[CallerMemberName] string memberName = "",
[CallerLineNumber] int lineNumber = 0)
{
const string MsgFormat = "Exiting member {1} at line {2}";
InternalLog(logger, logLevel, MsgFormat, filePath, memberName, lineNumber);
}
private static void InternalLog(
Logger logger,
LogLevel logLevel,
string format,
string filePath,
string memberName,
int lineNumber)
{
if (logger == null)
throw new ArgumentNullException("logger");
if (logLevel == null)
throw new ArgumentNullException("logLevel");
logger.Log(logLevel, format, filePath, memberName, lineNumber);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment