跟踪类-如何通过代码设置自动刷新

fer*_*ega 5 c# trace system.diagnostics

我想将AutoFlush属性设置为true,但是我需要通过代码来实现。以编程方式。

我已经找到了如何配置跟踪元素以及跟踪类的AutoFlush属性的方法。

然后,我有以下代码来获取TraceSource:

private static TraceSource GetTraceSource()
{
    var ts = new TraceSource("TraceManager")
        {
            Switch =
                {
                    Level = SourceLevels.All
                }
        };
    ts.Attributes.Add("AutoFlush", "true");
    ts.Listeners.Remove("Default");

    var file = System.IO.Path.GetTempPath() + @"\MyApplication.log";
    var textListener = new TextWriterTraceListener(file)
        {
            Filter = new EventTypeFilter(SourceLevels.All)
        };

    ts.Listeners.Add(textListener);
    return ts;
}
Run Code Online (Sandbox Code Playgroud)

如何在此代码内将AutoFlush属性设置为true?

谢谢。

Pau*_*aul 5

尝试添加此...

Trace.AutoFlush = true;
Run Code Online (Sandbox Code Playgroud)