【发布时间】:2020-09-08 00:03:57
【问题描述】:
我有一个带有以下目标的 NLog.config 文件:
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
autoReload="true"
throwExceptions="true"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log" >
<variable name="ProgramDataPath"
<target name="MyLog" xsi:type="File" fileName="${ProgramDataPath}/MyLog.txt"
archiveFileName="${ProgramDataPath}/Archive/Mine/{#}_MyLog.txt" archiveNumbering="Date" archiveEvery="Day" archiveDateFormat="dd-MM-yyyy" maxArchiveFiles="7" />
我希望能够在每次启动应用程序时将“archiveEvery”属性设置为“Day”或“Month”值,这应该通过读取我拥有的另一个配置文件来完成属性:
<IsMylogDailyArchiveEnabled>false</IsMylogDailyArchiveEnabled>
False 表示它应该每月记录一次,True - 每天记录一次。
到目前为止,我可以阅读后面的内容,但是在我启动我的应用程序后,NLog.config 中的任何内容似乎都没有改变...
public string IsMylogDailyArchiveEnabled
{
get { return _isMylogDailyArchiveEnabled ; }
set
{
_isMylogDailyArchiveEnabled = value;
//Here I can see that 'false' is being returned as it should
EnableMyLogDailyArchiving(_isMylogDailyArchiveEnabled );
}
}
private void EnableMyLogDailyArchiving(string value)
{
var config = new XmlLoggingConfiguration("NLog.config");
var target = config.FindTargetByName("MyLog") as FileTarget;
if (value == "false")
{
//Does not work
target.ArchiveEvery = FileArchivePeriod.Month;
}
else
{
target.ArchiveEvery = FileArchivePeriod.Day;
}
//LogManager.ReconfigExistingLoggers();
LogManager.Configuration = config;
}
尝试了两个选项“LogManager.ReconfigExistingLoggers()”和重新分配配置(见上文),但是如前所述,我在 NLog.config 文件中看不到任何更改。
有什么想法或想法吗?谢谢
【问题讨论】:
标签: c# logging configuration nlog