如何使用ELMAH手动记录错误 [英] How to use ELMAH to manually log errors

查看:123
本文介绍了如何使用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 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆