如何使用 ELMAH 手动记录错误 [英] How to use ELMAH to manually log errors
本文介绍了如何使用 ELMAH 手动记录错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以使用 ELMAH 执行以下操作?
Is it possible to do the following using ELMAH?
logger.Log(" something");
我正在做这样的事情:
try
{
// Code that might throw an exception
}
catch(Exception ex)
{
// I need to log error here...
}
ELMAH 不会自动记录此异常,因为它已被处理.
This exception will not be automatically logged by ELMAH, because it was handled.
推荐答案
直接写日志的方法,从ELMAH 1.0开始工作:
Direct log writing method, working since ELMAH 1.0:
try
{
some code
}
catch(Exception ex)
{
Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(new Elmah.Error(ex));
}
ELMAH 1.2 引入了更灵活的 API:
ELMAH 1.2 introduces a more flexible API:
try
{
some code
}
catch(Exception ex)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
}
两种解决方案之间存在差异:
There is a difference between the two solutions:
Raise
方法将 ELMAH 过滤规则应用于异常.Log
方法没有.Raise
是基于订阅的,能够将一个异常记录到多个记录器中.
Raise
method applies ELMAH filtering rules to the exception.Log
method does not.Raise
is subscription based and is able to log one exception into the several loggers.
这篇关于如何使用 ELMAH 手动记录错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文