in src/log4net/Core/LocationInfo.cs [74:154]
public LocationInfo(Type? callerStackBoundaryDeclaringType)
{
// Initialize all fields
ClassName = NotAvailable;
FileName = NotAvailable;
LineNumber = NotAvailable;
MethodName = NotAvailable;
FullInfo = NotAvailable;
if (callerStackBoundaryDeclaringType is not null)
{
try
{
StackTrace st = new(true);
int frameIndex = 0;
// skip frames not from fqnOfCallingClass
while (frameIndex < st.FrameCount)
{
if (st.GetFrame(frameIndex) is StackFrame frame
&& frame.GetMethod()?.DeclaringType == callerStackBoundaryDeclaringType)
{
break;
}
frameIndex++;
}
// skip frames from fqnOfCallingClass
while (frameIndex < st.FrameCount)
{
if (st.GetFrame(frameIndex) is StackFrame frame
&& frame.GetMethod()?.DeclaringType != callerStackBoundaryDeclaringType)
{
break;
}
frameIndex++;
}
if (frameIndex < st.FrameCount)
{
// take into account the frames we skip above
int adjustedFrameCount = st.FrameCount - frameIndex;
var stackFramesList = new List<StackFrameItem>(adjustedFrameCount);
StackFrames = new StackFrameItem[adjustedFrameCount];
for (int i = frameIndex; i < st.FrameCount; i++)
{
if (st.GetFrame(i) is StackFrame frame)
{
stackFramesList.Add(new StackFrameItem(frame));
}
}
stackFramesList.CopyTo(StackFrames, 0);
// now frameIndex is the first 'user' caller frame
if (st.GetFrame(frameIndex) is StackFrame locationFrame)
{
if (locationFrame.GetMethod() is System.Reflection.MethodBase method)
{
MethodName = method.Name;
if (method.DeclaringType is not null)
{
ClassName = method.DeclaringType.FullName;
}
}
FileName = locationFrame.GetFileName();
LineNumber = locationFrame.GetFileLineNumber().ToString(System.Globalization.NumberFormatInfo.InvariantInfo);
// Combine all location info
FullInfo = $"{ClassName}.{MethodName}({FileName}:{LineNumber})";
}
}
}
catch (System.Security.SecurityException)
{
// This security exception will occur if the caller does not have
// some undefined set of SecurityPermission flags.
LogLog.Debug(_declaringType, "Security exception while trying to get caller stack frame. Error Ignored. Location Information Not Available.");
}
}
}