From ee37d0f03796cca9dd895b2c15194811559f420d Mon Sep 17 00:00:00 2001 From: Johann Duscher Date: Fri, 5 Jul 2024 23:53:50 +0200 Subject: [PATCH] fixup! Add support for logging with context properties. --- .../Log4NetLoggingAdapter.cs | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/src/Akka.Logger.log4net/Log4NetLoggingAdapter.cs b/src/Akka.Logger.log4net/Log4NetLoggingAdapter.cs index fef5106..b1338b7 100644 --- a/src/Akka.Logger.log4net/Log4NetLoggingAdapter.cs +++ b/src/Akka.Logger.log4net/Log4NetLoggingAdapter.cs @@ -147,21 +147,33 @@ internal void AddCallerInfoFromStackTraceIfMissing(PropertiesDictionary properti #region Helper function(s) - // Get the stack frame of the caller of the method in the provided type. + // Get the stack frame of the caller of the method in the provided 'logClass' from + // the stack trace. + // (Note that generating a stack trace is costly and should be avoided if possible.) static StackFrame? GetCallerStackFrame(Type logClass) - => new StackTrace(fNeedFileInfo: true) - .GetFrames() - .FirstOrDefault(frame => GetDeclaringTypes(frame.GetMethod()).Contains(logClass)); - - // Get the declaring type of the method as well as all declaring types up to the root type. - static IEnumerable GetDeclaringTypes(MethodBase method) { - var type = method.DeclaringType; - while (type is not null) + // Get the sequence of stack frames from the 'StackTrace' object. + if (new StackTrace(fNeedFileInfo: true).GetFrames() is not { } frames) + return null; + + // Try to find the frame where the declaring type of the frame's method is the + // provided 'logClass'. + foreach (var frame in frames) { - yield return type; - type = type.DeclaringType; + // Traverse the declaring types of the method up to the root type and try to + // find the 'logClass'. + var type = frame.GetMethod()?.DeclaringType; + while (type is not null) + { + // If 'true' we have found the caller. + if (type == logClass) + return frame; + + type = type.DeclaringType; + } } + + return null; } #endregion