user2968369
user2968369

Reputation: 275

A factor while making at runtime NLog config user-define

My Nlog config file is as follows:

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

      <targets>
        <target xsi:type="File"
          name="file"
          layout="${longdate}|${level:uppercase=true}|${logger}|${message}"
          archiveAboveSize="2000"
          maxArchiveFiles="1"
          archiveFileName="${basedir}/log_archived.txt"
          fileName="log.txt" />
      </targets>

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

In my project the code is as follows:

 //Step1
    for (int i = 0; i < 100; i++)
    {
        logger.Fatal("Sample fatal error message::  {0}", i);
    }

    //Step2
    var nlogConfigFile = "NLog.config";
    var xdoc = XDocument.Load(nlogConfigFile);
    var ns = xdoc.Root.GetDefaultNamespace();
    var fileTarget = xdoc.Descendants(ns + "target").FirstOrDefault(t => (string)t.Attribute("name") == "file");
    fileTarget.Attribute("archiveAboveSize").SetValue(8000);
    xdoc.Save(nlogConfigFile);

    //Step3
    for (int i = 0; i < 100; i++)
    {
        logger.Fatal("Sample fatal error message::  {0}", i);
    }

At the end of Step1. I have a log.txt of 2KB. Then in step 2 I change the archiveAboveSize to 8K. Then at the end of step 3 I should have a log.txt of 8K. But it does not happen. The file remains of 2KB. But if I close the app and again rerun it then the log.txt size is 8KB. Why don't at the end of step3 i have file size of 8KB with out closing app

Upvotes: 0

Views: 242

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236278

You need to reload NLog configuration before step 3:

LogManager.Configuration = LogManager.Configuration.Reload();

Upvotes: 2

Related Questions