One of the main jobs that the ORM software should handle is communicating with the database server and generating SQL statements for reading and writing data. Undoubtedly, the performance of each application developed by using an ORM tool depends on the generated SQL. It is a responsibility of the ORM software to optimize each statement and avoid unnecessary queries. So, when you are facing a difficult choice of an O/R Mapper, it is always meaningful to see and compare how your data is treated by different products. However, not all database engines are distributed with additional profiling software for observing the database events and tracing.
Telerik OpenAccess ORM has an integrated mechanism for tracing and logging the communication between the application and the database server. This includes tracing of the generated SQL statements. This post is focused on the different ways to use this functionality.
There are three available approaches for observing the events that can be used in different cases:
First of all, to enable the tracing, open the Backend Configuration dialog from the Telerik -> OpenAccess -> Configuration menu. Then scroll to the Logging node and enable the Event tracing option. Another setting that is common for all tracing approaches is the Log level. It determines how detailed the log will be. The available logging levels are as follows:
In our case, to trace the SQL statements, the normal level is detailed enough.
The API of Telerik OpenAccess ORM provides the TraceAdapter class which can be used to trace events dynamically and process them in the code. To use the class, another “Listener” class is required. In the demo project related to this post a custom OpenAccessTracer class is implemented. It derives from System.Diagnostics.TraceListener and overrides some of its methods. In this case, the tracer class writes output messages to a TextBox control and counts the executed SQL statements. Of course, you can implement any custom logic to integrate the tracer into your application.
The 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 tracer class, just create an instance and initialize it.
To enable logging to the console, set the Write Log Output to Console option to True. Then while debugging the application, the events will be shown in the Output window of Visual Studio.
To log the events to a text file, open the Backend Configuration dialog, enable the Write events to text file option and specify the file name. The “.txt” extension will be added automatically. Note that the path specified in the file name can be either absolute or relative to the path of the application.
The demo application used in this topic has enabled all the three methods described above simultaneously. The full source code is available for download in our Code library.
Hope you will be satisfied with the results when you test it yourself. We are looking forward to getting your feedback.
Subscribe to be the first to get our expert-written articles and tutorials for developers!