4

below is my piece of code which i am using to log my error details.

StackTrace sTrace = new StackTrace(true);
string functionname = Environment.NewLine + " MethodName - " + sTrace.GetFrame(1).GetMethod().Name;
string classname = Environment.NewLine + " File Path - " + sTrace.GetFrame(1).GetFileName() + Environment.NewLine + " Line No. - " + sTrace.GetFrame(1).GetFileLineNumber() + Environment.NewLine + " ClassName - " + sTrace.GetFrame(1).GetMethod().ReflectedType.Name + Environment.NewLine + " DateTime - " + DateTime.Now.ToString();
WriteLine(string.Concat("ERROR: ", errMsg, classname, functionname,
                                 Environment.NewLine));

this works perfect in debug mode, but in relese mode, i am getting function name and class name as blank, Line Number (sTrace.GetFrame(1).GetFileLineNumber()) as 0.

is there any other best way to get function name, class name, and line number from where error originated.

thanks in advance.

Chethan
  • 298
  • 1
  • 4
  • 17
  • 1
    Look at the documentation of `StackFrame`. There you can see that most debug information is created only in assemblies build with debug configuration. – Fratyx Oct 10 '14 at 04:44
  • Try breakpointing on Release, and see on the immediate window the problem. – DevEstacion Oct 10 '14 at 04:56
  • possible duplicate of [Display lines number in Stack Trace for .NET assembly in Release mode](http://stackoverflow.com/questions/628565/display-lines-number-in-stack-trace-for-net-assembly-in-release-mode) – Carbine Oct 10 '14 at 05:08

3 Answers3

3

StackTrace information will be most informative with Debug build configurations. By default, Debug builds include debug symbols, while Release builds do not. The debug symbols contain most of the file, method name, line number, and column information used in constructing StackFrame and StackTrace objects.

Actually, Release mode optimize code and dose not have Program DataBase file(.pdb)

In Release mode

Property -> Build -> Define Debug Constant (Check it)

Property -> Build -> Optmize Code (UnCheck it)

spottedmahn
  • 14,823
  • 13
  • 108
  • 178
jiten
  • 5,128
  • 4
  • 44
  • 73
1

You can get it if you are using .pdb files in your application. Please check this

You can enable it : Properties > Linker > Debugging > Generate Debug Info = "Yes"

A Note on pdb files

spottedmahn
  • 14,823
  • 13
  • 108
  • 178
Zigma
  • 529
  • 6
  • 37
1

Here is the screenshot - https://i.stack.imgur.com/ryREI.jpg

Properties-->Build-->Release conf-->Adv->Debug infor ( FULL).

Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48