Elmah error logging, can I just log a message?

瘦欲@ 提交于 2019-12-03 08:21:59

问题


I just installed Elmah (https://code.google.com/p/elmah/) for my ASP.NET application. Is it possible to log a message without creating an Exception first?

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

So is it possible to do:

ErrorSignal.FromCurrentContext().log("Hello I am testing Elmah");

回答1:


Yes, you can use ErrorSignal without throwing an exception.

ErrorSignal.FromCurrentContext().Raise(new NotSupportedException());

For the custom message, you can create a custom exception.

var customEx = new Exception("Hello I am testing Elmah", new NotSupportedException()); 
ErrorSignal.FromCurrentContext().Raise(customEx);



回答2:


Try this

Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception("Your message"));



回答3:


I know this is an old question, but if you don't want to create an exception you can also use

var error = new Error
{
   Source = eventType.ToString(),
   Type = $"Trace-{eventType}",
   Message = message,
   Time = DateTime.UtcNow
};

ErrorLog.GetDefault(HttpContext.Current).Log(error);

as shown in this answer.



来源:https://stackoverflow.com/questions/18115891/elmah-error-logging-can-i-just-log-a-message

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