C# 在MVC4中使用Elmah和NLog

C# 在MVC4中使用Elmah和NLog,c#,asp.net-mvc-4,elmah,nlog,C#,Asp.net Mvc 4,Elmah,Nlog,我在MVC4应用程序中使用Elmah处理异常。然后我安装了NLog进行调试。当出现异常时 try { // There is exception occured here } catch (DataException ex) { logger.Log(LogLevel.Error, dex.Message); ModelState.AddModelError("", "Unable to save changes"); } 为什么NLog不记录异常,而elmah记录异常

我在MVC4应用程序中使用Elmah处理异常。然后我安装了NLog进行调试。当出现异常时

try
{
    // There is exception occured here
}
catch (DataException ex)
{
    logger.Log(LogLevel.Error, dex.Message);
    ModelState.AddModelError("", "Unable to save changes");
}
为什么NLog不记录异常,而elmah记录异常?
我想使用NLog和elmah记录异常,如何配置它们?

我决定将此作为答案发布,因为评论中不清楚:

描述如何构造web.config(或使用单独的配置文件)

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

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

****Nlog特定设置在此处*****
一些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>


为此,您需要从web.config中提供设置,请记住,顶部需要一个配置部分(第一行或第二行),并进一步提供日志记录的详细信息down@KerSplosh我已经设置了web.config,还有什么?您需要在web.config的顶部有一个这样的节点,再往下一点有一个相应的节点:最后,我刚刚找到了解决方案。这是因为在try块中有一些代码抛出new Exception(),但它没有被处理。我只处理DataException。如果我将DataException更改为Exception,NLog将记录该异常。所以这里的重点是句柄和未处理的异常。我今天刚学会。web.config与您的相同,感谢您的回复:)