簡體   English   中英

在MVC4中使用Elmah和NLog

[英]Using Elmah and NLog in MVC4

我在MVC4應用程序中使用Elmah來處理異常。 然后我安裝了NLog進行調試。 有例外時

try
{
    // There is exception occured here
}
catch (DataException ex)
{
    logger.Log(LogLevel.Error, dex.Message);
    ModelState.AddModelError("", "Unable to save changes");
}

為什么當Elmah記錄時NLog不會記錄異常? 我想使用NLog和elmah記錄異常,如何配置它們?

我決定將其發布為答案,因為評論中尚不清楚:

NLog文檔描述了如何構造web.config(或使用單獨的配置文件)

您需要在web.config中使用此塊

<configuration>
  <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
  </configSections>
  <nlog>
     **** Nlog specific settings go here *****
  </nlog>
</configuration>

NLog設置示例:

  <variable name="logDirectory" value="${basedir}/logs/${shortdate}"/>
  <targets>
    <target name="file1" xsi:type="File" fileName="${logDirectory}/file1.txt"/>
    <target name="file2" xsi:type="File" fileName="${logDirectory}/file2.txt"/>
  </targets>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM