StackTrace class methods not working in release mode

ぃ、小莉子 提交于 2019-12-10 19:09:46

问题


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.


回答1:


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)




回答2:


Here is the screenshot - http://imgur.com/Oet3SED

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




回答3:


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



来源:https://stackoverflow.com/questions/26292033/stacktrace-class-methods-not-working-in-release-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!