开发者

Problem with Environment.StackTrace

On a thread that is processing new data in the system, if the data is invalid I write a message in the event log, containing the Environment.StackTrace information.

Writing in the event log throws an exception with no text message

Message: 
CallStack -    at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
       at System.Environment.get_StackTrace()
       at <my method that writes in the event log>

Any ideas why this ha开发者_如何学JAVAppens?

EDIT: I am interested in what can cause the Environment.StackTrace to throw exception in general, so that i can understand what is happening in my case


@anchandra I don't know if you have figured this out since, as this is now 4 years old.

However I would like to add as I just stumbled upon this myself.

So, why does Environment.StackTrace throws an exception?

First the most uninteresting answer: As you can see in the MSDN reference to the property, it can throw an ArgumentOutOfRangeException if "The requested stack trace information is out of range.", note also that the executing context must have System.Security.Permissions.EnvironmentPermission.

Now what stumped me for a second: It does NOT throw an exception! Yes, that is what was happening to me, it actually returned me a Stack Trace that started listing the call to Environment.StackTrace like "at System.Environment.get_StackTrace()" then it showed all the other calling methods. Because I was logging this into an audit log, if you look at it you assume there is an exception occurring at the last frame of the stack, but that's not true, I was just happening to request a Stack Trace at that point and stick it in my error log, very dumb once I realized this.


You need to capture the stacktrace before moving onto a separate thread.

Stacktrace will only show you the frames up to the root of the thread.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜