Using Caller Information Attributes In C#

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

These attributes will provide some information about the calling method to another method via parameters; however, 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 see some real time examples.

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

  1. class Program {  
  2.     static void Main(string[] args) {  
  3.         LogInfo();  
  4.     }  
  5.     static void LogInfo([System.Runtime.CompilerServices.CallerMemberName] string methodName = "", [System.Runtime.CompilerServices.CallerFilePath] string filepath = "", [System.Runtime.CompilerServices.CallerLineNumber] int lineNumber = 0) {  
  6.         Console.WriteLine("Welcome to c# Corener!");  
  7.         Console.WriteLine("Method Name: " + methodName);  
  8.         Console.WriteLine("FilePath: " + filepath);  
  9.         Console.WriteLine("Line Numer: " + lineNumber);  
  10.         Console.ReadLine();  
  11.     }  
  12. }  

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. Also, these parameters are assigned with default values.

Following is the output window for the above code.

  1. // Output:  
  2. // Welcome to c# Corener!.  
  3. // Method Name: Main  
  4. //FilePath: D:\Sai\CallerInformation\CallerInformation\Program.cs  
  5. // Line Number: 13  

As observed in the output window.

  • The method name is printed as "Main" because this LogInfo method is called by the Main method.
  • A complete source file path at compile time of the calling method; i.e., the Main method, is printed. If this LogInfo method is in another class or component, the same file path will be printed as it will print the 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.

  1. LogInfo("MainMethod");  

Now, the output will be as below.

  1. // Output:  
  2. // Welcome to c# Corener!.  
  3. // Method Name: MainMethod   
  4. //FilePath: D:\Sai\CallerInformation\CallerInformation\Program.cs  
  5. // Line Number: 13  

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

Following are the different types of names to be displayed 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 the compiler finds these parameters with any of these attributes, it will assign the respective information and proceed only when these parameters are default parameters with the respective default value.

This attribute cannot be used on any properties, fields, or classes 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 up this concept as I concentrated more on providing technical information instead of theoretical.

Happy coding.