elmah:没有 HttpContext 的异常?
我在 Application_Start 上生成一个线程并希望记录异常。没有 Context/HttpContext/HttpContext.Current ,那么我如何才能记录它呢?
目前,它在我的线程中没有捕获任何异常,如果我编写 ErrorSignal.FromCurrentContext().Raise(ex);
,我会收到有关上下文不能为空的错误。
也许我可以创建一个虚拟的 HttpContext 但不知何故我认为这不会很好地工作。
-edit- 我尝试了 ErrorSignal.Get(new HttpApplication()).Raise(ex);
但它似乎没有发现该异常。
I spawn a thread on Application_Start and would like to log exceptions. There is no Context/HttpContext/HttpContext.Current
, so how might I get it to log?
At the moment, it does not catch any exception in my threads and if I write ErrorSignal.FromCurrentContext().Raise(ex);
I get an error about context cannot be null.
Maybe I can create a dummy HttpContext but somehow I don't think that will work well.
-edit- I tried ErrorSignal.Get(new HttpApplication()).Raise(ex);
and it doesn't seem to pick up that exception.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
确保您在 web.config 中设置了应用程序名称
,然后
就可以工作了
Make sure you set your application name in web.config
and then
will work
我没有像 Brendan Carey 的回答那样使用
因为我只是在内存中记录。尽管如此,他的命令在我的情况下运行得很好,无需命名应用程序:由于需要 System.Web.Abstractions 3.5.0.0 的错误,我必须使用 .NET 4.0 重新编译 Elmah。如果有人需要的话,我为 .NET 4.0 编译的分支就在这里(也是强命名):
http ://code.google.com/r/scottstafford-elmah/
I wasn't using
<errorLog>
as in Brendan Carey's answer because I was only in-memory logging. Nevertheless, his command worked great in my case without naming the application:I DID have to recompile Elmah with .NET 4.0, because of an error about needing System.Web.Abstractions 3.5.0.0. My compiled-for-.NET 4.0 fork is here if anyone wants it (also strong naming):
http://code.google.com/r/scottstafford-elmah/
对于我的应用程序,我将
this.Context.ApplicationInstance
保存在Application_Start
中,以便我可以使用保存的实例调用Elmah.ErrorSignal.Get
。有了ErrorSignal
,我就可以Raise
。这将通过所有电子邮件过滤器。下面是代码。我使用 FluentScheduler 来
For my application, I saved
this.Context.ApplicationInstance
inApplication_Start
so that I can callElmah.ErrorSignal.Get
with the saved instance. With theErrorSignal
, I could thenRaise
. This goes through all the email filters.Below is the code. I use FluentScheduler to
我添加了一个解决方案:在控制台应用程序中使用 ELMAH,它增加了以下功能:除了日志记录之外,还可以发送电子邮件、推文和过滤器。
I added a solution to: Using ELMAH in a console application that adds ability to send email, tweets and filter in addition to logging.