详细异常消息(C#) [英] Verbose Exception Message (C#)
问题描述
我已经设置了一些代码来在我的C#Web应用程序中记录错误,这很好用,但是我想要一些额外的信息.
当我在Web浏览器中查看异常时,它会向我显示引发错误的行号,并且通常是周围代码的一些摘录.
在我的日志中,此消息丢失.
我试过了:
Hi,
I have setup some code to log errors in my c# web app which works great but I want some extra info.
When I view an exception in the web browser it shows me the line number that threw the error and usually a little excerpt of the surrounding code.
In my logs this message is missing.
I have tried:
ex.Message
ex.Message
ex.Source
ex.TargetSite
ex.StackTrace
Environment.StackTrace
这构成了异常的大多数信息,但是我不知道要获得消息的一部分,而行号和代码摘录正是我真正需要的.
有人知道如何访问此信息吗?我一直在尝试搜索此信息,但找不到任何涵盖它的信息.
This makes up most of the information of the exception but I don''t know to get the part of the message with the line number and excerpt of code which to be honest is all I really need.
Anyone know how to access this info? I have been trying to google this info but cant find anything that covers it.
推荐答案
您需要的是System.Exception.Stack
,以一个字符串的形式.另外,通常应该递归获取System.Exception.InnerException
(因为InnerException
,如果不为null,也是Exception
).要获得更多,请使用System.Environment.StackTrace
.创建StackTrace
的实例以及捕获异常的位置;您可以将堆栈帧(一次不需要全部)或一次全部堆栈起来;分别使用GetFrame
或GetFrames
.
参见System.Exception
:
http://msdn.microsoft.com/en-us/library/system.exception.aspx [ ^ ].
System.Diagnostics.StackTrace
:
http://msdn.microsoft.com/en-us/library/system.diagnostics. stacktrace.aspx [ ^ ].
如果您已经了解所有相关的班级和成员,我不知道您怎么看不到所有这些可能性.
—SA
What you need isSystem.Exception.Stack
, in the form of one string. Also, you should generally getSystem.Exception.InnerException
, recursively (becauseInnerException
, if not null, is alsoException
). To get a lot more, useSystem.Environment.StackTrace
. Create an instance ofStackTrace
and the point where an exception was caught; you can get the stack frames on-by-one (good when you don''t need them all) or all at once; useGetFrame
orGetFrames
, respectively.
SeeSystem.Exception
:
http://msdn.microsoft.com/en-us/library/system.exception.aspx[^].
System.Diagnostics.StackTrace
:
http://msdn.microsoft.com/en-us/library/system.diagnostics.stacktrace.aspx[^].
I have no idea how you failed to see all that possibilities if you already knew all the relevant classes and members.
—SA
这篇关于详细异常消息(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!