Help Yourself in Debugging (Part-3) StackTrace and Caller Information attribute

This post is about two feature in C# Caller Information Attribute and StackTrac class , which are helpful to get information at runtime from where the call came i.e. both provide information about caller. As purpose of both is 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 start with StackTrace and Caller information following image shows structure of the code.

As you can see in code it create 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 get created GetFrames method is usd to get information about each frame(each method call is represented by one frame) and finally each frame details is appeded using StringBuilder, then it displayed by front end.

Following is output get recieved once the code run

So output prints each call made from one layer to antoher if you see above code call is coming from the fron layer to database layer.

Advatage

StackTrace provide detail informaiton about from where call came from the beggining till end. This is one resason devloper can use StackTrace when there is need to get i.e. trace details level information.

Also StackTrace frame also provide control on the caller method, provide assebmbly from where call is coming.

Disadvantage

Inline method is not get listed when code compiled in Relase mode, which is get listed when code compiled in Debug mode.

To understand it cosider below code changes in front layer code.

Now if you compile code in Debug mode with below configuration

It will generate below output for you , where you can see four line i.e. on 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 line. The line of calling method "GetEmployeeList1" is missing.

So StackTrace doesn't list out methods which is converted to inline method by compiler. if you want to list out you can mark method as [MethodImpl(MethodImplOptions.NoInlining)]

if devloper mark method as [MethodImpl(MethodImplOptions.AggressiveInlining)] than the method also no get list in debug .
So StackTrace information can be spoof by making method inline.

As StackTrace provide every minute detail, StackTrace will show the full trace right into the core MS
sourced assemblies, and will reveal details about what technologies
you're using, and possible versions as well. This gives intruders
valuable info on possible weaknesses that could be exploited. So always careful before displaying this information and put security on this.

Advantage over Caller Information attribute

It provides minute details which is not possible with Caller Inormation attribute.

Line number, Filename and Method Name information is given by StackFrame which cannot be changed by devloper But in case of Caller Information attribute devloper can spoof by passing wrong value in caller information parameter.

Caller Information attribute

This is new feature introduce in C# 5.0. and attributes are part of System.Runtime.CompilerServices namespace. These attributes required to be added as optional parameter in the method for which need to get caller information.
Example