DEV Community

Cover image for Using caller information for verbose logging
Adam K Dean
Adam K Dean

Posted on

1

Using caller information for verbose logging

A cool feature that has been added in Visual Studio 2012 and C# 5.0 is Caller Information attributes. Basically it allows you to see where your method was called from without having to sift through stack traces upon stack traces.

You simply prefix an optional parameter with one of the three attributes:

[CallerMemberName] provides the method or property name of the caller as a string.

[CallerFilePath] provides the full path of the source file that contains the caller as a string.

[CallerLineNumber] provides the line number in the source file at which the method is called as an int.

Learn by example. Here I've made a simple logging method, we pass to it a string, and the compiler will fill in the rest. Make sure to including System.Runtime.CompilerServices in your using statements.

static void VerboseLog(string value,
    [CallerMemberName] string callerMemberName = "",
    [CallerFilePath] string callerFilePath = "",
    [CallerLineNumber] int callerLineNumber = -1)
{
    int index = callerFilePath.LastIndexOf(Path.DirectorySeparatorChar);
    string localPath = callerFilePath.Substring(index + 1);
    Debug.WriteLine("{0} {1}:{2} {3}",
        localPath, callerMemberName, callerLineNumber, value);
}
Enter fullscreen mode Exit fullscreen mode

Let's test it with a simple Console Application. Notice in the above method that I remove the full path to make it more readable. Not necessarily needed but pleasant nonetheless.

static void Main(string[] args)
{
    VerboseLog("this is a test");
    DoSomething();
}

static void DoSomething()
{
    for (int i = 0; i < 5; i++)
    {
        VerboseLog(string.Format("something {0}", i));
    }
}
Enter fullscreen mode Exit fullscreen mode

This outputs:

Program.cs Main:16 this is a test
Program.cs DoSomething:24 something 0
Program.cs DoSomething:24 something 1
Program.cs DoSomething:24 something 2
Program.cs DoSomething:24 something 3
Program.cs DoSomething:24 something 4
Enter fullscreen mode Exit fullscreen mode

So very simple, and yet very, very useful.

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Ask anything about your entire project, code and get answers and even architecture diagrams. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Start free in your IDE

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay