سؤال

I want to have one global point for handling all exceptions in the asp.mvc3 application. I've created new application, added elmah using nuget and implemented ElmahCustomLogger class. I've also registered this custom logger in web.config elmah section.

For some reason ElmahCustomLogger::Log method gets never called (i'm putting a brakepoint there)

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();
    }
}
هل كانت مفيدة؟

المحلول

It looks like I just have to declare specific constructor to make it work:

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

نصائح أخرى

Inspired by this post

Here is my solution:

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

Then in your web.config point Elmah to use the new logger

<errorLog type="CompanyName.Models.Platform.Exceptions.EnhancedSqlErrorLog, YourDllName" connectionString="Data Source=.;Initial Catalog=YourDatabase;Integrated Security=True;MultipleActiveResultSets=True;" />
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top