protected void LogPath(IMethodInvocation invocation, StackTrace stackTrace, InvokeNameExtractingResult behaviorResult) { try { int bhvNextIndex = behaviorResult.getNextStartIndex(); InvokeNameExtractingResult clientResult = extractClientInvokeName(stackTrace, bhvNextIndex); int clientFirstIndex = clientResult.getFoundFirstIndex(); InvokeNameExtractingResult byPassResult = extractByPassInvokeName(stackTrace, bhvNextIndex, clientFirstIndex - bhvNextIndex); String clientInvokeName = clientResult.getInvokeName(); String byPassInvokeName = byPassResult.getInvokeName(); String behaviorInvokeName = behaviorResult.getInvokeName(); // Save client invoke name for error message. if (clientResult.getSimpleClassName() != null) { PutObjectToMapContext("df:ClientInvokeName", clientInvokeName); } // Save by-pass invoke name for error message. if (byPassResult.getSimpleClassName() != null) { PutObjectToMapContext("df:ByPassInvokeName", byPassInvokeName); } if (clientInvokeName.Trim().Length == 0 && byPassInvokeName.Trim().Length == 0) { return; } Log(clientInvokeName + byPassInvokeName + behaviorInvokeName + "..."); } catch (Exception ignored) { Log("Failed to extract client or by-pass name: " + ignored.Message); } }
// =============================================================================== // Log Invocation // ============== protected void LogInvocation(IMethodInvocation invocation) { StackTrace stackTrace = new StackTrace(); MethodBase method = invocation.Method; InvokeNameExtractingResult behaviorResult = null; try { behaviorResult = extractBehaviorInvokeName(stackTrace); } catch (Exception ignored) { Log("Failed to extract behavior name: msg=" + ignored.Message); } String invokeName = null; if (behaviorResult != null && behaviorResult.getSimpleClassName() != null) { String invokeClassName = behaviorResult.getSimpleClassName(); String invokeMethodName = behaviorResult.getMethodName(); invokeName = buildInvocationExpressionWithoutKakko(invocation, invokeClassName, invokeMethodName); } else { invokeName = BuildSimpleExpressionFromDaoName(method.DeclaringType.Name) + "." + method.Name; } // Save behavior invoke name for error message. PutObjectToMapContext("df:BehaviorInvokeName", invokeName + "()"); int length = invokeName.Length; StringBuilder sb = new StringBuilder(); for (int i = 0; i < length; i++) { sb.Append("="); } Log("/=====================================================" + sb.ToString() + "=="); Log(" " + invokeName + "()"); Log(" " + sb.ToString() + "=/"); LogPath(invocation, stackTrace, behaviorResult); // Specified OutsideSql if (IsSpecifiedOutsideSql(invocation)) { Object[] args = invocation.Arguments; OutsideSqlContext outsideSqlContext = GetOutsideSqlContext(); if (outsideSqlContext != null) { Log("path: " + outsideSqlContext.OutsideSqlPath); } else { Log("path: " + GetOutsideSqlPath(args)); } Log("option: " + GetOutsideSqlOption(args)); } }
protected InvokeNameExtractingResult extractInvokeName(InvokeNameExtractingCallback callback, StackTrace stackTrace) { String targetSimpleClassName = null; String targetMethodName = null; int lineNumber = 0; int foundIndex = -1; // The minus one means 'Not Found'. int foundFirstIndex = -1; // The minus one means 'Not Found'. bool onTarget = false; for (int i = 0; i < stackTrace.FrameCount; i++) { StackFrame stackFrame = stackTrace.GetFrame(i); // The inner class is not appeared. MethodBase method = stackFrame.GetMethod(); if (method == null) { break; // End } if (method.DeclaringType == null) { break; // If you use WCF, it may return null. } String className = method.DeclaringType.Name; // This is simple class name at CSharp. String methodName = method.Name; if (i > callback.getStartIndex() + callback.getLoopSize()) { break; } if (className.StartsWith("System.")) // This is system package of CSharp. { if (onTarget) // But the class name is simple so this 'if' statement is waste... { break; } continue; } if (callback.isTargetElement(className, methodName)) { if (methodName.Equals("Invoke")) { continue; } targetSimpleClassName = className.Substring(className.LastIndexOf(".") + 1); targetMethodName = methodName; if (callback.isUseAdditionalInfo()) { try { lineNumber = stackFrame.GetFileLineNumber(); } catch (Exception) { // Ignored } } foundIndex = i; if (foundFirstIndex == -1) { foundFirstIndex = i; } onTarget = true; continue; } if (onTarget) { break; } } InvokeNameExtractingResult result = new InvokeNameExtractingResult(); if (targetSimpleClassName == null) { result.setInvokeName("");// Not Found! It sets empty string as default. return(result); } String filteredClassName = callback.filterSimpleClassName(targetSimpleClassName); result.setSimpleClassName(callback.filterSimpleClassName(targetSimpleClassName)); result.setMethodName(targetMethodName); if (lineNumber > 0) { result.setInvokeName(filteredClassName + "." + targetMethodName + "():" + lineNumber + " --> "); } else { result.setInvokeName(filteredClassName + "." + targetMethodName + "() --> "); } result.setFoundIndex(foundIndex); result.setFoundFirstIndex(foundFirstIndex); return(result); }