다음을 통해 공유


ASP.NET Error Handling and Tracing

Posted May 29, 2002

Chat Date: May 16, 2002

Chat Participants:

  • Bradley Millington, Program Manager
  • Erik Olson, Program Manager
  • Steve Turley, Software Design Engineer

Erica
Welcome to today's MSDN Chat. Our topic is ASP.NET Error Handling and Tracing. Questions, comments and suggestions are welcome.

Erica
I'll let the hosts introduce themselves:

Erik_MS
Hi, I'm Erik Olson, a program manager on the ASP.NET team.

Bradley_MS
Welcome everyone! My name is Bradley Millington, and I am a Program Manager on the ASP.NET team.

Bradley_MS
I helped design and develop the Trace feature of ASP.NET, and am happy to take any questions you have for that area.

sturley_MS
I'm Steve Turley, a test developer for ASP.NET

Erica
Fire away with your questions!

Erik_MS
Q: orgbrat : When using try-catch, is it better to use nested catch's with one for system exceptions and one for SQL exceptions and how much overhead is used.

Erik_MS
A: I don't know if there's a best way to do it. If you want to handle different exception types discretely, it's better to have separate

Erik_MS
specifiers. If you're just interested in something went wrong, a broader one is probably fine. I'd avoid munching all exceptions unless

Erik_MS
there's a good reasons to do that :-)

Erik_MS
Exception handling incurs very little overhead unless an exception is thrown (i.e. adding the blocks won't kill you at all).

Bradley_MS
Q: chus_MS : Bradley do you can explain to us what is the trace feature of asp.net?

sturley_MS
Q: How do I catch an exception on a global basis (like catching it in main() in C++)

sturley_MS
A: For ASP.NET apps you can use Application_OnError in your global.asax page. The techniques are not exactly comparable but will give you some global error handling

Bradley_MS
A: The Trace feature of ASP.NET allows you to instrument your ASP.NET pages with debugging messages, and then turn those messages on or off using a Page directive or configuration switch: <%@ Trace="True" %>. It also displays some useful information...

Bradley_MS
...about a page, such as Server Variables, Cookie values, Headers, etc.

Bradley_MS
If you just create a simple .aspx page with the directive, you can see what information is presented by default. There is a lot of useful info there! :-)

Bradley_MS
Q: ChiliYago : Where is the best resource to learn Tracing?

Bradley_MS
A: The best place to learn about Tracing quickly is the Quickstart samples included in the .NET Framework SDK. Try this link:

Bradley_MS
https://samples.gotdotnet.com/QuickStart/aspplus/default.aspx?url=/quickstart/aspplus/doc/tracingoverview.aspx

Erik_MS
Q: orgbrat : when you add a custom thrown error, how much cost is there..

Erik_MS
A: I think you're OK with that. I don't think there's any appreciable difference between throwing System.Exception and a derived type.

Erik_MS
You don't want to throw exceptions if you don't need them (e.g. don't use them as return codes).

Erik_MS
Q: lepard : When I deploy my apsx page via xcopy I get a runtime compile error on my destination page

Erik_MS
Q: lepard : When I open this project in devenv it compiles ok - what's wrong?

Erik_MS
A: There are lot of things that could be going on here. Do you rely on types that aren't on the target machine?

Erik_MS
Is it something to do with ACLs, etc.? Do you know specifically what's failing?

sturley_MS
Q: When will the Exception Handling Application Block for .NET RTM be released?

sturley_MS
A: I don't understand, can you be more explicit?

Erik_MS
Q: (additional - the errors it reports are things like can't find function left)

Erik_MS
A: Hmm, it sounds like you're missing a reference to an assembly or an import directive. Where is left defined? (done)

Erik_MS
Q: lepard : It says there is an error in the code behind page - And reports errors in built-in functions!

Erik_MS
A: Unfortunately, I haven't seen that. I'm stumped :-(

Erik_MS
Q: BANGGROUP : How can we raise Application_Error in VB?

Erik_MS
A: It should be called whenever there's an unhandled exception. You just need to 'Throw New Exception("Hello")' or whatever

Erik_MS
and if you have a handler defined, it should get called. (done)

Erik_MS
Q: orgbrat : if you are using a try-catch-finally and perform a return false prior to the catch's will the code always reruen false and then hit the finally any way..

Erik_MS
A: Yes, your finally block should always run.

sturley_MS
Q: Do we have the exception information within the scope of the application_OnError event? Is it in the EventArgs ?

sturley_MS
A: The error is in Context.Error or Server.GetLastError. The exception info is lost by the time the user is redirected to your error page. You need to capture it in Page_Error or Application_Error, save it in the Session if needed, process it, and then do

sturley_MS
a Server.Transfer to your error page.

Bradley_MS
Q: dotnetguy : Can the trace page be brought by some command in the IDE instead of typing the URL in the browser?

Bradley_MS
A: The only way to invoke Tracing is to request a page that has Tracing turned on. By default, the actual Tracing information displays inline within the requested page's rendering. It can also be viewed separately using a web-based "Trace Viewer".

Bradley_MS
It is possible to turn off the inline rendering of Trace so that browsers of your web-site don't see the trace information, but you can. Be warned, however, that tracing should only be turned on while debugging, as it does incur a performance impact...

Bradley_MS
...to your site. (done)

sturley_MS
Q: how do we catch ASP.NET errors if we use custom error page? After configured in the web.config, can the error still be catched and logged within that custom page?

sturley_MS
A: See my previous answer

Erik_MS
Q: BANGGROUP : Do you recommend we throw new exception instead return the error code and error message in the application function?

Erik_MS
A: It sort of depends. Throwing an exception is more expensive than a return code. If it's some truly exceptional case, that's not a bad way

Erik_MS
to do it. If it's a binary yes/no question, for example, I'd prefer a return code personally.(done)

Bradley_MS
The Trace Viewer is invoked using a special URL Trace.axd, within your app.

Erik_MS
Q: durayakar : Do we have the exception information within the scope of the application_OnError event? Is it in the EventArgs ?

Erik_MS
A: It's Server.GetLastError (or Context.Error) (done)

Erik_MS
A: CarlP_MVP : How come in the catch block, I can't use a response.redirect with a True second parm? I get a thread aborted error, but if I put the rediect after the catch block, then it works...

Erik_MS
A: Response.Redirect(string, true) actually throws a ThreadAbortException by design. That's how we unwind the stack to terminate the

Erik_MS
response. If you don't want it to throw, do Redirect(string, false). Or did I misunderstand? (done)

Erik_MS
Q: CarlP_MVP : Erik - no you did not misunderstand. I wasn't aware that is how it unwinds the stack. Makes sense now.

Erik_MS
A: FWIW, Redirect(string) is the same as Redirect(string, true). If you structure your code to deal with request execution continuing,

Erik_MS
you can use Redirect(string, false) all the time and not have to worry about those mystery exceptions :-) (done)

Erik_MS
Q: durayakar : is Exception object serializable ?

Erik_MS
A: Yes, it is. Most (if not all) of the derived types should be as well. (done)

Erik_MS
Q: durayakar : Server.Transfer(string,true) throws an exception too, same reason ?

Erik_MS
A: Yes, that's right. It throws to unwind the stack on the current request (that's how it's aborted) (done)

Erik_MS
Q: durayakar : Then is there any public component that catches and persists the errors to a given directory or database? if not I am definitely starting to design it tomorrow afternoon

Erik_MS
A: There isn't (that I'm aware of anyway). ASP.NET catches the ThreadAbortException internally and does cleanup. For other exceptions,

Erik_MS
a reasonable thing to do is handle Application_OnError (e.g. in global.asax or from an IHttpModule derived class) and log errors to the

Erik_MS
event log or a database (or whatever). I think centralizing that for your app is a great idea! (done)

sturley_MS
Here's a simple way to capture the whole error stack:

sturley_MS
if (e2 == null) { Response.Write ("Session-passed Exception is NULL"); } else {

sturley_MS
string stack = ""; int i = 0;

sturley_MS
while (e2 != null) { i++; stack = "stack part #" + i + ": " + e2.Message + "
\r\n" + e2.StackTrace + "

\r\n" + stack; e2 = e2.InnerException; } Response.Write ("Session-passed Exception:

sturley_MS
Response.Write ("Session-passed Exception:

\r\n" + stack); <End> }

Erik_MS
Q: lepard : (slightly off topic) - (snip)

Erik_MS
A: Sorry, this group is pretty clueless about Exchange unfortunately. Sorry we can't help out there!

Bradley_MS
So is anyone currently using the Trace feature of ASP.NET and have suggestions for features they'd like to see in a future release?

Erik_MS
A: What kind of errors are they? By default, when the ASP.NET processModel cycles reactively

Erik_MS
Q: lepard : I consistently end up getting aspnet_wp.exe errors in my event log - what could be causing this?

Erik_MS
A: What kind of errors are they? By default, when the ASP.NET processModel cycles reactively

Erik_MS
E.g. a crash or a timeout or a deadlock or exceeding the memory threshold, aspnet_wp is recycled

Erik_MS
and an event is logged. You can control whether or not you see those errors with the tag.

Erik_MS
If you're consistently reactively cycling your process, though, you should investigate it and find out why. Perhaps you

Erik_MS
need a higher memory threshold. (done)

sturley_MS
Q: FrogBody : in implementing a IsNumeric() function in C# I use try/catch and try to cast the str to an int and if it works I return true, catch false... is this an acceptable way of using try/catch or am I incurring a huge overhead when there's a better way?

sturley_MS
A: try - catch blocks have no overhead if there are no exceptions, but pretty heavy overhead if there is.

sturley_MS
Using try-catch is not an efficient way to handle expected errors.

Erik_MS
Q: lepard : usually its reporting deadlock

Erik_MS
A: OK, here's the deal with deadlock: if you have a pending request requests in the queue that don't return for more than 3 minutes

Erik_MS
ASP.NET thinks that you're deadlock and restarts the process. You can up the limit if you want (responseDeadlockInterval) or set

Erik_MS
it to "infinite" if you want it out of your way. (done)

Erik_MS
Q: CarlP_MVP : I've used the trace feature a lot, but sometimes it forget to turn it off for a given page. Is there a way to turn off globally? CarlP_MVP : per application? per machine

Erik_MS
A: You can turn it off with config but page directives take precedence. If you only turn it on with config,

Erik_MS
you can disable it that way and use a location directive e.g. ...stuff... in machine.config

Erik_MS
to make sure it can't be overridden with config. (done)

Erik_MS
Q: CarlP, you can use the global tracing instead of each page, it seems

Erik_MS
A: I *think* you can use to point at a given handler with config. (done)

Bradley_MS
Q: lepard : Is there a way to log trace information for a release system so the user sees a friendly error but the trace info is saved for the developer to use?

Bradley_MS
A: You can turn on global tracing and then view the trace info from the trace.axd viewer: Because pageOutput property is false, the clients of your website will not see...

Bradley_MS
... the trace messages.

Bradley_MS
Saving trace information to a database or file is not supported however. This is definitely something we will consider for a future relase.

Erik_MS
Q: CarlP_MVP : suggested feature - create a TODO task for all pages with trace turned on...

Erik_MS
A: That is a great idea--thanks for the suggestion!

Erica
Thanks for joining us today! You've asked some great questions. Unfortunately, it's time to go. You can find the transcript of this chat soon on the MSDN Web site at https://msdn.microsoft.com/chats/recent.asp

Erik_MS
Thanks much for attending! You can also visit www.asp.net to find lots of links and resources on ASP.NET.

Erica
You can also find development resources for ASP.NET at https://msdn2.microsoft.com/en-us/asp.net/default.aspx.

Bradley_MS
Thanks for the questions all. Be sure to visit www.asp.net!!

Top of PageTop of Page