StackTrace类方法在发布模式下不起作用 [英] StackTrace class methods not working in release mode
问题描述
下面是我用来记录错误详细信息的代码。
StackTrace sTrace = new StackTrace(true );
字符串函数名称= Environment.NewLine + MethodName- + sTrace.GetFrame(1).GetMethod()。Name;
字符串类名称= Environment.NewLine +文件路径- + sTrace.GetFrame(1).GetFileName()+ Environment.NewLine +行号- + 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));
这在调试模式下很完美,但是在相对模式下,我将函数名和类名作为空白,行号(sTrace.GetFrame(1).GetFileLineNumber())为0。
还有其他最佳方法来获取函数名称,类名称和行
预先感谢。
StackTrace 信息将在调试构建配置中提供最丰富的信息。默认情况下,调试版本包括调试符号,而发布版本则不包含。调试符号包含用于构造StackFrame和StackTrace对象的大多数文件,方法名,行号和列信息。具有程序数据库文件(.pdb)
在发布模式下
属性->构建->定义调试常数(选中)
属性->构建->优化代码(取消选中)
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.
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)
这篇关于StackTrace类方法在发布模式下不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!