Search code examples
visual-studioasp.net-mvc-5nlogerror-loggingarchiving

Visual Studio: Archive the contents of a text file once a month


Working with Visual Studio 2013 ASP .NET MVC 5, I have this .txt file in which I log the exceptions that are caught in my web application (using NLog). Every month, I want to archive that file into a different folder in my project. How can I do this? Thanks


Solution

  • I think this could do

    <?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"
          autoReload="true" internalLogFile="c:/logs/nlog_ex.txt" internalLogLevel="Debug">
    
      <!-- 
      See http://nlog-project.org/wiki/Configuration_file 
      for information on customizing logging rules and outputs.
       -->
      <targets>
        <target xsi:type="File"
                name="exceptions"
                encoding="utf-8"
                lineEnding="Default"
                layout="${longdate} - ${message}"
                archiveFileName="c:/logs/${date:format=yyyy-MM}/exceptions.txt"
                archiveNumbering="Sequence"
                archiveEvery="Month"
                fileName="c:/logs/exceptions.txt"
                deleteOldFileOnStartup="false"
                enableFileDelete="true"
                createDirs="true"
                concurrentWrites="true"
                autoFlush="true"
                keepFileOpen="false"
        />
      </targets>
    
      <rules>
        <!-- add your logging rules here -->
        <logger name="*" minlevel="Info" writeTo="exceptions" />
      </rules>
    </nlog>
    

    I used this domcumentation.