【问题标题】:Using Elmah and NLog in MVC4在 MVC4 中使用 Elmah 和 NLog
【发布时间】:2014-05-07 14:17:25
【问题描述】:

我在我的 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 中的设置,记住您需要顶部的配置部分(第一行或两行),以及进一步向下的日志记录详细信息跨度>
  • @KerSplosh 我已经设置了web.config,,还有什么?
  • 你需要在 web.config 的顶部有一个这样的节点,并在下面有一个相应的节点:

标签: c# asp.net-mvc-4 elmah nlog


【解决方案1】:

我决定将其发布为答案,因为在 cmets 中并不清楚:

The NLog Documentation 说明如何构建您的 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>

【讨论】:

  • 终于,我找到了解决方案。这是因为在 try 块中有代码抛出新的 Exception() 并且它没有被处理。我只处理DataException。如果我将 DataException 更改为 Exception,NLog 将记录该异常。所以这里的重点是句柄和未处理的异常。我今天才学的。 web.config 和你的一样,谢谢你的回复:)
猜你喜欢
  • 1970-01-01
  • 2011-09-30
  • 2020-07-09
  • 2011-05-03
  • 2011-10-05
  • 1970-01-01
  • 1970-01-01
  • 2015-04-24
  • 2015-07-18
相关资源
最近更新 更多