2016-12-18 56 views
0

我有一個客戶的DNN網站與自定義模塊有模塊錯誤記錄的問題。該網站已從6.2升級到7.4版,現在升級到9.0版。自升級到7.4以來,模塊例外不再出現在管理員/主機事件中。它出現在DNN 7.4 as explained here中更改了模塊異常記錄。這是以前的代碼,但現在沒有任何記錄;DNN(DotNetNuke)模塊異常不記錄

測試對象:

public class foo 
{ 
    public int id { get; set; } 
    public string bar { get; set; } 
} 

測試的WebAPI控制器:

[DnnAuthorize] 
public class MyCustomModuleController : DnnApiController 
{ 
    private static readonly ILog Logger = LoggerSource.Instance.GetLogger(typeof(MyCustomModuleController)); 


    [HttpGet] 
    public HttpResponseMessage GetFoo(int id) 
    { 
     try 
     { 
      foo test = null; 
      var bar = test.bar; //will throw null exception 

      ... 

     } 
     catch (Exception ex) 
     { 
      Logger.Error(ex); //no log entries in db since ver. 7.4 
      return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Server error"); 
     } 
    } 

有,我應該能或者是有記錄事件的新方式的設置?

+0

升級是否改變DotNetNuke.log4net.config中的任何內容? – DotNetNuclear

+0

@DNetNet核心,log4Net配置文件沒有改變 –

回答

1

我的理解是DotNetNuke.Instrumentation.GetLogger()返回記錄對象,用於執行Log4Net日誌記錄以存儲在/ Portals/_default/Logs中的文件appender日誌。您還可以從主機>主機設置>日誌中查看它們。

通常情況下,您可以通過在構造函數中設置並調用.Error()/。ErrorFormat(),.Warn()/。WarnFormat()等函數來添加錯誤或信息消息到日誌文件。

public class MyCustomModuleController : DnnApiController 
{ 
    private DotNetNuke.Instrumentation.ILog Logger { get; set; } 

    public MyCustomModuleController() 
    { 
     Logger = DotNetNuke.Instrumentation.LoggerSource.Instance.GetLogger(this.GetType()); 
    } 

    public HttpResponseMessage GetFoo(int id) 
    { 
     try 
     { 
      ... 
     } 
     catch (Exception ex) 
     { 
      Logger.Error(ex); 
     } 
    } 
} 

可用的其他日誌記錄技術是使用DotNetNuke.Services.Exceptions將異常記錄到數據庫。這些錯誤會被添加到EventLog和Exceptions表中。

public class MyCustomModuleController : DnnApiController 
{ 
    public HttpResponseMessage GetFoo(int id) 
    { 
     try 
     { 
      ... 
     } 
     catch (Exception ex) 
     { 
      DotNetNuke.Services.Exceptions.Exceptions.LogException(ex); 
     } 
    } 
} 

DNN可能會將Log4Net進程與EventLog斷開連接。我不記得它是如何在版本6中起作用的。

+0

我測試過'DotNetNuke.Services.Exceptions.Exceptions.LogException(ex)',它工作。看起來他們對Log4Net實現做出了重大改變,因爲該方法不被棄用。我會將您的答案標記爲已接受,但我會在問題跟蹤器中提交一張票。謝謝 –