Anixe.IO.Appmetrics.Extensions.SqlDB 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Anixe.IO.Appmetrics.Extensions.SqlDB --version 1.1.0                
NuGet\Install-Package Anixe.IO.Appmetrics.Extensions.SqlDB -Version 1.1.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Anixe.IO.Appmetrics.Extensions.SqlDB" Version="1.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Anixe.IO.Appmetrics.Extensions.SqlDB --version 1.1.0                
#r "nuget: Anixe.IO.Appmetrics.Extensions.SqlDB, 1.1.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Anixe.IO.Appmetrics.Extensions.SqlDB as a Cake Addin
#addin nuget:?package=Anixe.IO.Appmetrics.Extensions.SqlDB&version=1.1.0

// Install Anixe.IO.Appmetrics.Extensions.SqlDB as a Cake Tool
#tool nuget:?package=Anixe.IO.Appmetrics.Extensions.SqlDB&version=1.1.0                

Anixe.IO.Appmetrics.Extensions.SqlDB

Anixe.IO.Appmetrics.Extensions.SqlDB allows you to quickly integrate any System.Data.Common based driver database with graylog

How to integrate

Please follow the instructions in appmetrics , then add in csproj as a package reference

<PackageReference Include="Anixe.IO.Appmetrics.Extensions.SqlDB" Version="1.0.6" />

register appmetrics and extension in DI container in Startup.cs. The goal is to provide DbProviderFactory pattern as UseSqlDBWithAnixeAppmetrics argument. The factory will be store as static variable inside.

    public void ConfigureServices(IServiceCollection services)
    {
      services.UseAnixeAppmetrics(this.config, opts => // use this to provide metrics for inbound http requests (middlewaere) and unhandled exceptions (filter)
      {
        // override any option here
        opts.Middleware.HostName = "my machine"; /*providing machine name is on your own*/
        opts.Middleware.ServerIp = "127.0.0.1"; /*providing machine name is on your own*/
      })

      services.UseSqlDBWithAnixeAppmetrics(NpgsqlFactory.Instance); // register dependencies with custom factory from Npgsql driver.
    }

then use it by executing commands injected from DI. It is mandatory to use provided objects from DI container. If you don't use it then no db app metrics will be recorded. You are able to use Func<DbConnection, DbCommand> or Func<DbTransaction, DbCommand> dependencies. Each call on one of these functions will create new DbCommand instance with provided DbConnection or DbTransaction so you can control the connection scope via Dispose pattern.

Here is my example of DatabaseService which executes various db operations using injected dependencies from Appmetrics.

  public class DatabaseService : IDatabaseService
  {
    private readonly Func<DbConnection, DbCommand> dbCommandFunc;
    private readonly Func<DbTransaction, DbCommand> dbCommandWithinTransactionFunc;

    public DatabaseService(Func<DbConnection, DbCommand> dbCommandFunc, Func<DbTransaction, DbCommand> dbCommandWithinTransactionFunc)
    {
      this.dbCommandFunc = dbCommandFunc;
      this.dbCommandWithinTransactionFunc = dbCommandWithinTransactionFunc;
    }


    // create new connection ,run command and close the connection
    public async Task<T> ExecuteScalar<T>(string sqlQuery, DbParameter[] cmdParms, CancellationToken token = default)
    {
      using (var connection = CreateConnection())
      using (var cmd = dbCommandFunc(connection))
      {
        PrepareCommand(cmd, CommandType.Text, sqlQuery, cmdParms); // write SELECT here
        return (T)await cmd.ExecuteScalarAsync(token);
      }
    }


    // reused connection from argument with ExecuteReaderAsync
    public async Task<IDataReader> ExecuteQueryAsync(string sqlQuery, DbParameter[] cmdParms, DbConnection conn)
    {      
      EnsureOpen(conn);
      using (var cmd = dbCommandFunc(conn))
      {
        PrepareCommand(cmd, CommandType.Text, sqlQuery, cmdParms); // write SELECT here
        return await cmd.ExecuteReaderAsync(CommandBehavior.SequentialAccess);
      }
    }


    // command with transaction
    public async Task<IDataReader> ExecuteQueryAsync(string sqlQuery, DbParameter[] cmdParms, DbTransaction tr)
    {
      EnsureOpen(tr.Connection);
      using (var cmd = dbCommandWithinTransactionFunc(tr))
      {
        PrepareCommand(cmd, CommandType.Text, sqlQuery, cmdParms); // write SELECT here
        return await cmd.ExecuteReaderAsync(CommandBehavior.SequentialAccess);
      }
    }

  }

Example message

{
  "version": "1.1",
  "host": "wm-anx-macbook.local",
  "level": 6,
  "timestamp": 1563458762.351234,
  "_application_name": "wheels_admin_api",
  "_transaction_name": "api/v1/export_ion/sales_discount/test",
  "_src": "wheels_admin_api",
  "_rid": "5486539052653189219",
  "_mid": "5189252136311428311",
  "_st": "OK",
  "_p": "CAR",
  "_env": "development",
  "_platform": "wheels",
  "_host_ip": "192.168.195.81",
  "_rq_mth": "GET",
  "_rq_url": "http://localhost:5001/api/v1/export_ion/sales_discount/test?ionUrl=%2fapi%2fv1%2fsales_discounts%2fion%3ftenant%3dtest%26key%3dapi_secret_key",
  "_rs_code": 200,
  "_client_ip": "127.0.0.1",
  "_tt": 34.039,
  "_ttp": 8.383,
  "_ttc": 25.656,
  "_db_sel_customers_tt": 25.656,
  "_db_sel_customers_count": 1,
  "short_message": "-"
}

custom fields from db appmetrics are those with _db prefix, so: _db_sel_customers_tt and _db_sel_customers_count. The format beneath is _prefix_operation_tablename_property, where:

  • _prefix - always _db for database communication related data
  • _operation - can be sel for SELECT, ins for INSERT, upd for UPDATE, del for DELETE, fetch for FETCH cursor.
  • _tablename - SQL table name of the operation
  • _tt - time taken in miliseconds, this is sum of all the same operations on the same table
  • _count - number of the same operations on the same table

Example for multiple operations (4 selects from customers and one update of customers table)

{
  ...
  "_db_sel_customers_tt" : 234.23,
  "_db_sel_customers_count": 4,
  "_db_upd_customers_tt" : 24.3,
  "_db_upd_customers_count": 1,
  ...
}
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.1 is compatible.  netcoreapp2.2 is compatible.  netcoreapp3.0 is compatible.  netcoreapp3.1 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.0.0 195 1/8/2024
2.0.0 317 12/20/2022
1.1.0 510 8/31/2021
1.0.9 347 7/8/2021
1.0.8 2,141 3/15/2021
1.0.7 391 1/27/2021
1.0.6 3,202 8/29/2019
1.0.5 348 8/27/2019
1.0.4 360 8/23/2019
1.0.3 335 8/23/2019
1.0.2 329 7/24/2019
1.0.1 444 7/23/2019
1.0.0 576 7/22/2019