error handling - Is there a way of showing variable values in an global ASP.Net exception? -


i have global error trap in application_error method of global.asax writes unhandled exception log file.

is there anyway of reporting variable names , values part of exception report?

unless tricky aspect-oriented programming, pretty need make sure manually introduce relevant information stack trace when exceptions thrown. example:

public void dosomething(int number, string name) {     try     {         ...     }      catch (exception e)     {         throw new exception("error occurred while doing something: " +                             new {number, name}, e);     } } 

this way, number , name included in stack trace when exception trickles top level.

edit

after reading david stratton's answer, felt need expand on little. sense experienced c# programmers haven't learned of tricks i've learned.

first of all, wanted point out exception-handling system in .net designed idea of innerexception purpose (providing additional information @ various points of stack trace), , not @ hacky do. however, should provide exception innerexception constructor parameter, rather appending e.tostring() new exception's message.

secondly, based on various comments , answers i've read on stackoverflow, own experience, it's best to:

  1. avoid catching exception if there's nothing specific plan it.
  2. when catch exception, rethrow unless know why exception thrown , you're in scope know how gracefully retreat trying do. pretending nothing went wrong asking more trouble down road.
  3. when re-throwing exceptions, either throw; preserve original exception's stack trace, or include original exception new exception's innerexception.
  4. consistently log exceptions don't re-thrown. happen @ ui level, can tell user unexpected happened rather allowing program crash.

finally, wanted mention anonymous type declaration syntax ideal kind of thing because concise , automatically produce string uses given variable names , values. example, new {number, name}.tostring() might produce "{ number = 1, name = test }".


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -