Skip to content

Instrumented-AdoNet is a basic library to be able to instrument adonet calls.

License

Notifications You must be signed in to change notification settings

gavindew/instrumented-adonet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

instrumented-adonet

Instrumented-AdoNet is a basic library to be able to instrument adonet calls.

It does this by wrapping IDbConnection which in turn creates and returned wrapped IDbCommand objects, so that when database methods are called, we can log and intercept errors.

This library borrows the code constructs from https://github.com/MiniProfiler/dotnet to wrap DbConnection and DbCommand with some basic instrumentation so we can handle events.

This library was originally created to be able to handle database errors in a consistent manner throughout an app, without having to have code scattered all over the place.

Basic usage:

You need to create an implementation of the IInstrumentationHandler to be able to do anything useful. The example belows shows a simple implementation which prints instrumentation data to console.

Step 1 : Create an implementation of IInstrumentationHandler

public class BasicConsoleLoggingInstrumenter : IInstrumentationHandler
    {
        public void ExecuteStart(IDbCommand instrumentedDbCommand, SqlExecuteType executeType)
        {
            Console.WriteLine($"ExecuteStart:{instrumentedDbCommand.CommandText}");
        }

        public void ExecuteFinish(IDbCommand instrumentedDbCommand, SqlExecuteType executeType, DbDataReader reader)
        {
            Console.WriteLine($"ExecuteFinish:{instrumentedDbCommand.CommandText}");
        }

        public void OnError(IDbCommand instrumentedDbCommand, SqlExecuteType executeType, Exception exception)
        {
            Console.WriteLine($"OnError:{instrumentedDbCommand.CommandText}\n{exception.Message}");
        }
    }

Step 2 : Ensure you created and use the InstrumentConnection

The code below assumes an in-memory Sqlite connection

var instrumenter = new BasicConsoleLoggingIstrumenter();
var connection = new SqlConnection("DataSource=:memory:");

var instrumentedConnection = new InstrumentedDbConnection(connection, instrumenter);

About

Instrumented-AdoNet is a basic library to be able to instrument adonet calls.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages