Elmah add message to error logged through call to Raise(e)

家住魔仙堡 提交于 2019-12-03 16:53:53

问题


I'm a bit confused at how to add a message to an error logged programatically with ELMAH.

eg:

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);
    }
}

It seems all Elmah can do is log the raw exception, how can I also log my own debug info?


回答1:


You can throw a new Exception setting the original as the inner exception and ELMAH will log the messages for both:

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

will show

System.Exception: ID = 1 ---> System.NullReferenceException: Object reference not set to an instance of an object.



回答2:


I found that I can also do something like:

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

To log my own messages. Then in when I browse to

http://localhost:5050/elmah.axd

I see my messages as type NotImplementedException. Not very pretty but works.



来源:https://stackoverflow.com/questions/3812538/elmah-add-message-to-error-logged-through-call-to-raisee

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!