private void LogMethodAfterInvocation(IInvocation invocation, LogActive activeState, object returnValue) { if (invocation == null) { throw new ArgumentNullException(nameof(invocation)); } LogActive methodActiveState = activeState; MethodInfo methodInfo = invocation.MethodInvocationTarget; Debug.Assert(methodInfo != null); (object filteredReturnValue, LogActive anyParametersToLog) = FilterReturnValue(methodInfo, methodActiveState, returnValue); if (anyParametersToLog == LogActive.On) { using (LogContext.PushProperty(LogTypeName, LogType.Diagnostic)) using (LogContext.Push(new InvocationEnricher(invocation))) using (LogContext.PushProperty(ReturnValueName, filteredReturnValue, destructureObjects: true)) { string logMessage = $"{GetSourceMessage(invocation)} invocation ended"; m_Logger.Information(logMessage); } } }
protected override void CompletedInvocation(IInvocation invocation, DiagnosticLogState activeState, object returnValue) { if (invocation == null) { throw new ArgumentNullException(nameof(invocation)); } LogActive classActiveState = activeState; LogMethodAfterInvocation(invocation, classActiveState, returnValue); }
private static Tuple <object, LogActive> FilterReturnValue( MethodInfo methodInfo, LogActive activeState, object returnValue) { if (methodInfo == null) { throw new ArgumentNullException(nameof(methodInfo)); } LogActive returnParameterActiveState = activeState; ParameterInfo parameterInfo = methodInfo.ReturnParameter; Debug.Assert(parameterInfo != null); // Check for DiagnosticLogging ReturnValue scope. if (parameterInfo .GetCustomAttribute(typeof(DiagnosticLoggingAttribute), false) is DiagnosticLoggingAttribute returnValueDiagnosticAttribute) { returnParameterActiveState = returnValueDiagnosticAttribute.LogActive; } object returnParameterValue = FilteredParameterSubstitute; // Send a message back whether anything should be logged. LogActive anyParametersToLog = activeState; if (returnParameterActiveState == LogActive.On) { anyParametersToLog = LogActive.On; if (parameterInfo.ParameterType == typeof(void) || parameterInfo.ParameterType == typeof(Task)) { returnParameterValue = VoidSubstitute; } else { returnParameterValue = returnValue; } } return(Tuple.Create(returnParameterValue, anyParametersToLog)); }
private LogActive LogMethodBeforeInvocation( IInvocation invocation, LogActive activeState) { if (invocation == null) { throw new ArgumentNullException(nameof(invocation)); } LogActive methodActiveState = activeState; MethodInfo methodInfo = invocation.MethodInvocationTarget; Debug.Assert(methodInfo != null); // Check for DiagnosticLogging Method scope. if (methodInfo .GetCustomAttribute(typeof(DiagnosticLoggingAttribute), false) is DiagnosticLoggingAttribute methodDiagnosticAttribute) { methodActiveState = methodDiagnosticAttribute.LogActive; } Tuple <IList <object>, LogActive> filteredParametersTuple = FilterParameters(invocation, methodInfo, methodActiveState, m_FilterTheseParameters); IList <object> filteredParameters = filteredParametersTuple.Item1; LogActive anyParametersToLog = filteredParametersTuple.Item2; if (anyParametersToLog == LogActive.On) { using (LogContext.PushProperty(LogTypesName, LogTypes.Diagnostic)) using (LogContext.Push(new InvocationEnricher(invocation))) using (LogContext.PushProperty(ArgumentsName, filteredParameters, destructureObjects: true)) { string logMessage = $"{GetSourceMessage(invocation)} invocation started"; m_Logger.Information(logMessage); } } return(methodActiveState); }
protected override DiagnosticLogState StartingInvocation(IInvocation invocation) { Debug.Assert(invocation != null); Debug.Assert(invocation.TargetType != null); LogActive classActiveState = LogActive.Off; // Check for DiagnosticLogging Class scope. var classDiagnosticAttribute = invocation .TargetType .GetCustomAttributes(typeof(DiagnosticLoggingAttribute), false) .FirstOrDefault() as DiagnosticLoggingAttribute; if (classDiagnosticAttribute != null) { classActiveState = classDiagnosticAttribute.LogActive; } LogActive methodActiveState = LogMethodBeforeInvocation(invocation, classActiveState); return(new DiagnosticLogState(methodActiveState)); }
void timer_Elapsed(object sender, ElapsedEventArgs e) { new Thread(() => { SpeedportHybrid.initLtePopup(); Application.Current.Dispatcher.BeginInvoke(new Action(() => { LTECollection.Add(new LTEData() { Date = DateTime.Now, Data = rsrq.ToInt() }); LTECollection2.Add(new LTEData() { Date = DateTime.Now, Data = rsrp.ToInt() }); if (LogActive.Equals(true)) { // log log(string.Concat("Pysical Cell ID: ", phycellid, ", Cell ID: ", cellid, ", RSRP: ", rsrp, ", RSRQ: ", rsrq)); } })); }).Start(); }
protected override DiagnosticLogState StartingInvocation(IInvocation invocation) { if (invocation == null) { throw new ArgumentNullException(nameof(invocation)); } Debug.Assert(invocation.TargetType != null); LogActive classActiveState = LogActive.Off; // Check for DiagnosticLogging Class scope. if (invocation .TargetType.GetTypeInfo() .GetCustomAttributes(typeof(DiagnosticLoggingAttribute), false) .FirstOrDefault() is DiagnosticLoggingAttribute classDiagnosticAttribute) { classActiveState = classDiagnosticAttribute.LogActive; } LogActive methodActiveState = LogMethodBeforeInvocation(invocation, classActiveState); return(new DiagnosticLogState(methodActiveState)); }
public DiagnosticLogState(LogActive activeState) { ActiveState = activeState; }
private static (IList <object>, LogActive) FilterParameters( IInvocation invocation, MethodInfo methodInfo, LogActive activeState, HashSet <string> filterTheseParameters) { if (invocation == null) { throw new ArgumentNullException(nameof(invocation)); } if (methodInfo == null) { throw new ArgumentNullException(nameof(methodInfo)); } if (filterTheseParameters == null) { throw new ArgumentNullException(nameof(filterTheseParameters)); } ParameterInfo[] parameterInfos = methodInfo.GetParameters(); Debug.Assert(parameterInfos != null); object[] parameters = invocation.Arguments; Debug.Assert(parameters != null); Debug.Assert(parameterInfos.Length == parameters.Length); var filteredParameters = new List <object>(); // Send a message back whether anything should be logged. LogActive anyParametersToLog = activeState; for (int parameterIndex = 0; parameterIndex < parameters.Length; parameterIndex++) { LogActive parameterActiveState = activeState; ParameterInfo parameterInfo = parameterInfos[parameterIndex]; // Check if the parameter name matches any of the pre-determined filters. if (filterTheseParameters.Contains(parameterInfo.Name)) { parameterActiveState = LogActive.Off; } // Check for DiagnosticLogging Parameter scope. var parameterDiagnosticAttribute = parameterInfo .GetCustomAttribute(typeof(DiagnosticLoggingAttribute), false) as DiagnosticLoggingAttribute; if (parameterDiagnosticAttribute != null) { parameterActiveState = parameterDiagnosticAttribute.LogActive; } object parameterValue = FilteredParameterSubstitute; if (parameterActiveState == LogActive.On) { anyParametersToLog = LogActive.On; parameterValue = parameters[parameterIndex]; } filteredParameters.Add(parameterValue); } return(filteredParameters, anyParametersToLog); }
public DiagnosticLoggingAttribute(LogActive logActive) { LogActive = logActive; }