Caller Information attributes are newly introduced in Framework 4.0. There are three attributes which are more helpful in tracing, debugging and diagnosis the application.

These attributes will provide some information about the calling method to another method via. Parameters and these parameters should be declared as optional or default parameters.

Following are the caller information attributes

  1. CallerMemberNameAttribute: The parameter with this attribute will be filled with calling method name.
  2. CallerLineNumberAttribute: The parameter with this attribute will be filled with the line number on source file where this method is called.
  3. CallerFilePathAttribute: The parameter with this attribute will be filled with a complete file path at compile time of the class where this method is called.

These attributes are defined in System.Runtime.CompilerServices namespace.

Enough theory, let’s do some real time examples

Following are the sample console application where we are trying to print the data assigned to these parameters.

class Program 
    { 

        static void Main(string[] args) 
        { 

            LogInfo(); 

        }    

        static void LogInfo([System.Runtime.CompilerServices.CallerMemberName] string methodName = "", [System.Runtime.CompilerServices.CallerFilePath] string filepath = "", [System.Runtime.CompilerServices.CallerLineNumber] int lineNumber = 0) 
        { 

            Console.WriteLine("Welcome to TechXposer!"); 

            Console.WriteLine("Method Name: " + methodName); 
            Console.WriteLine("FilePath: " + filepath); 
            Console.WriteLine("Line Numer: " + lineNumber); 
            Console.ReadLine(); 

        } 
    }

In the above sample application, we created a new method called LogInfo method with three parameters and each assigned with three different caller information attributes we discussed above and also these parameters are assigned with default values.

Following is the output window for the above code

// Output:  

// Welcome to TechXposer!.  
// Method Name:  Main  
//FilePath: D:\Sai\CallerInformation\CallerInformation\Program.cs  
// Line Number: 13

As observed in the output window

  • Method Name printed as “Main” because this LogInfo method is called by Main method.
  • A complete source File Path at compile time of the calling method I.e.., Main method is printed. If this LogInfo method is in other class or component the same file path will be printed as it will print calling method.
  • Line Number of the line where this LogInfo method called. As in this case it is 13.

As these are optional parameters, we can override these values with our own data as below.

LogInfo("MainMethod");

Now the output will be as follows.

// Output:  

// Welcome to TechXposer!.  
// Method Name:  MainMethod 
//FilePath: D:\Sai\CallerInformation\CallerInformation\Program.cs  
// Line Number: 13  

As observed, now method Name is printed with the literal given.

Following are the different types of names will be display when using in special methods

  • If this LogInfo method is called in any class constructor then the method name will be “.ctor“.
  • If this LogInfo method is called in any static constructor then the method name will be “.cctor“.
  • If this LogInfo method is called in any destructor then the method name will be “Finalize“.

When Compiler finds these parameters with any of these attributes it will assign respective information and proceeds only when these parameters are default parameters with respective default value.

This Attributes cannot be used on any properties, fields or class as these are targeted to parameters

[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]

Values to these parameters will be assigned in Debug mode as well as in Release mode. So these parameters can be used for any logging purpose too.

Hope this explanation cleared about this concept as I concentrated more on providing technical stuff instead of theoretical.

Happy Coding 🙂