是否可以使用ELMAH进行以下操作?
logger.Log(" something");
我是这样做的:
try
{
// Code that might throw an exception
}
catch(Exception ex)
{
// I need to log error here...
}
ELMAH不会自动记录此异常,因为它已被处理。
是否可以使用ELMAH进行以下操作?
logger.Log(" something");
我是这样做的:
try
{
// Code that might throw an exception
}
catch(Exception ex)
{
// I need to log error here...
}
ELMAH不会自动记录此异常,因为它已被处理。
当前回答
catch(Exception ex)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
}
其他回答
我希望在一个线程中做同样的事情,我已经开始从我的MVC4应用程序中排队邮件,因此当异常被引发时,我没有HttpContext可用。为了做到这一点,我最终与以下基于这个问题和另一个答案在这里找到:elmah:异常没有HttpContext?
在配置文件中,我指定了一个应用程序名称:
<elmah>
<security allowRemoteAccess="false" />
<errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="ELMAH" applicationName="myApplication"/>
</elmah>
然后在代码中(就像上面提供的答案,但没有HttpContext)你可以传递null而不是HttpContext:
ThreadPool.QueueUserWorkItem(t => {
try {
...
mySmtpClient.Send(message);
} catch (SomeException e) {
Elmah.ErrorLog.GetDefault(null).Log(new Elmah.Error(e));
}
});
catch(Exception ex)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
}
可以使用Elmah.ErrorSignal()方法记录问题而不引发异常。
try
{
// Some code
}
catch(Exception ex)
{
// Log error
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
// Continue
}
我试图写自定义消息到elmah日志使用Signal.FromCurrentContext().Raise(ex);并发现这些例外都是冒泡的,例如:
try
{
...
}
catch (Exception ex)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
// this will write to the log AND throw the exception
}
此外,我不知道elmah是如何支持不同级别的日志记录的——是否有可能通过web关闭详细日志记录。配置设置?
是的,这是可能的。ELMAH被设计用来拦截未处理的异常。但是,您可以通过ErrorSignal类向ELMAH发出异常信号。这些异常不会被抛出(不会冒泡),而只被发送到ELMAH(以及ErrorSignal类的Raise事件的订阅者)。
举个小例子:
protected void ThrowExceptionAndSignalElmah()
{
ErrorSignal.FromCurrentContext().Raise(new NotSupportedException());
}