c# 拷贝代码 取消行号
In C++ if problems happen, we'll want to log the error along with filename and Line Numbers, often like:
在C ++中,如果发生问题,我们将要记录错误以及文件名和行号,通常如下:
CString strCompleteMessage;strCompleteMessage.Format(_T("%s , HRESULT %0x [%s,%d]"),strMessage,hr,A2W(__FILE__),__LINE__);LogInfo(strCompleteMessage);
CString strCompleteMessage; strCompleteMessage.Format(_T(“%s,HRESULT%0x [%s,%d]”),strMessage,hr,A2W(__ FILE __),__ LINE __); LogInfo(strCompleteMessage);
In C#, there are no such macros so we use the StackFrame class. Below we show how to print the filename and line numbers while logging messages/errors.
在C#中,没有这样的宏,因此我们使用StackFrame类。 下面我们显示在记录消息/错误时如何打印文件名和行号。
public static void foo() { // some operations here // some error here string msg = "Unable to do xyz operation, Please report this to abc@xyz.com";
公共静态无效foo() { //这里的一些操作//这里有些错误字符串msg = “无法执行xyz操作,请将此信息报告给abc@xyz.com” ;
// true means get the file information also ( needs pdb files which can be generated for Release builds )
// true表示还获取文件信息(需要可以为Release版本生成的pdb文件)
StackFrame CallStack = new StackFrame(0, true); Console.WriteLine("Error:{0} occurred in:{1} in File: {2} at Line: {3}", msg, CallStack.GetMethod(), CallStack.GetFileName(), CallStack.GetFileLineNumber() );}
StackFrame CallStack = new StackFrame(0, true ); Console.WriteLine( “错误:{0}发生在:{1}在文件中:{2}在第{3}行:” , msg,CallStack.GetMethod(),CallStack.GetFileName(),CallStack.GetFileLineNumber()) ; }
c# 拷贝代码 取消行号