jwood803
8/24/2013 - 5:45 PM

.NET files to show how to use and configure TraceSource for logging.

.NET files to show how to use and configure TraceSource for logging.

namespace TraceSourceLogging
{
  using System.Diagnostics;

  class Program
  {
    private static TraceSource traceSource = new TraceSource("Log");

    static void Main(string[] args)
    {
       traceSource.TraceData(TraceEventType.Verbose, 1, "Now we're logging!");
       traceSource.Flush();
       traceSource.Close();
    }
  }
}
<?xml version="1.0"?>
<configuration>
  <system.diagnostics>
    <sources>
      <source name="Log" switchValue="Information" switchType="System.Diagnostics.SourceSwitch">
        <listeners>
          <add name="myListener"/>
          <remove name="Default"/>
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add name="myListener"
        type="System.Diagnostics.TextWriterTraceListener"
        initializeData="myListener.log">
      </add>
    </sharedListeners>
  </system.diagnostics>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
</configuration>
<source name="Log" switchValue="Off" switchType="System.Diagnostics.SourceSwitch">
  <listeners>
    <add name="myListener"/>
    <remove name="Default"/>
  </listeners>
</source>
<system.diagnostics>
  <sources>
    <source name="Log" switchValue="Information" switchType="System.Diagnostics.SourceSwitch">
      <listeners>
        <add name="myListener"/>
        <remove name="Default"/>
      </listeners>
    </source>
    <source name="EmployeeLog" switchValue="Information" switchType="System.Diagnostics.SourceSwitch">
      <listeners>
        <add name="myListener"/>
        <remove name="Default"/>
      </listeners>
    </source>
    <source name="PersonLog" switchValue="Information" switchType="System.Diagnostics.SourceSwitch">
      <listeners>
        <add name="myListener"/>
        <remove name="Default"/>
      </listeners>
    </source>
  </sources>
  <sharedListeners>
    <add name="myListener"
      type="System.Diagnostics.TextWriterTraceListener"
      initializeData="myListener.log">
    </add>
  </sharedListeners>
</system.diagnostics>
public class Person
{
    private static readonly TraceSource traceSource = new TraceSource("PersonLog");

    public string FirstName { get; set; }
    public string LastName { get; set; }

    public bool IsValid()
    {
        var isValid = (!string.IsNullOrWhiteSpace(this.FirstName) || !string.IsNullOrWhiteSpace(this.LastName));
        traceSource.TraceInformation(string.Format("Is {0} {1} valid - {2}", this.FirstName, this.LastName, isValid));
        return isValid;
    }
}

public class Employee : Person
{
    private static readonly TraceSource traceSource = new TraceSource("EmployeeLog");

    public string Department { get; set; }
    public Person Manager { get; set; }
    public bool IsActive { get; set; }

    public bool IsDepartmentValid()
    {
        var isValid = !string.IsNullOrWhiteSpace(this.Department);
        traceSource.TraceInformation(string.Format("Is the {0} department valid - {1}", this.Department, isValid));
        return isValid;
    }
}