Elmah自定义记录器无法正常工作

时间:2013-02-15 03:10:05

标签: c# asp.net-mvc elmah

我希望有一个全局点来处理asp.mvc3应用程序中的所有异常。 我创建了新的应用程序,使用nuget添加了elmah并实现了ElmahCustomLogger类。我还在web.config elmah部分注册了这个自定义记录器。

由于某些原因,ElmahCustomLogger :: Log方法从未被调用过(我在那里放置制动点)

public class ElmahCustomLogger : ErrorLog 
{
    public override string Log(Error error)
    {
        throw new NotImplementedException();
    }

    public override ErrorLogEntry GetError(string id)
    {
        throw new NotImplementedException();
    }

    public override int GetErrors(int pageIndex, int pageSize, IList errorEntryList)
    {
        throw new NotImplementedException();
    }
}

2 个答案:

答案 0 :(得分:4)

看起来我只需声明特定的构造函数即可使其工作:

public class ElmahCustomLogger : Elmah.ErrorLog
{
    public ElmahCustomLogger(IDictionary config) {}
    ...
}

答案 1 :(得分:0)

受此post启发

这是我的解决方案:

namespace CompanyName.Models.Platform.Exceptions
{
    /// <summary>
    /// Custom SQL Elmah logger with more details from the Exception.Data Collection
    /// </summary>
    public class EnhancedSqlErrorLog : SqlErrorLog
    {
        public EnhancedSqlErrorLog(IDictionary config) : base(config)
        {
            // Must define for Elmah to work
        }

        public EnhancedSqlErrorLog(string connectString) : base(connectString)
        {
            // Must define for Elmah to work
        }

        public override string Log(Error error)
        {
            // If there is data on the exception, log it
            if (error.Exception.Data != null && error.Exception.Data.Keys.Count > 0)
            {
                var sb = new StringBuilder();

                foreach (var key in error.Exception.Data.Keys)
                {
                    sb.AppendLine(key + " - " + error.Exception.Data[key]);
                }

                // Be sure to append to whatever is already there
                error.Detail += sb.ToString();
            }

            return base.Log(error);
        }
    }
}

然后在你的web.config中指向Elmah使用新的记录器

<errorLog type="CompanyName.Models.Platform.Exceptions.EnhancedSqlErrorLog, YourDllName" connectionString="Data Source=.;Initial Catalog=YourDatabase;Integrated Security=True;MultipleActiveResultSets=True;" />