简体   繁体   English

每次运行后清除与tracelistener关联的日志?

[英]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. 我正在使用TextWriterTraceListener进行日志记录,使用app.config将其配置到应用程序中,如下所示。

<?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. 最好的方法是从TextWriterTraceListener类派生自己的Custom TraceListener。 Here's a sample that rolls a new log file at every interval. 这是一个每隔一段时间滚动一个新日志文件的示例 You should be able to modify this implementation. 您应该能够修改此实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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