By using Caller Info attributes, you can obtain information about the caller to a method. You can obtain file path of the source code, the line number in the source code, and the member name of the caller. This information is helpful for tracing, debugging, and creating diagnostic tools. The following example shows how to use Caller Info attributes. On each call to the TraceMessage method, the caller information is substituted as arguments to the optional parameters.

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

class Program
{
    static void Main(string[] args)
    {
        DoProcessing();
    }

    public static void DoProcessing()
    {
        TraceMessage("Something happened.");
    }

    public static void TraceMessage(string message,
        [CallerMemberName] string memberName = "",
        [CallerFilePath] string sourceFilePath = "",
        [CallerLineNumber] int sourceLineNumber = 0)
    {
        //Trace.WriteLine("message: " + message);
        //Trace.WriteLine("member name: " + memberName);
        //Trace.WriteLine("source file path: " + sourceFilePath);
        //Trace.WriteLine("source line number: " + sourceLineNumber);

        Console.WriteLine("message: " + message);
        Console.WriteLine("member name: " + memberName);
        Console.WriteLine("source file path: " + sourceFilePath);
        Console.WriteLine("source line number: " + sourceLineNumber);
    }
}