Asp.Net MVC Application_Start 中的 Elmah 日志错误

发布于 2024-11-20 00:14:17 字数 770 浏览 5 评论 0原文

可能的重复:
elmah:没有 HttpContext 的异常?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

琉璃繁缕 2024-11-27 00:14:17

您肯定对一件事,HttpContext.Current 不存在于 Application_Start 中,这可能是 Elmah 不记录该方法错误的问题之一。

但是,如果您在 Application_Start 中出现异常,那么您会遇到比 Elmah 不记录异常更大的问题。您的 Application_Start 方法应该是完美的,或者如果不是,则需要编写它,以便它在您的开发环境中失败,以便您可以在将其推送到生产环境之前看到问题。如果是间歇性异常,我建议将问题代码放在其他地方。

更新

根据您的评论,也许这对您有用。它还具有检查每个请求以查看第一次是否失败的额外好处。

protected void Application_BeginRequest(object sender, EventArgs args)
{
  CheckConfiguration();
}

private static readonly object ConfigurationLockObject = new object();
private void CheckConfiguration()
{
  /** Lock Bypass **/
  if (IsConfigured()) 
  {
    return;
  }

  // The lock ensures the configuration only gets called one at a time.
  lock (ConfigurationLockObject)
  {
    // Must check for configuration again, just in case multiple threads had hit the lock
    if (!IsConfigured())
    {
      try
      {
        // Configuration Code
      }
      catch (Exception exception)
      {
        Log.Error("Exception Occurred", exception);
      }
    }
  }
}

You're definitely right about one thing, HttpContext.Current does not exist in Application_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.

protected void Application_BeginRequest(object sender, EventArgs args)
{
  CheckConfiguration();
}

private static readonly object ConfigurationLockObject = new object();
private void CheckConfiguration()
{
  /** Lock Bypass **/
  if (IsConfigured()) 
  {
    return;
  }

  // The lock ensures the configuration only gets called one at a time.
  lock (ConfigurationLockObject)
  {
    // Must check for configuration again, just in case multiple threads had hit the lock
    if (!IsConfigured())
    {
      try
      {
        // Configuration Code
      }
      catch (Exception exception)
      {
        Log.Error("Exception Occurred", exception);
      }
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文