Configure archival and purging of log file in Nlog

 In previous article "Implement Logging in Sql Server Database using Nlog in .net core MVC application - part 3 "  I have demonstrated that how we can implement Logging in sql server database using Nlog in .core mvc application. In previous article I have logged the exceptions in sql server database, in this article I will show you how you can configure archival and purging of log file by modifying the few settings in Nlog.config file. Archival and purging of logs will help us to remove the outdated log files from the server and keep server storage junk free.

Lets configure the Nlog.config file to achieve the archival and purging of log file.

 <?xml version="1.0" encoding="utf-8" ?>  
 <nlog xmlns=""  
    xsi:schemaLocation=" NLog.xsd"  
    internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">  
  <variable name="myvar" value="myvalue"/>  
   <target xsi:type="File" name="logError" fileName="${basedir}/logs/${shortdate}.log"  
       layout="${longdate} ${uppercase:${level}} ${message}" />  
   <target xsi:type="File" name="logErrorWithCSV" fileName="${basedir}/logs/${shortdate}.csv"  
    <layout xsi:type="CsvLayout" delimiter="Pipe" withHeader="true">  
     <column name="Date" layout="${longdate}" />  
     <column name="level" layout="${level:upperCase=true}"/>  
     <column name="message" layout="${message}" />  
     <column name="exception" layout="${exception:format=ToString}"/>  
     <column name="stacktrace" layout="${stacktrace:topFrames=10}" />  
     <column name="CustomProperty" layout="${event-properties:CustomProperty}"/>  
   <target xsi:type="Database"  
   dbProvider="Microsoft.Data.SqlClient.SqlConnection, Microsoft.Data.SqlClient"  
   connectionString="Data Source=DESKTOP-IA0LSSV\SQLEXPRESS;Initial Catalog=Emp;User Id=sa;Password=12345"  
   commandText="INSERT INTO AppException(Date,ExceptionMessage,level,exception,stacktrace,CustomProperty) VALUES (@Date,@ExceptionMessage,@level,@exception,@stacktrace,@CustomProperty)">  
    <parameter name="@Date" layout="${date}" />  
    <parameter name="@level" layout="${level:upperCase=true}"/>  
    <parameter name="@ExceptionMessage" layout="${message}" />  
    <parameter name="@exception" layout="${exception:format=ToString}"/>  
    <parameter name="@stacktrace" layout="${stacktrace:topFrames=10}" />  
    <parameter name="@CustomProperty" layout="${event-properties:CustomProperty}"/>  
   <logger name="ErrorLogFile" minlevel="Debug" writeTo="logError" />  
   <logger name="logErrorWithCSV" minlevel="Debug" writeTo="logErrorWithCSV" />  
   <logger name="logErrorWithSqlServer" minlevel="Trace" writeTo="logErrorWithSqlServer" />  

Lets discuss the setting added in NLog.config file.

  • archiveFileName: This setting will be file name of archive file.
  • archiveEvery: This is the frequency of archieval of files. It can have settings as per below list
    •     None : Don't archive based on time.
    •     Year : Archive every year.
    •     Month: Archive every month.
    •     Day  :  Archive daily.
    •     Hour :  Archive every hour.
    •     Minute: Archive every minute. 
  •  maxArchiveFiles: It will accept the number of files to be keep on the server indirectly it is setting for purging
  • enableArchiveFileCompression: This setting enables the archive file to be compressed as zip (Note: you need to mention the zip extension in archiveFileName setting)

After modification in Nlog.config below is the output when i executed the application.



  • Archival file generated as per below screenshot

NLog Archival 1

  • another file generated

NLog Archival 2

  • All three files generated

NLog Archival 3

you can see in above screenshot file no 0 is deleted by nlog automatically because i have set the maxArchiveFiles file to 3.


This way you can easily configure archival and purging of log file using Nlog.


Popular posts from this blog

Implement Logging in CSV file using Nlog in .net core MVC application- part 2

Implement Nlog in .Net core MVC application part 1

Angular User Session Timeout example step by step

Devexpress Datebox date formatting in angular 6 with example

Disable backspace key using Jquery