2013-02-15 105 views
1

我想有一個全局點來處理asp.mvc3應用程序中的所有異常。 我創建了新的應用程序,使用nuget添加了elmah並實現了ElmahCustomLogger類。我也在web.config elmah部分註冊了這個自定義記錄器。Elmah自定義記錄器不工作

出於某種原因ElmahCustomLogger ::登錄方法被稱爲永遠不會(我把那裏brakepoint)

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

回答

4

它看起來像我只需要申報的具體構造,使其工作:

public class ElmahCustomLogger : Elmah.ErrorLog 
{ 
    public ElmahCustomLogger(IDictionary config) {} 
    ... 
} 
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;" />