GithubHelp home page GithubHelp logo

Tracing\Debugging about npoco HOT 6 CLOSED

mika76 avatar mika76 commented on August 20, 2024
Tracing\Debugging

from npoco.

Comments (6)

schotime avatar schotime commented on August 20, 2024

Here is the easiest way which I use.
Create a class which derives from Database, then override the OnExecutingCommand method.

public class MyDb : Database 
{
    public override void OnExecutingCommand(IDbCommand cmd)
    {
        File.WriteAllText("blah.txt", FormatCommand(cmd));
    }
}

from npoco.

katwork avatar katwork commented on August 20, 2024

I found a function on PSC that combines the SQL and parameters and modified it to work in my database instance. I haven't made any changes to it yet other than changing the types to work with what NPoco uses. Pardon the VB.

Partial Public Class MyDB
  Inherits NPoco.Database

  Protected _lastCombinedSQL As String = String.Empty

  Public ReadOnly Property LastCombinedSQL As String
    Get
      Return _lastCombinedSQL
    End Get
  End Property

  Public Overrides Sub OnExecutingCommand(cmd As IDbCommand)
    Try
      _lastCombinedSQL = ConvertCommandParamatersToLiteralValues(cmd)
    Catch ex As Exception
      _lastCombinedSQL = "[error combining sql command and parameters]"
    End Try
    MyBase.OnExecutingCommand(cmd)
  End Sub

  ''' <summary>
  ''' A Method to convert a IDbCommand with paramaters to string value.
  ''' </summary>
  ''' <param name="cmd"></param>
  ''' <returns></returns>
  ''' <remarks>
  ''' Based on http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=7760&amp;lngWId=10 
  ''' </remarks>
  Public Shared Function ConvertCommandParamatersToLiteralValues(cmd As IDbCommand) As String
    If (cmd Is Nothing) Then Return String.Empty
    Dim query As String = cmd.CommandText
    For Each prm As IDbDataParameter In cmd.Parameters
      Select Case prm.DbType
        Case SqlDbType.Bit
          Dim boolToInt As Integer = If(CBool(prm.Value), 1, 0)
          query = query.Replace(prm.ParameterName, String.Format("{0}", If(CBool(prm.Value), 1, 0)))
          Exit Select
        Case SqlDbType.Int
          query = query.Replace(prm.ParameterName, String.Format("{0}", prm.Value))
          Exit Select
        Case SqlDbType.VarChar
          query = query.Replace(prm.ParameterName, String.Format("'{0}'", prm.Value))
          Exit Select
        Case Else
          query = query.Replace(prm.ParameterName, String.Format("'{0}'", prm.Value))
          Exit Select
      End Select
    Next
    Return query
  End Function

End Class

from npoco.

schotime avatar schotime commented on August 20, 2024

You could also use the latest Glimpse.Ado package, which will be able to monitor all SQLs that are run. If you are running on Asp.net MVC you will need to install the Glimpse.Mvc4 or relevant version of MVC as well.

It will look like this.

glimpse

from npoco.

techniq avatar techniq commented on August 20, 2024

This might be a good item to add to the Wiki, along with MiniProfiler support.

from npoco.

schotime avatar schotime commented on August 20, 2024

Thanks @techniq. Have put one together quickly.
https://github.com/schotime/NPoco/wiki/Debugging-and-Profiling

from npoco.

techniq avatar techniq commented on August 20, 2024

Perfect. Thanks.

from npoco.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.