简体   繁体   中英

Clearing the log associated with a tracelistener after each run?

I'm using TextWriterTraceListener for logging, which is being configured into the app using app.config as shown below.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.diagnostics>
    <trace autoflush="true" indentsize="100"  >
      <listeners>
        <add name="myListener" type="System.Diagnostics.TextWriterTraceListener" 
             initializeData="MyLog.log" />
        <remove name="Default" />
      </listeners>
    </trace>
  </system.diagnostics>
</configuration>

How do I ensure that the logging starts afresh (clearing the previous contents) after each run, rather than appending to the contents to the existing log ?

The best way would be to derive your own Custom TraceListener from the TextWriterTraceListener class. Here's a sample that rolls a new log file at every interval. You should be able to modify this implementation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM