Asp.Net MVC Application_Start 中的 Elmah 日志错误
在 Global.asax
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
throw new Exception("TEST");
}
// ... other usual methods
}
Elmah
没有记录异常。
我在这里做了相当多的初始化和配置工作,如果出现任何问题,我会收到错误日志,这一点很重要。
不确定为什么它不起作用,但可能与 MVC 生命周期有关 - 也许现阶段没有 HttpContext
?有没有办法通过Elmah
在这里记录错误?
Possible Duplicate:
elmah: exceptions without HttpContext?
In Global.asax
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
throw new Exception("TEST");
}
// ... other usual methods
}
Elmah
is not logging the exception.
I do quite a bit of initialisation and configuration work in here and it's kinda important that I get error logs if anything goes wrong.
Not sure why it doesn't work but presumably to do with the MVC lifecycle - perhaps there is no HttpContext
at this stage? Is there any way to log errors through Elmah
here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您肯定对一件事,
HttpContext.Current
不存在于Application_Start
中,这可能是 Elmah 不记录该方法错误的问题之一。但是,如果您在 Application_Start 中出现异常,那么您会遇到比 Elmah 不记录异常更大的问题。您的
Application_Start
方法应该是完美的,或者如果不是,则需要编写它,以便它在您的开发环境中失败,以便您可以在将其推送到生产环境之前看到问题。如果是间歇性异常,我建议将问题代码放在其他地方。更新
根据您的评论,也许这对您有用。它还具有检查每个请求以查看第一次是否失败的额外好处。
You're definitely right about one thing,
HttpContext.Current
does not exist inApplication_Start
, which is probably one of the problems of why Elmah doesn't log errors from that method.But, if you have an exception in Application_Start, you have bigger problems than Elmah not logging the exception. You're
Application_Start
method should be flawless, or if it's not, it needs to be written so that it fails in your dev environment so you can see the problem before you push it to production. If it's an intermittent exception, I would suggest putting the problem code elsewhere.UPDATE
Based on your comment, maybe this would work for you. It also has the additional benefit of checking on every request to see if it failed the first time.