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 the calling function & create a dump of variables & values.
当异常发生时,如何获得所有本地和会话变量的转储?我想写一些基于反射的函数来询问调用函数并创建一个变量和值的转储。
Is there an existing library that I can use?
我可以使用现有的库吗?
UPDATE
更新
After speaking to a colleague, I was pointed to AOP or Aspect Oriented Programming. Here is what I understand ... Using AOP, one would simple decorate the methods & classes with certain attributes. AOP framework then injects code in or around these classes & methods. There are two separate kinds of framework, one that injects code & then compiles the assembly & the second simply uses reflection & traps the call which you have decorated and wraps whatever code around the method at runtime.
在与同事交谈后,我被指面向AOP或面向方面的编程。这是我所理解的……使用AOP,可以简单地用某些属性来修饰方法和类。然后AOP框架向这些类和方法中或周围注入代码。有两种不同的框架,一种是注入代码,然后编译程序集,另一种是简单地使用反射和陷阱调用,你在运行时修饰和包装方法周围的任何代码。
I hope all that makes sense. I will be doing more research on this & post my approach.
我希望这一切都说得通。我将在这方面做更多的研究,并发布我的方法。
Thanks guys ...
谢谢大家……
3 个解决方案
#1
11
I'm not sure if this is what you're looking for. But if you're in a catch-block you can get all fields and properties of this class in the following way:
我不确定这是不是你要找的。但是,如果您处于一个控制块中,您可以通过以下方式获得该类的所有字段和属性:
try
{
double d = 1 / 0;
}
catch (Exception ex)
{
var trace = new System.Diagnostics.StackTrace();
var frame = trace.GetFrame(1);
var methodName = frame.GetMethod().Name;
var properties = this.GetType().GetProperties();
var fields = this.GetType().GetFields(); // public fields
// for example:
foreach (var prop in properties)
{
var value = prop.GetValue(this, null);
}
foreach (var field in fields)
{
var value = field.GetValue(this);
}
foreach (string key in Session)
{
var value = Session[key];
}
}
I've showed how to get the method name where the exception occured only for the sake of completeness.
我已经演示了如何获取异常发生的方法名称,这只是为了完整性。
- Type.GetProperties Method
- 类型。getproperty方法
- Type.GetFields Method
- 类型。GetFields方法
- PropertyInfo.GetValue Method
- PropertyInfo。GetValue方法
- FieldInfo.GetValue Method
- FieldInfo。GetValue方法
- StackTrace Class
- 加类
With BindingFlags you can specify constraints, for example that you only want properties of this class and not from inherited:
使用BindingFlags,您可以指定约束,例如,您只想要这个类的属性,而不是继承的:
Using GetProperties() with BindingFlags.DeclaredOnly in .NET Reflection
与BindingFlags使用getproperty()。DeclaredOnly在。net反射
Of course the above should give you only a starting-point how to do it manually and you should encapsulate all into classes. I've never used it myself so it's untested.
当然,上面的内容应该只提供如何手动完成的起点,并且应该将所有内容封装到类中。我自己从来没有用过,所以没有经过测试。
#2
1
You should not use Exception handling in Try Catch form. Rather, it should be
您不应该在Try Catch表单中使用异常处理。相反,它应该是
- Page Level Error
- 页面级别的错误
- 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....
当面对业务逻辑中的错误时,它将直接移动到Glogal.asax。cs文件在Application_Error事件下,不返回调用函数。在这里你可以登录下面的错误消息像....
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.
如果出现页面级错误,优先级是页面OnError Override,最后是应用程序级错误事件。这里也可以记录错误。
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.
我更喜欢Application_error处理程序,因为如果您有20个模块,当您需要为每个模块创建baseclass时,就会出现这种情况。使代码冗余是不好的。
Now in the Web Config you can write code to redirect the user on some default page like below.
现在,在Web配置中,您可以编写代码,在如下所示的默认页面上重定向用户。
<customErrors defaultRedirect="ErrorPage.htm" mode="On">
<error statusCode="404" redirect="ErrorPageNotFound.htm"/>
</customErrors>
#3
-1
This is a question asked ad nauseum on Stack Overflow, although phrased differently. In one thread, the answer was to use PostSharp. As others have suggested dumping the stack trace, you can do that. The easiest would be to manually dump the local variables. This can either be to Trace or you can create your own custom exception handler.
这是一个在Stack Overflow上令人作呕的问题,尽管措辞不同。在一条线索中,答案是使用PostSharp。正如其他人建议转储堆栈跟踪一样,您可以这样做。最简单的方法是手动转储本地变量。这可以是跟踪,也可以创建自己的自定义异常处理程序。
#1
11
I'm not sure if this is what you're looking for. But if you're in a catch-block you can get all fields and properties of this class in the following way:
我不确定这是不是你要找的。但是,如果您处于一个控制块中,您可以通过以下方式获得该类的所有字段和属性:
try
{
double d = 1 / 0;
}
catch (Exception ex)
{
var trace = new System.Diagnostics.StackTrace();
var frame = trace.GetFrame(1);
var methodName = frame.GetMethod().Name;
var properties = this.GetType().GetProperties();
var fields = this.GetType().GetFields(); // public fields
// for example:
foreach (var prop in properties)
{
var value = prop.GetValue(this, null);
}
foreach (var field in fields)
{
var value = field.GetValue(this);
}
foreach (string key in Session)
{
var value = Session[key];
}
}
I've showed how to get the method name where the exception occured only for the sake of completeness.
我已经演示了如何获取异常发生的方法名称,这只是为了完整性。
- Type.GetProperties Method
- 类型。getproperty方法
- Type.GetFields Method
- 类型。GetFields方法
- PropertyInfo.GetValue Method
- PropertyInfo。GetValue方法
- FieldInfo.GetValue Method
- FieldInfo。GetValue方法
- StackTrace Class
- 加类
With BindingFlags you can specify constraints, for example that you only want properties of this class and not from inherited:
使用BindingFlags,您可以指定约束,例如,您只想要这个类的属性,而不是继承的:
Using GetProperties() with BindingFlags.DeclaredOnly in .NET Reflection
与BindingFlags使用getproperty()。DeclaredOnly在。net反射
Of course the above should give you only a starting-point how to do it manually and you should encapsulate all into classes. I've never used it myself so it's untested.
当然,上面的内容应该只提供如何手动完成的起点,并且应该将所有内容封装到类中。我自己从来没有用过,所以没有经过测试。
#2
1
You should not use Exception handling in Try Catch form. Rather, it should be
您不应该在Try Catch表单中使用异常处理。相反,它应该是
- Page Level Error
- 页面级别的错误
- 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....
当面对业务逻辑中的错误时,它将直接移动到Glogal.asax。cs文件在Application_Error事件下,不返回调用函数。在这里你可以登录下面的错误消息像....
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.
如果出现页面级错误,优先级是页面OnError Override,最后是应用程序级错误事件。这里也可以记录错误。
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.
我更喜欢Application_error处理程序,因为如果您有20个模块,当您需要为每个模块创建baseclass时,就会出现这种情况。使代码冗余是不好的。
Now in the Web Config you can write code to redirect the user on some default page like below.
现在,在Web配置中,您可以编写代码,在如下所示的默认页面上重定向用户。
<customErrors defaultRedirect="ErrorPage.htm" mode="On">
<error statusCode="404" redirect="ErrorPageNotFound.htm"/>
</customErrors>
#3
-1
This is a question asked ad nauseum on Stack Overflow, although phrased differently. In one thread, the answer was to use PostSharp. As others have suggested dumping the stack trace, you can do that. The easiest would be to manually dump the local variables. This can either be to Trace or you can create your own custom exception handler.
这是一个在Stack Overflow上令人作呕的问题,尽管措辞不同。在一条线索中,答案是使用PostSharp。正如其他人建议转储堆栈跟踪一样,您可以这样做。最简单的方法是手动转储本地变量。这可以是跟踪,也可以创建自己的自定义异常处理程序。