代码之家  ›  专栏  ›  技术社区  ›  fearofawhackplanet

Elmah add message to error logged through call to Raise(e)通过调用将消息添加到错误日志

  •  12
  • fearofawhackplanet  · 技术社区  · 15 年前

    我对如何将消息添加到用ELMAH编程记录的错误中有点困惑。

    public ActionResult DoSomething(int id)
    {
        try { ... }
    
        catch (Exception e)
        {
            // I want to include the 'id' param value here, and maybe some
            // other stuff, but how?
            ErrorSignal.FromCurrentContext().Raise(e);
        }
    }
    

    似乎Elmah所能做的就是记录原始异常,我怎么能同时记录我自己的调试信息呢?

    2 回复  |  直到 15 年前
        1
  •  18
  •   Jeff Ogata    15 年前

    您可以抛出一个新异常,将原始异常设置为内部异常,ELMAH将记录这两个异常的消息:

    catch(Exception e)
    {
        Exception ex = new Exception("ID = 1", e);
        ErrorSignal.FromCurrentContext().Raise(ex);
    }
    

    将显示

    System.Exception: ID = 1 ---> System.NullReferenceException: Object reference not set to an instance of an object.
    
        2
  •  3
  •   Mark    13 年前

    Elmah.ErrorSignal.FromCurrentContext().Raise(new NotImplementedException("class      FbCallback.Page_Load() Request.Url= " + Request.Url));
    

    记录我自己的消息。当我浏览到

    http://localhost:5050/elmah.axd
    

    不是很漂亮但是很管用。

    推荐文章