How to get a dump of all local variables?

后端 未结 3 628
旧巷少年郎
旧巷少年郎 2020-12-08 15:34

How can I get a dump of all local & session variables when an exception occurs? I was thinking of writing some sort of reflection based function that would interrogate t

3条回答
  •  半阙折子戏
    2020-12-08 16:22

    You should not use Exception handling in Try Catch form. Rather, it should be

    1. Page Level Error
    2. Application Level error

    Suppose You have a Presentation Layer and a Business Logic Layer/DataAccess Layer.

    Upon facing the error in say Business Logic, it will move directly to Glogal.asax.cs file under Application_Error Event without going back to the calling function. Here you can log the error message like below....

    HttpContext.Current.Server.GetLastError().InnerException.StackTrace
    HttpContext.Current.Server.GetLastError().InnerException.Message
    HttpContext.Current.Server.GetLastError().InnerException.Source
    HttpContext.Current.Server.GetLastError().InnerException.TargetSite.DeclaringType.FullName
    HttpContext.Current.Server.GetLastError().InnerException.TargetSite.DeclaringType.Name
    HttpContext.Current.Server.GetLastError().InnerException.TargetSite.DeclaringType.Namespace
    

    In case of page level error, Priority is the Page OnError Override and finally the Application Level error event. here also you can log errors.

    I will prefer Application_error handler because If you have 20 modules and a situation come when you need to create baseclass for each module. It is not good to make code redundancy.

    Now in the Web Config you can write code to redirect the user on some default page like below.

    
       
    
    

提交回复
热议问题