Dries Schuddinck
Dries Schuddinck

Reputation: 11

Log faulty NHibernate queries with parameter values

I found out how to log NHibernate queries with log4net, I also found how to log queries with the IInterceptor within the OnPrepareStatement method.

But what I really need is a way how to log NHibernate queries when they fail (fe ORA faults). The IInterceptor method has a way to do it, but it does not provide me a way to log the parameter values. Also the solution I came up with is not thread safe.

Anybody has better ideas?

Thanks in advance

Upvotes: 1

Views: 125

Answers (1)

user4573148
user4573148

Reputation:

Here is some custom code that is part of the IMethodInvocation parameter. This code is basically a new file that Implements IInterceptionBehavior. The GetParam is crude but it gets the value.

        ''' <summary>
        ''' this can be changed to handle a multitude of parameters/enhancements
        ''' </summary>
        ''' <param name="input"></param>
        ''' <returns></returns>
        Private Function GetParam(input As IMethodInvocation) As Object

            'access MethodInvocation
            Dim count = input.Inputs.Count
            Dim param As New Object
            If count <> 0 Then
                param = input.Inputs.Item(0)
                Return param
            End If

            'nothing
            Return Nothing
        End Function`

Here is how it is used.

Public Function Invoke(input As IMethodInvocation, getNext As GetNextInterceptionBehaviorDelegate) As IMethodReturn Implements IInterceptionBehavior.Invoke
             ' Before invoking the method on the original target. 
            Dim icp As ClaimsPrincipal = TryCast(Thread.CurrentPrincipal, ClaimsPrincipal)

            ' Access IClaimsIdentity which contains claims
            Dim claimsIdentity As ClaimsIdentity = DirectCast(icp.Identity, ClaimsIdentity)
            Dim param = GetParam(input)

                If param IsNot Nothing Then
                    WriteLog([String].Format("{0} is invoking method {1} at {2} with a parameter of {3}", claimsIdentity.Name, input.MethodBase, DateTime.Now.ToLongTimeString(), param))
                Else
                    WriteLog([String].Format("{0} is invoking method {1} at {2} without a parameter", claimsIdentity.Name, input.MethodBase, DateTime.Now.ToLongTimeString()))
                End If


            Return result
        End Function

Upvotes: 1

Related Questions