Skip to content

Instantly share code, notes, and snippets.

@fengli320
Forked from svick/CallerInfoAttibuteNet4.cs
Last active April 10, 2021 14:35
Show Gist options
  • Save fengli320/bfbfb9948c2463ab1ca724d4b3ddfc15 to your computer and use it in GitHub Desktop.
Save fengli320/bfbfb9948c2463ab1ca724d4b3ddfc15 to your computer and use it in GitHub Desktop.
How to use caller info attributes on .Net 4.0
// 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