ELMAH:无异常HttpContext的?
-
22-09-2019 - |
题
我在产卵一个的Application_Start线程,想记录的例外。没有Context/HttpContext/HttpContext.Current
,所以我怎么可能会得到它登录?
目前,它不会在我的线程捕捉任何异常,如果我写ErrorSignal.FromCurrentContext().Raise(ex);
我得到上下文中的错误不能为空。
也许我可以创建一个虚拟的HttpContext,但不知何故,我不认为这会工作得很好。
-edit - 我试过ErrorSignal.Get(new HttpApplication()).Raise(ex);
,它似乎并没有拿起例外
解决方案
请确保您在设置您的应用程序名称的web.config
<errorLog type="Elmah.SqlErrorLog, Elmah"
connectionStringName="nibWeb"
applicationName="Nib.Services" />
和然后
ErrorLog.GetDefault(null).Log(new Error(error));
将工作
其他提示
我不使用<errorLog>
在布伦丹·凯莉的答案,因为我只是在内存中的日志记录。然而,他的命令不点名的应用程序在我的情况很好工作:
Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(new Exception("The application has done something.")));
我DID必须重新编译ELMAH与.NET 4.0,因错误而需要大约System.Web.Abstractions 3.5.0.0。我compiled-for-.NET 4.0叉在这里,如果有人想它(也强命名):
有关我的申请,我保存在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);
}
}
我添加了一个解决方案:在控制台应用程序,使用ELMAH添加到发送电子邮件,鸣叫和过滤器除了记录能力。
不隶属于 StackOverflow