c# 拷贝代码 取消行号_Sairama今日有趣的代码-在调试C#时打印行号

c# 拷贝代码 取消行号

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()) ; }

翻译自: https://www.hanselman.com/blog/sairamas-interesting-code-of-the-day-printing-line-numbers-while-debugging-c

c# 拷贝代码 取消行号

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值