سؤال

I am using NLog for the first time in a winForm app which will be running for years may be. This app is also logging data in a text file. As the the app will be running for years so the text file will grow and grow. That’s why I want to limit the size of text file. Lets say when 10MB limit is reached the text file starts deleting the last entries to accommodate for the new data. How can I do it with NLog.

Some where in my c# project I have

private static Logger logger = LogManager.GetCurrentClassLogger();

logger.Trace("Error:.........."); // and similar msgs

My NLog.config is (it just logs data to a text file)

<?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">

  <targets>
    <target name="logfile" xsi:type="File" fileName="${basedir}/file.txt" />
  </targets>

  <rules>
    <logger name="*" minlevel="Info" writeTo="logfile" />
  </rules>
</nlog>
هل كانت مفيدة؟

المحلول

You can limit file size to 10MB and set max arhive files count to 1:

<target xsi:type="File"
      name="file"
      layout="${longdate}|${level:uppercase=true}|${logger}|${message}"        
      archiveAboveSize="10000000"
      maxArchiveFiles="1"
      archiveFileName="${basedir}/log_archived.txt"
      fileName="log.txt" />

When file will extend 10MB, it will be archived, and logging will continue to new file. When new file will extend 10MB, it will replace archived file. So, you will have two files - current and archived.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top