1

In my WCF / C# project, I am using Nlog, it successfully creates logs inside the current date folder, but it does not archive the log files.

NLog.config:

 <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      throwExceptions="false"
      internalLogFile="c:\temp\nlog-internal.log"
      internalLogLevel="Trace"
      >

  <targets>
    <!-- Logs will go into Logs folder -->
    <target xsi:type="File"
            name="file"
            fileName="Logs/${shortdate}/Test.log"
            createDirs="true"
            layout="${longdate}|${level:uppercase=true}|${callsite}|[${threadid}]|${message}${exception:format=tostring}"
            keepFileOpen="false"
            archiveEvery="Day"
            archiveFileName="Logs/Test_${shortdate}.log"
            />
  </targets>

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

I want that if system ran on today 03/10/2025, and does not restarted on next day the log file should be moved outside from the dated folder.

Consider the actual date for this test: October 3rd 2025.

If I run my app now, the folder 2025-10-03 and inside that folder file Test.log will be created in the Log folder.

.
├── ...
├── Logs
│   └── 2025-10-03
        └── Test.log
└── ...

If I send a request to my app next day without restarting the app, the file above will be archived to outside the dated folder and a new folder and file will be created in the Log folder.

.
├── ...
├── Logs
│   └── 2025-10-03
        └── 
│   └── 2025-10-04
        └── Test.log
│   └── Test_2025-10-04.log
└── ...
3
  • 1
    Does this help? "Archive move operation only works if FileName is static in nature, and not rolling automatically because of ${date} or ${shortdate} " Commented Oct 3 at 11:48
  • 1
    yes, I read it but in one of my project it is working with ${shortdate} in FileName. I am wonder in one it is working and in other it is not working. Commented Oct 3 at 12:12
  • In one of your projects, it probably works because the application restarts every day — when the app starts, NLog creates a new log file for the current date automatically. In this case, however, the application keeps running continuously, so NLog doesn’t detect that the date has changed during runtime and therefore doesn’t trigger the archive operation. Commented Oct 4 at 8:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.