ASP.Net MVC 异常日志记录与错误处理相结合
我正在寻找一个简单的解决方案来在我的 ASP.Net MVC 1.0 应用程序中进行异常日志记录和错误处理。
我读过很多文章,包括 StackOverflow 上发布的问题,它们都针对不同情况提供了不同的解决方案。 我仍然无法想出适合我需求的解决方案。
以下是我的要求:
能够在我的控制器上使用 [HandleError] 属性(或等效的属性)来处理可能从任何操作或视图引发的所有异常。 这应该处理未在任何操作上专门处理的所有异常(如第 2 点所述)。 我希望能够为控制器中的所有操作指定在错误情况下用户必须重定向到哪个视图。
我希望能够在特定操作的顶部指定 [HandleError] 属性(或等效属性)以捕获特定异常并将用户重定向到适合该异常的视图。 所有其他异常仍必须由控制器上的 [HandleError] 属性处理。
在上述两种情况下,我希望使用 log4net(或任何其他日志库)记录异常。
在
我该如何实现上述目标? 我读过有关使我的所有控制器继承自重写 OnException 方法的基本控制器的内容,并在其中进行日志记录。 然而,这会扰乱将用户重定向到适当的视图,或者使其变得混乱。
我读过有关编写自己的过滤器操作的信息,该操作实现了 IExceptionFilter 来处理此问题,但这将与 [HandleError] 属性发生冲突。
到目前为止,我的想法是最好的解决方案是编写我自己的继承自 HandleErrorAttribute 的属性。 这样我就可以获得 [HandleError] 的所有功能,并且可以添加我自己的 log4net 日志记录。 解决方案如下:
public class HandleErrorsAttribute: HandleErrorAttribute {
private log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public override void OnException(ExceptionContext filterContext)
{
if (filterContext.Exception != null)
{
log.Error("Error in Controller", filterContext.Exception);
}
base.OnException(filterContext);
}
}
上面的代码能满足我的要求吗? 如果没有,什么解决方案可以满足我的要求?
I am looking for a simple solution to do Exception Logging combined with Error Handling in my ASP.Net MVC 1.0 application.
I've read lots of articles, including Questions posted here on StackOverflow, which all provide varying solutions for different situations. I am still unable to come up with a solution that suits my needs.
Here are my requirements:
To be able to use the [HandleError] attribute (or something equivalent) on my Controller, to handle all exceptions that could be thrown from any of the Actions or Views. This should handle all exceptions that were not handled specifically on any of the Actions (as described in point 2). I would like to be able to specify which View a user must be redirected to in error cases, for all actions in the Controller.
I want to be able to specify the [HandleError] attribute (or something equivalent) at the top of specific Actions to catch specific exceptions and redirect users to a View appropriate to the exception. All other exceptions must still be handled by the [HandleError] attribute on the Controller.
In both cases above, I want the exceptions to be logged using log4net (or any other logging library).
How do I go about achieving the above? I've read about making all my Controllers inherit from a base controller which overrides the OnException method, and wherein I do my logging. However this will mess around with redirecting users to the appropriate Views, or make it messy.
I've read about writing my own Filter Action which implements IExceptionFilter to handle this, but this will conflict with the [HandleError] attribute.
So far, my thoughts are that the best solution is to write my own attribute that inherits from HandleErrorAttribute. That way I get all the functionality of [HandleError], and can add my own log4net logging. The solution is as follows:
public class HandleErrorsAttribute: HandleErrorAttribute {
private log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public override void OnException(ExceptionContext filterContext)
{
if (filterContext.Exception != null)
{
log.Error("Error in Controller", filterContext.Exception);
}
base.OnException(filterContext);
}
}
Will the above code work for my requirements? If not, what solution does fulfill my requirements?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我仍然对所有不同的解决方案以及属性如何相互干扰感到有点困惑,但我采用了这个解决方案:
我仍然使用原始问题中所解释的 [HandleError] 属性,并且我只是装饰每个控制器都有一个 [LogErrors] 属性。
这对我有用,因为它将错误记录保留在一个位置,并且不会导致多次记录重复的异常(如果我扩展 [HandleError] 并在多个位置使用该属性,则会发生这种情况)。
我认为不可能将异常日志记录和错误处理合并到一个属性或类中,而不会使它变得非常乏味和复杂,或影响 [HandleError] 的使用
但这对我有用,因为我装饰了每个控制器仅使用 [LogErrors] 属性一次,并按照我想要的方式使用 [HandleError] 装饰控制器和操作,而不会相互干扰。
更新:
这是我如何使用它的示例:
在上面的代码中,
ContactForm
操作重载中的SmtpExceptions以非常具体的方式处理 - 向用户呈现特定于发送失败的消息的 ViewPage ,在本例中称为“MessageFailed”。 所有其他异常均由 [HandleError] 的默认行为处理。 另请注意,首先记录错误,然后处理错误。 这由以下内容表示:更新:
对此有一个替代解决方案,并且有很好的解释。 我建议通读它以更好地理解所涉及的问题。
ASP.NET MVC HandleError 属性、自定义错误页面和日志记录异常
(感谢下面的 Scott Shepherd,他在下面的答案中提供了链接)。
I'm still a bit confused with all the different solutions out there, and how attributes can interfere with each other, but I went with this solution:
I still use the [HandleError] attribute as explained in the original question, and I just decorate each controller with a [LogErrors] attribute.
This works for me, as it keeps the error logging in one place and doesn't cause duplicate exceptions to be logged multiple times (which will happen if I extend [HandleError] and use the attribute in multiple places).
I don't think it will be possible to combine both the Exception Logging and Error Handling into one atrribute or class, without it becoming very tedious and complex, or affecting the use of [HandleError]
But this works for me since I decorate each controller only once, with the [LogErrors] attribute, and decorate Controllers and Actions with [HandleError] exactly how I want to, without them interfering with each other.
Update:
Here is an example of How I use it:
In the above code, SmtpExceptions in the
ContactForm
action overload are handled in a very specific way - the user is presented with a ViewPage specific to failed sent messages, in this case it is called "MessageFailed" . All other exceptions are handled by the default behaviour of [HandleError]. Also note that logging of errors occurs first, followed by handling of errors. This is indicated by the following:Update:
There is an alternative solution to this, with a very good explanantion. I recommend reading through it to get a better understanding of the issues involved.
ASP.NET MVC HandleError Attribute, Custom Error Pages and Logging Exceptions
(Thanks to Scott Shepherd below, who provided the link in an answer below).