
For example, you might want to ensure that critical messages are always sent to a text file, regardless of the current configuration settings. The application can dynamically change the properties set by the configuration file to override any settings specified by the user. The configuration file initializes the settings for the trace source at the time the application is initialized. This results in some messages being written by only one of the two listeners. The filters identified for the two listeners are initialized with different source levels. Two techniques are shown for adding trace listeners: adding the listener directly to the trace source and adding a listener to the shared listeners collection and then adding it by name to the trace source. In addition to configuring the trace listeners, the configuration file creates filters for both listeners and creates a source switch for the trace source. Replace the default configuration file content with the following settings to initialize a console trace listener and a text writer trace listener for the trace source that was created in step 1.
#Itrace new user instructions code
MySource.TraceInformation("Informational message.")Īdd an application configuration file, if one is not present, to the project to initialize the trace source named TraceSourceApp in the code example in step 1.

MySource.TraceEvent(TraceEventType.Error, 4, "Error message.") MySource.TraceEvent(TraceEventType.Critical, 3, "Critical message.")

MySource.TraceEvent(TraceEventType.Warning, 2, "Warning message.") MySource.TraceEvent(TraceEventType.Error, 1, "Error message.") MySource.Listeners("console").Filter = configFilter ' Issue a warning and a critical message. ' If you do not change the switch level, the event filter ' any settings in the configuration file. ' Allow the trace source to send messages to listeners MySource.Listeners("console").Filter = New EventTypeFilter(SourceLevels.Warning) ' Create a new event type filter that ensures ' Save the original settings from the configuration file.ĭim configFilter As EventTypeFilter = CType(mySource.Listeners("console").Filter, EventTypeFilter) Private Shared mySource As New TraceSource("TraceSourceApp") MySource.TraceInformation("Informational message.") MySource.TraceEvent(TraceEventType.Error, 4, MySource.TraceEvent(TraceEventType.Critical, 3, MySource.TraceEvent(TraceEventType.Warning, 2, MySource.TraceEvent(TraceEventType.Error, 1, Issue a warning and a critical message.

If you do not change the switch level, the event filter any settings in the configuration file.

Allow the trace source to send messages to listeners New EventTypeFilter(SourceLevels.Warning) Create a new event type filter that ensures Save the original settings from the configuration file. This code logs errors and warnings and outputs some of them to the console, and some of them to the myListener file that is created by the entries in the configuration file. To create and initialize a trace source using a configuration fileĬreate a Visual Studio console application project (.NET Framework) and replace the supplied code with the following code. However, we recommend that you use configuration files to facilitate the reconfiguration of the traces produced by trace sources at run time. This topic provides instructions for both options. Trace output from TraceSource can be created and initialized with or without the use of configuration files. TraceSource provides tracing methods that allow you to easily trace events, trace data, and issue informational traces. The TraceSource class is used by applications to produce traces that can be associated with the application.
