Telerik OpenAccess ORM

Telerik OpenAccess ORM Send comments on this topic.
How to: Create a Custom TraceListener
See Also
Programmer's Guide > Developer's Guide > 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 CustomTraceListener
{
   
public class OpenAccessTraceListener : TraceListener
   {
       
private int id;
       
private int count;
       
private TextBox textBox;
       
private Thread myThread;

       
public OpenAccessTraceListener(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);
       }
   }
}
VB.NET Copy Code
Imports System.Threading

Public Class OpenAccessTraceListener
    Inherits TraceListener
    Private id As Integer
    Private count As Integer
    Private _textBox As TextBox
    Private myThread As Thread

    Public Sub New(ByVal _textBox As TextBox)
        MyBase.New("Telerik OpenAccess ORM")
        Me.myThread = Thread.CurrentThread
        Me._textBox = _textBox
        Me.count = 0
        Telerik.OpenAccess.Diagnostics.TraceAdapter.Instance.Level = "4"
        Me.id = Telerik.OpenAccess.Diagnostics.TraceAdapter.Instance.Listeners.Add(Me)
    End Sub

    Public Overloads Overrides Sub Write(ByVal message As String)
        Me.WriteLine(message)
    End Sub

    Public Overloads Overrides Sub WriteLine(ByVal message As String)
        If Object.ReferenceEquals(Me.myThread, Thread.CurrentThread) = False Then
            Return
        End If

        If message.StartsWith("driver.stat.exec") Then
            count += 1
            Dim index As Integer
            If message.IndexOf("SELECT") >= 0 Then
                index = message.IndexOf("SELECT")
            Else
                index = message.IndexOf("INSERT")
            End If

            Me._textBox.Text += String.Format("{0}: {1}" & vbCrLf, count.ToString(), message.ToString().Substring(index))
            Me._textBox.SelectionStart = Me._textBox.Text.Length
            Me._textBox.SelectionLength = 0
            Me._textBox.ScrollToCaret()
        End If
    End Sub

    Friend Sub Reset()
        Me._textBox.Text = String.Empty
        Me.count = 0
    End Sub

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        Me._textBox.Text += Me.count.ToString() & " quires executed"
        MyBase.Dispose(disposing)
    End Sub
End Class

See Also