是否可以使用ELMAH进行以下操作?

logger.Log(" something");

我是这样做的:

try 
{
    // Code that might throw an exception 
}
catch(Exception ex)
{
    // I need to log error here...
}

ELMAH不会自动记录此异常,因为它已被处理。


当前回答

我建议在您自己的一个简单的包装类中包装对Elmah的调用。

using Elmah;

public static class ErrorLog
{
    /// <summary>
    /// Log error to Elmah
    /// </summary>
    public static void LogError(Exception ex, string contextualMessage=null)
    {
        try
        {
            // log error to Elmah
            if (contextualMessage != null) 
            {
                // log exception with contextual information that's visible when 
                // clicking on the error in the Elmah log
                var annotatedException = new Exception(contextualMessage, ex); 
                ErrorSignal.FromCurrentContext().Raise(annotatedException, HttpContext.Current);
            }
            else 
            {
                ErrorSignal.FromCurrentContext().Raise(ex, HttpContext.Current);
            }

            // send errors to ErrorWS (my own legacy service)
            // using (ErrorWSSoapClient client = new ErrorWSSoapClient())
            // {
            //    client.LogErrors(...);
            // }
        }
        catch (Exception)
        {
            // uh oh! just keep going
        }
    }
}

然后在需要记录错误时调用它。

try {
   ...
} 
catch (Exception ex) 
{
    // log this and continue
    ErrorLog.LogError(ex, "Error sending email for order " + orderID);
}

这有以下好处:

You don't need to remember this slightly archaic syntax of the Elmah call If you have many DLLs you don't need to reference Elmah Core from every single one - and just put this in your own 'System' DLL. If you ever need to do any special handling or just want to put in a breakpoint to debug errors you have it all one place. If you ever move away from Elmah you can just change one place. If you have legacy error logging you want to retain (I just happen to have a simple error logging mechanism that's tied into some UIs that I dont immediately have time to remove).

注意:我为上下文信息添加了一个'contextualMessage'属性。如果你愿意,你可以省略这个,但我发现它非常有用。Elmah自动展开异常,因此底层异常仍将在日志中报告,但当您单击contextualMessage时将可见。

其他回答

我试图写自定义消息到elmah日志使用Signal.FromCurrentContext().Raise(ex);并发现这些例外都是冒泡的,例如:

try
{
    ...
}
catch (Exception ex)
{
    Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
    // this will write to the log AND throw the exception
}

此外,我不知道elmah是如何支持不同级别的日志记录的——是否有可能通过web关闭详细日志记录。配置设置?

我在使用ASP。NET core和ElmahCore。

要手动记录HttpContext中的错误(在控制器中),只需写:

using ElmahCore;
...
HttpContext.RiseError(new Exception("Your Exception"));

在没有HttpContext的应用程序的另一部分:

using ElmahCore;
...
ElmahExtensions.RiseError(new Exception("Your Exception"));

可以使用Elmah.ErrorSignal()方法记录问题而不引发异常。

try
{
    // Some code
}
catch(Exception ex)
{
    // Log error
    Elmah.ErrorSignal.FromCurrentContext().Raise(ex);

    // Continue
}

有时CurrentHttpContext可能不可用。

定义

public class ElmahLogger : ILogger
{
    public void LogError(Exception ex, string contextualMessage = null, bool withinHttpContext = true)
    {
        try
        {
            var exc = contextualMessage == null 
                      ? ex 
                      : new ContextualElmahException(contextualMessage, ex);
            if (withinHttpContext)
                ErrorSignal.FromCurrentContext().Raise(exc);
            else
                ErrorLog.GetDefault(null).Log(new Error(exc));
        }
        catch { }
    }
}

Use

public class MyClass
{
    readonly ILogger _logger;

    public MyClass(ILogger logger)
    {
        _logger = logger;
    }

    public void MethodOne()
    {
        try
        {

        }
        catch (Exception ex)
        {
            _logger.LogError(ex, withinHttpContext: false);
        }
    }
}
catch(Exception ex)
{
    Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
}