https://github.com/ttakahari/adonetprofiler
Profiling database access with ADO.NET
https://github.com/ttakahari/adonetprofiler
ado csharp profile
Last synced: about 1 year ago
JSON representation
Profiling database access with ADO.NET
- Host: GitHub
- URL: https://github.com/ttakahari/adonetprofiler
- Owner: ttakahari
- License: mit
- Created: 2015-10-20T07:11:49.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2020-06-25T12:42:44.000Z (almost 6 years ago)
- Last Synced: 2025-04-21T11:42:41.253Z (about 1 year ago)
- Topics: ado, csharp, profile
- Language: C#
- Size: 1.14 MB
- Stars: 12
- Watchers: 4
- Forks: 7
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# AdoNetProfiler
Profiler for ADO.NET with hooking ```DbConnection```, ```DbCommand```, ```DbDataReader```, ```DbTransaction```.
Although threre is already MiniProfiler as similar, I make this because I think it lacks hook points.
AdoNetProfiler can profile the state of ```DbConnection```, ```DbCommand``` and ```DbTransaction```.
[](https://ci.appveyor.com/project/ttakahari/adonetprofiler)
[](https://www.nuget.org/packages/AdoNetProfiler/)
## Install
from NuGet - AdoNetProfiler
```ps1
PM > Install-Package AdoNetProfiler
```
## How to use
In stead of creating the connection instance directly from ```SqlConnection``` or others, create from ```AdoNetProfilerDbConnection``` with giving the original connection instance and your profiler implementing ```IAdoNetProfiler``` as arguments.
```csharp
using (var connection = new AdoNetProfilerConnection(new SqlConnection("Your connection string"), new YourProfiler()))
{
// write data-access logics here.
}
```
Or, if initalizing ```AdoNetProfilerFactory``` when the application starts, it is available from ```DbProviderFctories```.
```csharp
// when the application starts.
AdoNetProfilerFactory.Inialize(typeof(YourProfiler)).
// data-access logic
var factory = DbProviderFactories.GetFactory("System.Data.SqlClient").
using (var connection = factory.CreateConnection())
{
connection.ConnectionString = "[Your connection string]";
// write data-access logics here.
}
```
## Profiler
```IAdoNetProfiler``` has the following contents.
```csharp
///
/// The interface that defines methods profiling of database accesses.
///
public interface IAdoNetProfiler
{
///
/// Get the profiler is enabled or not.
///
bool IsEnabled { get; }
///
/// Execute before the connection open.
///
/// The current connection.
void OnOpening(DbConnection connection);
///
/// Execute after the connection open.
///
/// The current connection.
void OnOpened(DbConnection connection);
///
/// Execute before the connection close.
///
/// The current connection.
void OnClosing(DbConnection connection);
///
/// Execute after the connection close.
///
/// The current connection.
void OnClosed(DbConnection connection);
///
/// Execute before the connection creates the transaction.
///
/// The current connection.
void OnStartingTransaction(DbConnection connection);
///
/// Execute after the connection creates the transaction.
///
/// The created transaction.
void OnStartedTransaction(DbTransaction transaction);
///
/// Execute before the transaction commits.
///
/// The current transaction.
void OnCommitting(DbTransaction transaction);
///
/// Execute after the transaction commits.
///
/// The current connection.
void OnCommitted(DbConnection connection);
///
/// Execute before the transaction rollbacks.
///
/// The current transaction.
void OnRollbacking(DbTransaction transaction);
///
/// Execute after the transaction rollbacks.
///
/// The current connection.
void OnRollbacked(DbConnection connection);
///
/// Execute before executes.
///
/// The executing command.
void OnExecuteReaderStart(DbCommand command);
///
/// Execute after reads.
///
/// The stream from data source.
/// The number of read data.
void OnReaderFinish(DbDataReader reader, int record);
///
/// Execute before executes.
///
/// The executing command.
void OnExecuteNonQueryStart(DbCommand command);
///
/// Execute after executes.
///
/// The executing command.
/// The result of executing .
void OnExecuteNonQueryFinish(DbCommand command, int executionRestlt);
///
/// Execute before executes.
///
/// The executing command.
void OnExecuteScalarStart(DbCommand command);
///
/// Execute after executes.
///
/// The executing command.
/// The result of executing .
void OnExecuteScalarFinish(DbCommand command, object executionRestlt);
///
/// Execute when the error occurs while the command executing.
///
/// The executing command.
/// The occurred error.
void OnCommandError(DbCommand command, Exception exception);
}
```
Implementing this interface, you can get your profiler.
An instance of `IAdoNetProfiler` is not thread-safe. So you should not use an instance of `IAdoNetProfiler` among some instances of `DbConnection`.
## Entity Framework
AdoNerProfiler does not support Entity Framework. When you use AdoNetProfiler with Entity Framework in a same project, some errors may occur becouse of conflicting.
## Glimpse
Glimpse is profiling library for ASP.NET.
AdoNetProfiler has the library for Glimpse.
from NuGet - Glimpse.AdoNetProfiler
```ps1
PM > Install-Package Glimpse.AdoNetProfiler
```
Glimpse.Ado, that has already been provided as a part of Glimpse libaries, displays SQL profilings.
But Glimpse.AdoNetProfiler displays connection and transaction profilings, to say nothing of SQL.




## Lisence
under MIT License