Documentation Home
MySQL Connector/Net Developer Guide
Related Documentation Download this Manual
PDF (US Ltr) - 5.5Mb
PDF (A4) - 5.5Mb
EPUB - 4.7Mb
HTML Download (TGZ) - 4.6Mb
HTML Download (Zip) - 4.6Mb

MySQL Connector/Net Developer Guide  /  Connector/Net Programming  /  Using the MySQL Connector/Net Trace Source Object

6.16 Using the MySQL Connector/Net Trace Source Object

MySQL Connector/Net 6.2 introduced support for .NET 2.0 compatible tracing, using TraceSource objects.

The .NET 2.0 tracing architecture consists of four main parts:

  • Source - This is the originator of the trace information. The source is used to send trace messages. The name of the source provided by MySQL Connector/Net is mysql.

  • Switch - This defines the level of trace information to emit. Typically, this is specified in the app.config file, so that it is not necessary to recompile an application to change the trace level.

  • Listener - Trace listeners define where the trace information will be written to. Supported listeners include, for example, the Visual Studio Output window, the Windows Event Log, and the console.

  • Filter - Filters can be attached to listeners. Filters determine the level of trace information that will be written. While a switch defines the level of information that will be written to all listeners, a filter can be applied on a per-listener basis, giving finer grained control of trace information.

To use tracing MySql.Data.MySqlClient.MySqlTrace can be used as a TraceSource for MySQL Connector/Net and the connection string must include "Logging=True".

To enable trace messages, configure a trace switch. Trace switches have associated with them a trace level enumeration, these are Off, Error, Warning, Info, and Verbose.

MySqlTrace.Switch.Level = SourceLevels.Verbose;

This sets the trace level to Verbose, meaning that all trace messages will be written.

It is convenient to be able to change the trace level without having to recompile the code. This is achieved by specifying the trace level in application configuration file, app.config. You then simply need to specify the desired trace level in the configuration file and restart the application. The trace source is configured within the system.diagnostics section of the file. The following XML snippet illustrates this:

      <source name="mysql" switchName="MySwitch"
              switchType="System.Diagnostics.SourceSwitch" />
      <add name="MySwitch" value="Verbose"/>

By default, trace information is written to the Output window of Microsoft Visual Studio. There are a wide range of listeners that can be attached to the trace source, so that trace messages can be written out to various destinations. You can also create custom listeners to allow trace messages to be written to other destinations as mobile devices and web services. A commonly used example of a listener is ConsoleTraceListener, which writes trace messages to the console.

To add a listener at runtime, use code such as the following:

ts.Listeners.Add(new ConsoleTraceListener());

Then, call methods on the trace source object to generate trace information. For example, the TraceInformation(), TraceEvent(), or TraceData() methods can be used.

Download this Manual
PDF (US Ltr) - 5.5Mb
PDF (A4) - 5.5Mb
EPUB - 4.7Mb
HTML Download (TGZ) - 4.6Mb
HTML Download (Zip) - 4.6Mb
User Comments
Sign Up Login You must be logged in to post a comment.