-
-
Save fengli320/bfbfb9948c2463ab1ca724d4b3ddfc15 to your computer and use it in GitHub Desktop.
How to use caller info attributes on .Net 4.0
This file contains 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
// Visual Studio 2012 or above is required | |
using System; | |
using System.Runtime.CompilerServices; | |
namespace System.Runtime.CompilerServices | |
{ | |
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)] | |
public class CallerMemberNameAttribute : Attribute | |
{ | |
} | |
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)] | |
public class CallerFilePathAttribute : Attribute | |
{ | |
} | |
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)] | |
public class CallerLineNumberAttribute : Attribute | |
{ | |
} | |
} | |
namespace ConsoleApplication1 | |
{ | |
class Program | |
{ | |
static void Main() | |
{ | |
LogMessage("message"); | |
} | |
public static void LogMessage(string message, | |
[CallerMemberName] string memberName = "", | |
[CallerFilePath] string filePath = "", | |
[CallerLineNumber] int lineNumber = 0) | |
{ | |
Console.WriteLine("[{0} {1} {2}] {3}", memberName, filePath, lineNumber, message); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment