4 ways to organize your NLog files

Organizing your log file(s) can be very important, depending on the software requirements different structures can be the right fit for you. Here I’ll present four different ways for structuring NLog log files so you can get the idea how to configure yours and tailor them by your needs.

nlog-sample-config.png

Using one file

All you need to think of here is the name of your log file. In the example bellow I’ve named the file MyApp.log and it will be located in the logs folder. The downside of this approach is the file might became large very fast (even hit the OS size limitations) and tracing events that happened at a different timestamp could be turned into endless browse through all the history of the logs.

<?xml version=”1.0″ encoding=”utf-8″ ?>
<nlog xmlns=”http://www.nlog-project.org/schemas/NLog.xsd&#8221; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”&gt;
<targets>
<target name=”logfile” xsi:type=”File” fileName=”logs\MyApp.log” />
<target name=”logconsole” xsi:type=”Console” />
</targets>
<rules>
<logger name=”*” minlevel=”Info” writeTo=”logconsole” />
<logger name=”*” minlevel=”Debug” writeTo=”logfile” />
</rules>
</nlog>

One log file per day in the same folder

This is an improvement of the first example because we create a separate log file for each day and in this example they are all going to be stored in the logs folder. The name of the file will be the date for which the log records are.

This approach has it’s advantages but we still have the problem of the log files becoming too big and that is a real scenario for larger scale applications.
<?xml version=”1.0″ encoding=”utf-8″ ?>
<nlog xmlns=”http://www.nlog-project.org/schemas/NLog.xsd&#8221;
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”&gt;
<targets>
<target name=”logfile” xsi:type=”File” fileName=”logs\${shortdate}.log” />
<target name=”logconsole” xsi:type=”Console” />
</targets>
<rules>
<logger name=”*” minlevel=”Info” writeTo=”logconsole” />
<logger name=”*” minlevel=”Debug” writeTo=”logfile” />
</rules>
</nlog>

One log file organized per dates into folder structure

This is similar as the previous example just in this case we are not using different file names but we are creating different folders for different dates and in each of those folders we’ll have a log file MyApp.log.

The problem with log files became too large still remains.
<?xml version=”1.0″ encoding=”utf-8″ ?>
<nlog xmlns=”http://www.nlog-project.org/schemas/NLog.xsd&#8221;
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”&gt;
<targets>
<target name=”logfile” xsi:type=”File”
fileName=”logs\${date:format=yyyy/MM/dd}\MyApp.log” />
<target name=”logconsole” xsi:type=”Console” />
</targets>
<rules>
<logger name=”*” minlevel=”Info” writeTo=”logconsole” />
<logger name=”*” minlevel=”Debug” writeTo=”logfile” />
</rules>
</nlog>

Archive file after reaching a certain file size

To achieve a size limit we’ll need to archive the log file after it reaches some size. We’ll need to specify archiveFileName and archiveAboveSize. So when the file reaches a certain size (archiveAboveSize) it will be moved to another file (archiveFileName).

Here is an example of such a config.

<?xml version=”1.0″ encoding=”utf-8″ ?>
<nlog xmlns=”http://www.nlog-project.org/schemas/NLog.xsd&#8221;
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”&gt;
<targets>
<target name=”logfile” xsi:type=”File” fileName=”logs\${date:format=yyyyMMdd}.log” archiveFileName=”logs\${date:format=yyyyMMddhhmmss}.log” />
<target name=”logconsole” xsi:type=”Console” />
</targets>
<rules>
<logger name=”*” minlevel=”Info” writeTo=”logconsole” />
<logger name=”*” minlevel=”Debug” writeTo=”logfile” />
</rules>
</nlog>

Hope this will be useful for you.

Happy coding,

J.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s