@@ -109,30 +109,43 @@ internal static void Write([NotNull] Type loggerType, TargetWithFilterChain targ
109109 /// <param name="stackTrace">The stack trace of the logging method invocation</param>
110110 /// <param name="loggerType">Type of the logger or logger wrapper. This is still Logger if it's a subclass of Logger.</param>
111111 /// <returns>Index of the first user stack frame or 0 if all stack frames are non-user</returns>
112- private static int FindCallingMethodOnStackTrace ( [ NotNull ] StackTrace stackTrace , [ NotNull ] Type loggerType )
112+ internal static int FindCallingMethodOnStackTrace ( [ NotNull ] StackTrace stackTrace , [ NotNull ] Type loggerType )
113113 {
114114 var stackFrames = stackTrace . GetFrames ( ) ;
115115 if ( stackFrames == null )
116116 return 0 ;
117117
118- var intermediate = stackFrames . Select ( ( f , i ) => new StackFrameWithIndex ( i , f ) ) ;
118+ //create StackFrameWithIndex so the index is know after filtering
119+ var allStackFrames = stackFrames . Select ( ( f , i ) => new StackFrameWithIndex ( i , f ) ) . ToList ( ) ;
120+ //filter on assemblies
121+ var filteredStackframes = allStackFrames . Where ( p => ! SkipAssembly ( p . StackFrame ) ) . ToList ( ) ;
119122 //find until logger type
120- intermediate = intermediate . SkipWhile ( p => ! IsLoggerType ( p . StackFrame , loggerType ) ) ;
123+ var intermediate = filteredStackframes . SkipWhile ( p => ! IsLoggerType ( p . StackFrame , loggerType ) ) ;
121124 //skip the logger type
122- intermediate = intermediate . SkipWhile ( p => IsLoggerType ( p . StackFrame , loggerType ) ) ;
125+ var stackframesAfterLogger = intermediate . SkipWhile ( p => IsLoggerType ( p . StackFrame , loggerType ) ) . ToList ( ) ;
123126
124- intermediate = FilterBySkipAssembly ( intermediate ) ;
125- return FindIndexOfCallingMethod ( intermediate ) ;
127+ //get first call after logger (or skip if is moveNext)
128+ var candidateStackFrames = stackframesAfterLogger ;
129+ if ( ! candidateStackFrames . Any ( ) )
130+ {
131+ //If some calls got inlined, we can't find LoggerType on the stack. Fallback to the filteredStackframes
132+ candidateStackFrames = filteredStackframes ;
133+ }
134+
135+ return FindIndexOfCallingMethod ( allStackFrames , candidateStackFrames ) ;
126136 }
127137
128138 /// <summary>
129139 /// Get the index which correspondens to the calling method.
140+ ///
141+ /// This is most of the time the first index after <paramref name="candidateStackFrames"/>.
130142 /// </summary>
131- /// <param name="stackFrames"></param>
132- /// <returns></returns>
133- private static int FindIndexOfCallingMethod ( IEnumerable < StackFrameWithIndex > stackFrames )
143+ /// <param name="allStackFrames">all the frames of the stacktrace</param>
144+ /// <param name="candidateStackFrames">frames which all hiddenAssemblies are removed</param>
145+ /// <returns>index on stacktrace</returns>
146+ private static int FindIndexOfCallingMethod ( List < StackFrameWithIndex > allStackFrames , List < StackFrameWithIndex > candidateStackFrames )
134147 {
135- var stackFrameWithIndex = stackFrames . FirstOrDefault ( ) ;
148+ var stackFrameWithIndex = candidateStackFrames . FirstOrDefault ( ) ;
136149 var last = stackFrameWithIndex ;
137150
138151 if ( last != null )
@@ -142,17 +155,16 @@ private static int FindIndexOfCallingMethod(IEnumerable<StackFrameWithIndex> sta
142155 //movenext and then AsyncTaskMethodBuilder (method start)? this is a generated MoveNext by async.
143156 if ( last . StackFrame . GetMethod ( ) . Name == "MoveNext" )
144157 {
145- var next = stackFrames . Skip ( 1 ) . FirstOrDefault ( ) ;
146- if ( next != null )
158+
159+ if ( allStackFrames . Count > last . StackFrameIndex )
147160 {
161+ var next = allStackFrames [ last . StackFrameIndex + 1 ] ;
148162 var declaringType = next . StackFrame . GetMethod ( ) . DeclaringType ;
149163 if ( declaringType == typeof ( System . Runtime . CompilerServices . AsyncTaskMethodBuilder ) )
150164 {
151-
152165 //async, search futher
153-
154- stackFrames = FilterBySkipAssembly ( stackFrames . Skip ( 1 ) ) ;
155- return FindIndexOfCallingMethod ( stackFrames ) ;
166+ candidateStackFrames = candidateStackFrames . Skip ( 1 ) . ToList ( ) ;
167+ return FindIndexOfCallingMethod ( allStackFrames , candidateStackFrames ) ;
156168 }
157169 }
158170 }
@@ -163,18 +175,6 @@ private static int FindIndexOfCallingMethod(IEnumerable<StackFrameWithIndex> sta
163175 return 0 ;
164176 }
165177
166- /// <summary>
167- /// Filter strackframes by assembly filter
168- /// </summary>
169- /// <param name="stackFrames"></param>
170- /// <returns></returns>
171- private static IEnumerable < StackFrameWithIndex > FilterBySkipAssembly ( IEnumerable < StackFrameWithIndex > stackFrames )
172- {
173- //skip while in "skip" assemlbl
174- stackFrames = stackFrames . SkipWhile ( p => SkipAssembly ( p . StackFrame ) ) ;
175- return stackFrames ;
176- }
177-
178178 /// <summary>
179179 /// Assembly to skip?
180180 /// </summary>
@@ -185,7 +185,8 @@ private static bool SkipAssembly(StackFrame frame)
185185 var method = frame . GetMethod ( ) ;
186186 var assembly = method . DeclaringType != null ? method . DeclaringType . Assembly : method . Module . Assembly ;
187187 // skip stack frame if the method declaring type assembly is from hidden assemblies list
188- return SkipAssembly ( assembly ) ;
188+ var skipAssembly = SkipAssembly ( assembly ) ;
189+ return skipAssembly ;
189190 }
190191
191192 /// <summary>
0 commit comments