繁体   English   中英

NLog不创建文件

[英]NLog not creating files

我正在尝试使用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*");
        }

    }

在记录器内部,我发现config为NULL:

在此处输入图片说明

我设法通过从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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM