我在Application_Start上生成一个线程,并希望记录异常。没有Context/HttpContext/HttpContext.Current
,那么我怎样才能记录它?
目前,它没有捕获我的线程中的任何异常,如果我写ErrorSignal.FromCurrentContext().Raise(ex);
,我得到一个关于上下文的错误不能为空。
也许我可以创建一个虚拟的HttpContext但不知何故我认为这不会很好。
-edit - 我尝试了ErrorSignal.Get(new HttpApplication()).Raise(ex);
,似乎没有接受该异常。
答案 0 :(得分:73)
确保在web.config中设置应用程序名称
<errorLog type="Elmah.SqlErrorLog, Elmah"
connectionStringName="nibWeb"
applicationName="Nib.Services" />
然后
ErrorLog.GetDefault(null).Log(new Error(error));
将起作用
答案 1 :(得分:2)
我没有像Brendan Carey那样使用<errorLog>
,因为我只是在记忆中记录。尽管如此,他的命令在我的案例中运作良好而没有命名应用程序:
Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(new Exception("The application has done something.")));
我DID必须使用.NET 4.0重新编译Elmah,因为有关需要System.Web.Abstractions 3.5.0.0的错误。如果有人想要它(也是强命名),我的compile-for-.NET 4.0分支就在这里:
答案 2 :(得分:1)
对于我的应用程序,我将this.Context.ApplicationInstance
保存在Application_Start
中,以便我可以使用已保存的实例调用Elmah.ErrorSignal.Get
。使用ErrorSignal
,我可以Raise
。这遍历了所有电子邮件过滤器。
以下是代码。我使用FluentScheduler
public class Global : HttpApplication {
void Application_Start(object sender, EventArgs e) {
var application = Context.ApplicationInstance;
FluentScheduler.TaskManager.UnobservedTaskException +=
(FluentScheduler.Model.TaskExceptionInformation i, UnhandledExceptionEventArgs a) =>
Elmah.ErrorSignal.Get(application).Raise(i.Task.Exception);
}
}
答案 3 :(得分:0)
我添加了一个解决方案:Using ELMAH in a console application,除了记录外,还增加了发送电子邮件,推文和过滤功能。