Question

Is there any way to configure NLog to log information per application session? As of now it appends the messages in the log file each time application is executed (WinForm). What we'd like to have is to only store the info of the current session. Meaning that when application launches, all previous messages are cleared before any new message is logged. This way only the messages of current sessions will be available in the log file.

Here is the current configuration

 <?xml version="1.0"?>
<nlog autoReload="true" xmlns="http://www.nlog-project.org/schemas/NLog.xsd"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <targets>
    <target name="FileTarget" xsi:type="File" fileName="MainLogFile.txt" layout="${longdate} ${callsite} ${level} ${message}"/>/>
  </targets>
  <rules>
    <logger name="*" levels="Trace,Info,Warn,Error,Debug,Fatal" writeTo="FileTarget"/>
  </rules>
</nlog>

Thanks

Was it helpful?

Solution

Assuming that you can only have one instance of your application open at once, you can just use the deleteOldFileOnStartup parameter:

  <targets>
    <target name="FileTarget" xsi:type="File" fileName="MainLogFile.txt" layout="${longdate} ${callsite} ${level} ${message}" deleteOldFileOnStartup="true">
  </targets>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top