Click here to Skip to main content
15,889,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the NLog configuration below:

<target
    name="MyLog"
    xsi:type="File"
    fileName="./Logs/${shortdate:cached=true}.txt"
    layout="${message}"
    archiveFileName="./Logs/Archive/${longdate}-{##}.txt"
    archiveOldFileOnStartup="true"
    archiveNumbering="DateAndSequence"
    maxArchiveFiles="2">
</target>


It creates one log file in the /Logs directory and this file is moved to the ./Logs/Archive directory each time the application runs. The problem is that I just get more and more archived files. Can anyone tell me what I am doing wrong to cause it not to honour the maximum-archived-files setting?

What I have tried:

Various file-naming settings for the main file and the archived files. Nothing seems to get it to honour the request to keep only two archived files.
Posted
Updated 18-May-17 14:13pm
Comments
F-ES Sitecore 17-May-17 10:18am    
Try an archiveNumbering that doesn't use date.
Patrick Skelton 17-May-17 13:03pm    
Thank you for the answer.

That does indeed only keep the required number of log files. One oddity is that, despite what the documentation says, archiveNumbering="Rolling" and archiveNumbering="Sequence" seem identical - i.e. the higher-numbered archived file contains later messages than the lower-numbered archived file. This seems like too big a bug to have gone unnoticed, so I guess I am doing something else wrong.

1 solution

You should remove "${longdate}" from the archiveFileName. The place-holder "{##}" will automatically insert date and sequence-number.

Instead use the File-Target parameter archiveDateFormat to control the date-layout used inside the place-holder.
 
Share this answer
 
Comments
Patrick Skelton 20-May-17 4:47am    
For a while, I couldn't get this to work. Turned out to be my trying to use colons in the date format, which I did escape with a backslash as mentioned in the docs. Still didn't work. Anyway, switching to hyphens got it working.

I still don't really see why the archiving needs to be mutually exclusive with having any kind of a time-stamp in the main file, given how easy I found it to write C# code to do what I wanted it to do. But thank you very much for the help.
Rolf Kristensen 20-May-17 5:25am    
The NLog archive-logic includes cleanup of old files, and it uses file-wildcard to recognize what files to remove. If using "random" filenames (ex. longdate), then the file-wildcard will fail.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900