Elmah不会在MVC应用中记录HTTP发布请求的异常-如果请求包含XML [英] Elmah not logging exceptions for http post requests in MVC app - if the request contains XML
问题描述
我在MVC4(RC)应用程序中遇到了一个奇怪的问题. (在.NET 4.0上运行)
I've come across a weird problem in my MVC4 (RC) application. (running on .NET 4.0)
我刚刚设置了Elmah来记录异常/错误.
I have just setup Elmah for logging exceptions / errors.
我基本上安装了 Elmah.MVC 和 elmah.sqlserver NuGet软件包. (分别是2.0.0和1.2版)
I basically installed the Elmah.MVC and elmah.sqlserver NuGet packages. (versions 2.0.0 and 1.2 respectively)
似乎开箱即用-我可以转到elmah页面并查看错误:
It seemed to work fine out of the box - I can go to the elmah page and view errors:
http://myserver/elmah
例如,如果我创建一些404错误,它们将出现在此日志中.
for example, if I create some 404 errors, they appear in this log.
这不起作用是什么:我有一个带有[HttpPost]
动作的标准MVC控制器.我已经设置好了,所以它总是会抛出异常:
What is not working is this: I have a standard MVC controller with a [HttpPost]
action. I've set it up so it will always throw an exception:
public class TestController : Controller
{
[HttpPost]
[ValidateInput(false)]
public void Testing()
{
throw new Exception("uh oh");
}
}
然后我尝试通过jQuery将数据发布到此控制器:
I then try to post data to this controller via jQuery:
$.post('/Test/Testing', {test_data: 'This is some test data'});
好的,这可行.响应返回典型的黄色死亡屏幕,并且错误被捕获并记录在Elmah中.
Ok, this works. The response returns the typical yellow screen of death, and the error is caught and logged in Elmah.
但是,如果我尝试发布类似XML/HTML的内容,则错误不记录在Elmah中.我仍然从服务器返回到相同的响应(黄色的死亡屏幕),但是在Elmah中什么也没有.
However, if I try to post something like XML/HTML the error is not logged in Elmah. I still get the same response from the server back (yellow screen of death), but nothing in Elmah.
$.post('/Test/Testing', {test_data: '<test><test1>This is some test data</test1></test>'});
为什么?这没有道理.
注意,我已经关闭了对动作的请求验证.如果我不这样做,那么发布XML/HTML数据将导致此异常:
Notice I have already turned off the request validation on the action. If I didn't do that, then posting XML/HTML data would cause this exception:
从客户端检测到潜在危险的Request.Form值
A potentially dangerous Request.Form value was detected from the client
NuGet也将拒绝记录该异常-我认为这是一个错误:
NuGet would also refuse to log that exception too - which I believe is a bug:
http://code.google.com/p/elmah/issues/detail?id = 217
那么我遇到此问题的原因是什么?这是与我在上面发现的问题有关的错误吗?
So what is the cause of this problem that I'm experiencing? It it a bug related to the issue I found above?
似乎很不幸的情况是我不能仅因为请求包含XML/HTML而记录异常.
It just seems quite an unfortunate situation that I can't log exceptions just because the request contained XML/HTML.
肯定有解决方法吗?
推荐答案
默认情况下,ELMAH不会捕获HttpRequestValidationException,并且如果用户发送无效请求,则ELMAH的报告中将丢失该请求.因此也必须定义和使用此全局过滤器:
ELMAH does not catch HttpRequestValidationException by default and if a user sends an invalid request it will be missed in ELMAH's report. so it's necessary to define and use this global filter as well:
public class ElmahRequestValidationErrorFilter : IExceptionFilter
{
public void OnException(ExceptionContext context)
{
if (context.Exception is HttpRequestValidationException)
ErrorLog.GetDefault(HttpContext.Current).Log(new Error(context.Exception));
}
}
这篇关于Elmah不会在MVC应用中记录HTTP发布请求的异常-如果请求包含XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!