Logs are important
Having logs can save any backend dev tons of debug time.
Not even mentioning that they should all be in the same place, seems like a no-brainer but often we don't see this.
It is also hard to have good logs, a good amount of time can be invested just to achieve this. And a lot of effort needs to be invested in good configurations... and we all know how much we "love" messing up with configurations.
Image may be NSFW.
Clik here to view.
Clik here to view.
Automatically archiving logs
Using NLog we can achieve log files archiving with some simple config, also the max number of logs we want to store.
For example I would like to store only 5 Mb of log files for my module, to make it simple I would have max 5 files. When the 6th is created the very first one is deleted, simple just means it's old and I don't need it.
The code to achieve this would be as follows (considering I have NLog and nLog config nuget packages), change 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="false" internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log"><targets><target xsi:type="File" name="alexTestLoggerTarget" fileName="${basedir}/logs/AlexTestLogger.txt" archiveAboveSize="1000000" maxArchiveFiles="4"/></targets><rules><logger name="AlexTestLogger" minlevel="Trace" writeTo="alexTestLoggerTarget"/></rules></nlog>
And my output logs would be:
Image may be NSFW.
Clik here to view.
Clik here to view.
Github
I posted this demo on github, so you can run it and see live how the logs are archived: