简体   繁体   中英

C# - NLog.config setting 'archiveEvery' value programatically on start

I have a NLog.config file with the following target:

<?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" />

I'd like to be able to modify the 'archiveEvery' property by setting it to either 'Day' or 'Month' value each time I start my app, and that should be done by reading another config file where I have the property:

<IsMylogDailyArchiveEnabled>false</IsMylogDailyArchiveEnabled>

False means it should log monthly, True - daily.

So far I am able to read the later, however nothing inside NLog.config seems to be changed after I start my app...

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;                   
    }

Tried both options 'LogManager.ReconfigExistingLoggers()' and reassigning configuration (see above), however as mentioned before, I can't see any changes inside the NLog.config file.

Any thoughts or ideas? Thanks

It should work,

When doing LogManager.Configuration = config; , the configuration is reloaded and all targets are initialized. So the new ArchiveEvery setting will be used.

But be aware that auto reload is enabled ( autoReload="true" ), so every change in the XML config will revert the change made in code. If you don't want that, then change the config by subscribing to LogManager.ConfigurationReloaded

PS: it's recommend to disabled throwExceptions in production (eg by removing throwExceptions="true" )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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