【问题标题】:NLog not creating filesNLog 不创建文件
【发布时间】:2018-08-13 15:34:21
【问题描述】:

我正在尝试使用 NLog 记录控制器中某些方法的活动,但每次我点击该方法时都没有创建日志文件,这是我的配置:

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<targets>
  <target name="file" xsi:type="File"
      layout="${longdate} ${logger} ${message}${exception:format=ToString}"
      fileName="${basedir}/test.txt"
      archiveFileName="${basedir}/{#}.txt"
      archiveDateFormat="yyyy-MM-dd HH_mm_ss"
      archiveNumbering="Date"
      archiveEvery="Month"
      maxArchiveFiles="6" />
</targets>

<rules>
  <logger name="*" minlevel="Info" writeTo="file" />
</rules>
</nlog>

这是我的日志记录代码:

public static Logger logger = LogManager.GetCurrentClassLogger();

public static void DashboardLogging(string username, string changedField, string oldValue, string newValue, string taskName)
    {
        try
        {
            logger.Info(" User: " + username + " | Field Changed: " + changedField + " | Change: " + oldValue + " --> " + newValue + " | Affected Task: " + taskName);
        }
        catch (Exception ex)
        {
            logger.Fatal(ex + " - *ERROR*");
        }

    }

查看记录器内部,我发现配置为 NULL:

【问题讨论】:

  • 您是否尝试打开internal logging 看看发生了什么?
  • 顺便说一句:Tutorial 请参阅第 2 点和第 3 点 ...(4 也可以阐明)
  • 我已经尝试过内部日志记录,它产生了相同的结果,我将完成链接的教程并报告我的发现。
  • 我尝试按照教程进行操作,发现当我获取记录器时,配置似乎为空(请参阅我的问题中的图片)。
  • @Fildor 谢谢你,你的评论拯救了我的一天,确切地说(4 也可以阐明)

标签: c# asp.net-mvc logging model-view-controller nlog


【解决方案1】:

我设法解决了这个问题,方法是从我的 web.config 文件中提取配置,将其放入子文件夹中的新 Nlog.config 文件中,然后在我登录之前指向它。

控制器:

private static Logger logger = LogManager.GetCurrentClassLogger();
public static void DashboardLogging(string username, string changedField, string oldValue, string newValue, string taskName)
    {
        try
        {
            LogManager.Configuration = new XmlLoggingConfiguration(AppDomain.CurrentDomain.BaseDirectory + "Views\\Dashboard\\Nlog.config");

            logger.Info(" User: " + username + " | Field Changed: " + changedField + " | Change: " + oldValue + " --> " + newValue + " | Affected Task: " + taskName);
        }
        catch (Exception ex)
        {
            logger.Error(ex, "*ERROR*");
        }

    }

Nlog.config:

    <?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
  </configSections>

  <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      throwConfigExceptions="true">

    <targets>
      <target name="file" xsi:type="File"
          layout="${longdate} ${logger} ${message}${exception:format=ToString}"
          fileName="${basedir}/Log/test.txt"
          archiveFileName="${basedir}/Log/{#}.txt"
          archiveDateFormat="yyyy-MM-dd HH_mm_ss"
          archiveNumbering="Date"
          archiveEvery="Month"
          maxArchiveFiles="6" />
    </targets>

    <rules>
      <logger name="*" minlevel="Info" writeTo="file" />
    </rules>
  </nlog>
</configuration>

【讨论】:

  • 现在您每次登录时都在设置配置。超级流畅啊您需要在应用启动时读取一次配置。
  • 但也许你的配置放错了地方。我从来不需要这样做。它刚刚奏效。见:github.com/NLog/NLog/wiki/…
  • 请注意,如果您将其用作独立的(在 web.config 之外),则需要将 &lt;nlog&gt; 作为根标记。如果您发布您的(经过清理的)web.config,我们也许可以看到为什么 nlog 没有从 web.config 中获取配置。
  • 感谢您的建议,我会继续您的建议!
猜你喜欢
  • 2017-01-23
  • 1970-01-01
  • 1970-01-01
  • 2017-07-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-12
  • 1970-01-01
相关资源
最近更新 更多