Introduction

In this article, we are going to see a log printer working with Entity Framework that converts SqlCommand to a completely executable T-SQL statement, including all parameter declarations and write to log file. Once a command is executed, You will see following message like this in your log file.

From the naming of these methods, you can already tell their purpose and execution order. For example,NonQueryExecuting() will be invoked when a non-query command is about to run. And NonQueryExecuted()method will be invoked once the command is done. In each-ingmethod, we initialize a Stopwatchinstance and assign it to interceptionContext, and retrieve it later in corresponding -ed method. This allows us to measure the time taken by the command and include it to log message.

To enable the interceptor, we need to add a piece of configuration to <entityFramework>section in our Web.config, as following snippet shows.

Note that we have a TraceSource field which is to be used to accept log messages. You can replace it with whatever you desire to use.

Format Basic Type Parameters

Next thing to do is to handle every parameter used by the SqlCommandinstance and format it in a T-SQL Statement. In order to achieve that, we need to figure out its SqlDbType and choose correct string format accordingly.

Format Structured Type Parameters

Structured type parameters are a bit complicated to handle. Structured type is defined in SQL Server, and is usually used to pass array to stored procedure. On application side, we need to create a DataTableinstance and add one or more DataRowinstances to it. In T-SQL, the statement consists of a DECLARE statement and one or more INSERT statements. In order to print structured type parameters, we need to obtain all columns and row, then choose correct format for each cell by its data type. In this article, we use GetTypeCodemethod.

Produce Complete SQL Statement

In SqlLogInterceptor, now we can produce complete SQL statement as well as execution time, exception and result in every -edmethod, and write the log message. Apart from log message, we also check Exceptionproperty to decide message level.

Conclusion

Entity Framework provides an accessible way to monitor each command. The code in this article takes advantage of it and produces executable T-SQL statements in log file. We can directly copy them, and debug them on SQL Server Management Studio, which saves our time of debugging.

In this article, we are going to see a log printer working with Entity Framework that converts SqlCommand to a completely executable T-SQL statement, including all parameter declarations and write to log file. Once a command is executed, You will see following message like this in your log file.

Posted on 20-03-2018

Comment:

To comment you must be logged in members.

Files with category

This sound player project use the libZPlay from http://libzplay.sourceforge.net . Methods are called from library libzplay.dll with [InteropServices::DllImport("")]. The use of the library is relatively easy.

Hello World...!!! Though I am not an expert programmer but today i want to share my little knowledge of C/C++ programming by sharing this simple Quiz Management System with you people. I hope this may help some students in their mini...

A CRON expression is a string composed of six fields (in some implementation seven), separated by a whites space, representing a time schedule. CRON expressions are used in various job schedulers (such as the Linux job scheduler, the Quartz...

This is a simple project on departmental store management system developed in c++ programming language . This project helps to keep all the information details of the items available in departmental store and maintains the stock and prints the bill....

Code Features: 1. Find the Volume of a cone 2. Find the value of other properties of the cone 3. Teach user how to compute the volume of the cone manually Thanks to following for the code: John Carlo C. Nañoz, Dondon Bagasbas, Gabby Peñones