Reputation: 300
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.
Upvotes: 4
Views: 4196
Reputation: 529
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
Upvotes: 1
Reputation: 5264
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)
Upvotes: 3
Reputation: 7462
Here is the screenshot - https://i.sstatic.net/ryREI.jpg
Properties-->Build-->Release conf-->Adv->Debug infor ( FULL).
Upvotes: 1