NLog 不创建文件
NLog not creating files
我正在尝试使用 NLog 从控制器中的某些方法记录 activity,但每次我点击该方法时都没有创建日志文件,这是我的配置:
<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:
我设法解决了这个问题,方法是从我的 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>
我正在尝试使用 NLog 从控制器中的某些方法记录 activity,但每次我点击该方法时都没有创建日志文件,这是我的配置:
<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:
我设法解决了这个问题,方法是从我的 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>