Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trace Class - How to set Autoflush by code

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.

like image 370
ferpega Avatar asked Mar 22 '13 16:03

ferpega


1 Answers

Try adding this...

Trace.AutoFlush = true;
like image 122
Paul Avatar answered Sep 22 '22 23:09

Paul