Read the below post related to the same topics:
- Help yourself in Debugging by using Call Stack and Immediate Window
- Help Yourself in Debugging (Part-2) using Breakpoint/Tracepoint
This post is about two features in C# Caller Information Attribute and StackTrace
class, which are helpful to get information at runtime from where the call came, i.e., both provide information about caller. As the purpose of both is the same but there is significant difference between both of them. Information provided by both can be used by developer application to provide trace information.
Before starting with StackTrace
and Caller information, the following image shows structure of the code.
So the project is divided in three layers, UI, Business and DataLayer.
Front Layer Code
class Program
{
public int i = 10;
public static void Main(string[] args)
{
try
{
var str = (new BusinessEmployee()).GetEmployeeList();
Console.WriteLine(str);
Console.ReadLine();
}
catch (Exception ex)
{
global::System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
}
As you see in the above code, the Font layer is calling "GetEmployeeList
" method to get a list of all employees.
BusinessLayer Code
public class BusinessEmployee
{
private readonly DataEmployee dataEmployee;
public BusinessEmployee()
{
dataEmployee = new DataEmployee();
}
public void GetEmployeeList()
{
dataEmployee.GetEmployeeList();
}
}
In this layer, Business class is calling DataLayer
to get employee list from the database.
DataLayer Code
public class DataEmployee
{
public void GetEmployeeList()
{
}
}
This layer returns employee list, but for the example, it's returning exception.
StackTrace
Class in C# part of System.Diagnostics
namespace which provides information same as Call Stack window (Read more about Call stack window ) at runtime.
To understand it in a better way, let's consider change in the above code in DataLayer
.
public string GetEmployeeList()
{
StackTrace stackTrace = new StackTrace(true);
StringBuilder sb = new StringBuilder();
foreach (StackFrame frame in stackTrace.GetFrames())
{
sb.AppendLine(" Method Name: " + frame.GetMethod().Name +
" File Name:" + frame.GetMethod().Module.Name + " Line No: " +
frame.GetFileLineNumber());
}
return sb.ToString();
}
As you can see in code, it creates StackTrace
class and when creating new object of class "true
" is passed as argument to the constructor of StackTrace
class for capturing the file name, line number, and column number.
After the object gets created, GetFrames
method is used to get information about each frame (each method call is represented by one frame) and finally each frame detail is appended using StringBuilder
, then it is displayed by front end.
Following is the output that gets received once the code runs:
So output prints each call made from one layer to another if you see the above code call is coming from the front layer to database layer.
Advantage
- StackTrace provides detailed information about from where call came from the beginning till end. This is one reason developer can use StackTrace when there is a need to get, i.e., trace details level information.
- Also StackTrace frame provides control on the caller method, provides assembly from where call is coming.
Disadvantage
- Inline method does not get listed when code is compiled in Release mode, which gets listed when code is compiled in Debug mode.
To understand it, consider the below code changes in front layer code.
Now if you compile code in Debug mode with below configuration:
It will generate the below output for you, where you can see four lines, i.e., one extra line of calling to method "GetEmployeeList1
".
So debug list all method calls, but now if you compile code in Release mode with below configuration:
It will generate below output for you, where you can see three lines. The line of calling method "GetEmployeeList1
" is missing.
So StackTrace doesn't list out methods which are converted to inline method by compiler. if you want to list out, you can mark method as [MethodImpl(MethodImplOptions.NoInlining)]
.
If developer marks method as [MethodImpl(MethodImplOptions.AggressiveInlining)]
, then the method also does not get list in debug.
So StackTrace information can be spoof by making method inline.
Advantage Over Caller Information Attribute
- It provides minute details which is not possible with Caller Information attribute.
- Line number, Filename and Method Name information is given by StackFrame which cannot be changed by developer. But in case of Caller Information attribute developer can spoof by passing wrong value in caller information parameter.
Caller Information Attribute
This is a new feature introduced in C# 5.0. and attributes are part of System.Runtime.CompilerServices
namespace. These attributes are required to be added as optional parameter in the method for which you need to get caller information.
Example
Accessibility ReturnType MethodName (Type parameterName,....,
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0
Name | Type | Description |
CallerMemberName | string | Provide caller method Name |
CallerFilePath | string | Provide caller method File Name |
CallerLineNumber | int | Provide caller Line number in File from where method gets called |
Note
Caller of the method can also pass this value as this attribute is added as parameter to method.
Example when calling method which is decorated by caller information attribute:
To understand this, let's make a change in the DataLayer
method like this:
public string GetEmployeeList([CallerMemberName] string memberName = "",
[CallerFilePath] string fileName = "",
[CallerLineNumber] int lineNumber = 0)
{
return " Method Name: " + memberName + " File Name:" + fileName + " Line No: " + lineNumber;
}
So running the above code provides the below output:
Advantage
- Information of caller attribute cannot be spoofed till developer passes information in method parameter.
- There is no runtime cost for this, i.e., it doesn't affect performance of the code as attribute are compile time.
- It is very helpful to find out when call is coming from some unknown location like in case of "
PropertyChange
".
Disadvantage
- Information of caller can be spoofed by developer if developer passes wrong information in the parameter of the caller information.
- There is only one
StackFrame
, i.e., it will give information about who called method (immediate caller of method) but doesn't provide detailed information like StackTrace
. - It's part of C# 5.0 so it's not working with older version of framework.
Advantage over StackTrace
- As its complied time, there is no rutime cost like
StackTrace
(we need to create object of StackTrace
runtime to get information). - Information provided by attribute cannot be spoofed by method attributes.
Do comment if the information is missing or wrong.
Referenced From