如何使用app.config阻止TextWriterTraceListener追加? [英] How do you stop a TextWriterTraceListener from appending using app.config?

查看:120
本文介绍了如何使用app.config阻止TextWriterTraceListener追加?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用System.Diagnostics.TraceSource进行日志记录,而我的侦听器之一是TextWriterTraceListener.在跟踪入门此处中进行设置如下:

I'm using System.Diagnostics.TraceSource for logging and one of my listeners is a TextWriterTraceListener. In the tracing primer here it sets this up as follows:

<listeners>
  <add initializeData="output.txt" 
       type="System.Diagnostics.TextWriterTraceListener"
       name="myLocalListener" />
</listeners>

问题在于,它将始终附加到output.txt.您如何将其更改为覆盖配置文件中的内容?

The problem is that this will always append to output.txt. How do you alter this to an overwrite in the config file?

以编程方式,我想要的侦听器是:

Programmatically the listener I want is a:

new TextWriterTraceListener(new StreamWriter("output.txt", false));

推荐答案

最简单的解决方案是自行制作.

The simplest solution is to make your own.

我建议您从TextWriterTraceListener继承,并在构造函数中将基本Writer设置为您建议的内容:new StreamWriter("output.txt", false).

I suggest that you inherit from TextWriterTraceListener and in your constructor set the base Writer to what you proposed: new StreamWriter("output.txt", false).

一些示例代码:

public class MyTextWriterTraceListener : TextWriterTraceListener
{
    public MyTextWriterTraceListener(string logFileName)
        : base(logFileName)
    {
        base.Writer = new StreamWriter(logFileName, false);
    }
}

这使您可以从配置文件中使用initializeData参数来指定文件名,或者如果使用代码创建则指定一个文件名.

This lets you take the initializeData parameter from a configuration file to specify the name of the file, or specify one if created in code.

这篇关于如何使用app.config阻止TextWriterTraceListener追加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆