TraceSource defined in App.config not used c#

35 Views Asked by At

My goal is to log errors and information in my C# command line application to log files. To not reinvent the wheel I used some examples to activate TraceSource in my project using the App.config file.

This is my App.config file, which should create two log files, one for errors and one for level information. The console listener is not required, it's only included for testing.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.diagnostics>
        <sources>
            <source name="BrokerTrace"
                    switchValue="All">
                <listeners>
                    <add name="Console"
                         type="System.Diagnostics.ConsoleTraceListener"
                         traceOutputOptions="DateTime">
                        <filter type="System.Diagnostics.EventTypeFilter"
                                initializeData="Error"/>
                    </add>
                    <add name="FileError"
                         type="System.Diagnostics.TextWriterTraceListener"
                         traceOutputOptions="DateTime"
                         initializeData="BrokerError.log">
                        <filter type="System.Diagnostics.EventTypeFilter"
                                initializeData="Error"/>
                    </add>
                    <add name="FileInfo"
                         type="System.Diagnostics.TextWriterTraceListener"
                         traceOutputOptions="DateTime"
                         initializeData="BrokerInfo.log" >
                        <filter type="System.Diagnostics.EventTypeFilter"
                                initializeData="Info"/>
                    </add>
                    <remove name="Default" />
                </listeners>
            </source>
        </sources>
    </system.diagnostics>
</configuration>

Inside the C# code of the project I create the instance of the TraceSource and log events:

private static TraceSource ts = new TraceSource("BrokerTrace");

private static async Task Main()
{
    ts.TraceEvent(TraceEventType.Information, 1, "Start application");
    ts.TraceEvent(TraceEventType.Error, 1, "Start application");
    ts.Flush();
    ts.Close();
}

My expectation is that after the application run there are two files BrokerError.log and BrokerInformation.log created with one entry each. But no log file is created.

What I also tried, is to check the listeners of the TraceSource object. By adding this lines of code at the beginning of the Main method:

foreach(var listener in ts.Listeners) 
{
    Console.WriteLine("TraceSource listener: " + listener.ToString());
}

The result is TraceSource listener: System.Diagnostics.DefaultTraceListener. So I think the app.config file is not correctly loaded and a default TraceSource object is created instead of my definition in the app.config.

What is wrong with my app.config file or with the usage of the TraceSource?

0

There are 0 best solutions below