【问题标题】:Log faulty NHibernate queries with parameter values使用参数值记录错误的 NHibernate 查询
【发布时间】:2023-03-12 04:02:01
【问题描述】:

我发现了如何使用 log4net 记录 NHibernate 查询,我还发现了如何使用 OnPrepareStatement 方法中的 IInterceptor 记录查询。

但我真正需要的是如何在 NHibernate 查询失败时记录它们(fe ORA 错误)。 IInterceptor 方法有办法做到这一点,但它没有为我提供记录参数值的方法。我想出的解决方案也不是线程安全的。

有人有更好的想法吗?

提前致谢

【问题讨论】:

    标签: .net nhibernate log4net


    【解决方案1】:

    这里是一些自定义代码,它是 IMethodInvocation 参数的一部分。这段代码基本上是一个实现 IInterceptionBehavior 的新文件。 GetParam 是粗略的,但它得到了价值。

            ''' <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`
    

    这是它的使用方法。

    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
    

    【讨论】:

      猜你喜欢
      • 2019-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多