Logging config data in our application
Now that we have the collection of diagnostic data configured, we need to add code to our application to send diagnostic data to the listeners. We can do this simply by making calls to the methods in the System.Diagnostics.Trace
class (documented at http://msdn.microsoft.com/en-us/library/36hhw2t6.aspx).
One of the more common methods we'll call is Trace.Writeline
, as seen here:
Trace.Writeline("An error has occurred!","Error")
If our filter is set to the value Error
or higher, our message would be logged. An alternative, easier syntax is:
Trace.TraceError("An error has occurred!")
Again, if our filter is set to the value Error
or higher, our message will be logged. The simplified methods are limited to TraceError
, TraceInformation
, and TraceWarning
, whereas the WriteLine
method can be used to log diagnostic data at any level, including custom levels.