Telerik OpenAccess ORM

Telerik OpenAccess ORM Send comments on this topic.
How to: Create a Custom TraceListener
See Also
Programmer's Guide > OpenAccess ORM Classic (Old API) > OpenAccess Tasks > Tracing and Logging > How to: Create a Custom TraceListener

Glossary Item Box

The Telerik OpenAccess ORM API provides the TraceAdapter class from the Telerik.OpenAccess.Diagnostic namespace. This class can be used to trace events dynamically and process them in the code. To use the class, another class deriving from TraceListener is required. The following code-snippet shows you the custom OpenAccessTraceListener class. It derives from the abstract System.Diagnostic.TraceListener and overrides some of its methods. In this example, the tracer class writes output messages to a TextBox control and counts the executed SQL Statements. Of course, you can implement your own custom logic to integrate the tracer into your application. The most important step here is to add the tracer to the Listeners collection of the singleton Telerik.OpenAccess.Diagnostics.TraceAdapter instance. This can be done in the constructor of the class. The WriteLine method filters the messages and prints only the SQL statements. They can be easily recognized because they start with the "driver.stat.exec" string. To use the custom traces class, just create an instance and initialize it.

C# Copy Code
using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
namespace TraceListenerDemo
{
   
public class OpenAcecssTraceListener : TraceListener
   {
       
private int id;
       
private int count;
       
private TextBox textBox;
       
private Thread myThread;
       
public OpenAcecssTraceListener( TextBox textBox ) :
           
base( "Telerik OpenAccess ORM" )
       {
           
this.myThread = Thread.CurrentThread;
           
this.textBox = textBox;
           
this.count = 0;
           Telerik.OpenAccess.Diagnostics.TraceAdapter.Instance.Level =
"4";
           
this.id = Telerik.OpenAccess.Diagnostics.TraceAdapter.Instance.Listeners.Add( this );
       }
       
public override void Write( string message )
       {
           
this.WriteLine( message );
       }
       
public override void WriteLine( string message )
       {
           
if ( Object.ReferenceEquals( this.myThread, Thread.CurrentThread ) == false )
               
return;
           
if ( message.StartsWith( "driver.stat.exec" ) )
           {
               count++;
               
int index;
               
if ( message.IndexOf( "SELECT" ) >= 0 )
                   index = message.IndexOf(
"SELECT" );
               
else
                   
index = message.IndexOf( "INSERT" );
               
this.textBox.Text += String.Format( "{0}: {1}\r\n", count.ToString(), message.ToString().Substring( index ) );
               
this.textBox.SelectionStart = this.textBox.Text.Length;
               
this.textBox.SelectionLength = 0;
               
this.textBox.ScrollToCaret();
           }
       }
       
internal void Reset()
       {
           
this.textBox.Text = String.Empty;
           
this.count = 0;
       }
       
protected override void Dispose( bool disposing )
       {
           
this.textBox.Text += this.count.ToString() + " quires executed";
           
base.Dispose( disposing );
       }
   }
}

See Also