Trace Class - How to set Autoflush by code
Asked Answered
S

1

6

I would like to set the AutoFlush attribute to true, but I need to do it by code. Programmatically.

I have found this how to configure the trace element and also the AutoFlush property of the Trace Class.

Then I have this code to get the 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;
}

How can I set the AutoFlush property to true inside this code ?

Thanks.

Slack answered 22/3, 2013 at 16:48 Comment(0)
I
6

Try adding this...

Trace.AutoFlush = true;
Internode answered 22/3, 2013 at 16:57 Comment(3)
Hi @Paul, but Trace is not a static class... is it ? :-OSlack
Correct but all of its properties and methods are static so no instance is needed to be referenced. The same applies to Trace.WriteLine and other methods.Internode
Opps. I hadn't seen the class methods. :-\ Thanks again.Slack

© 2022 - 2024 — McMap. All rights reserved.