Skip to content

Commit

Permalink
CallSiteInformation - Prepare for fast classname lookup from filename…
Browse files Browse the repository at this point in the history
… and linenumber
  • Loading branch information
snakefoot committed Dec 3, 2017
1 parent 46d55f4 commit b143e9c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/NLog/Fluent/LogBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public void Write(
if (callerLineNumber != 0)
Property("CallerLineNumber", callerLineNumber);

_logEvent.SetCallerInfo(callerMemberName, callerFilePath, callerLineNumber);
_logEvent.SetCallerInfo(null, callerMemberName, callerFilePath, callerLineNumber);

_logger.Log(_logEvent);
}
Expand Down
21 changes: 20 additions & 1 deletion src/NLog/Internal/CallSiteInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ public void SetStackTrace(StackTrace stackTrace, int userStackFrame, int? userSt
/// <summary>
/// Sets the details retrieved from the Caller Information Attributes
/// </summary>
/// <param name="callerClassName"></param>
/// <param name="callerMemberName"></param>
/// <param name="callerFilePath"></param>
/// <param name="callerLineNumber"></param>
public void SetCallerInfo(string callerMemberName, string callerFilePath, int callerLineNumber)
public void SetCallerInfo(string callerClassName, string callerMemberName, string callerFilePath, int callerLineNumber)
{
CallerClassName = callerClassName;
CallerMemberName = callerMemberName;
CallerFilePath = callerFilePath;
CallerLineNumber = callerLineNumber;
Expand Down Expand Up @@ -94,6 +96,22 @@ public MethodBase GetCallerStackFrameMethod(int skipFrames)

public string GetCallerClassName(MethodBase method, bool includeNameSpace, bool cleanAsyncMoveNext, bool cleanAnonymousDelegates)
{
if (!string.IsNullOrEmpty(CallerClassName))
{
if (includeNameSpace)
{
return CallerClassName;
}
else
{
int lastDot = CallerClassName.LastIndexOf('.');
if (lastDot < 0 || lastDot >= CallerClassName.Length - 1)
return CallerClassName;
else
return CallerClassName.Substring(lastDot + 1);
}
}

method = method ?? GetCallerStackFrameMethod(0);
if (method == null)
return string.Empty;
Expand Down Expand Up @@ -139,6 +157,7 @@ public int GetCallerLineNumber(int skipFrames)
return frame?.GetFileLineNumber() ?? 0;
}

public string CallerClassName { get; private set; }
public string CallerMemberName { get; private set; }
public string CallerFilePath { get; private set; }
public int? CallerLineNumber { get; private set; }
Expand Down
11 changes: 4 additions & 7 deletions src/NLog/LayoutRenderers/CallSiteLayoutRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,14 @@
// THE POSSIBILITY OF SUCH DAMAGE.
//

using System;

namespace NLog.LayoutRenderers
{
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;
using Config;
using Internal;
using NLog.Config;
using NLog.Internal;

/// <summary>
/// The call site (class name, method name and source information).
Expand Down Expand Up @@ -178,7 +175,7 @@ protected override void Append(StringBuilder builder, LogEventInfo logEvent)
if (FileName)
{
string fileName = logEvent.CallSiteInformation.GetCallerFilePath(SkipFrames);
if (fileName != null)
if (!string.IsNullOrEmpty(fileName))
{
int lineNumber = logEvent.CallSiteInformation.GetCallerLineNumber(SkipFrames);
AppendFileName(builder, fileName, lineNumber);
Expand Down
10 changes: 8 additions & 2 deletions src/NLog/LogEventInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ public LogEventInfo(LogLevel level, string loggerName, IFormatProvider formatPro
/// </summary>
public StackTrace StackTrace => CallSiteInformation?.StackTrace;

/// <summary>
/// Gets the callsite class name
/// </summary>
public string CallerClassName => CallSiteInformation?.CallerClassName;

/// <summary>
/// Gets the callsite member function name
/// </summary>
Expand Down Expand Up @@ -516,12 +521,13 @@ public void SetStackTrace(StackTrace stackTrace, int userStackFrame)
/// <summary>
/// Sets the details retrieved from the Caller Information Attributes
/// </summary>
/// <param name="callerClassName"></param>
/// <param name="callerMemberName"></param>
/// <param name="callerFilePath"></param>
/// <param name="callerLineNumber"></param>
public void SetCallerInfo(string callerMemberName, string callerFilePath, int callerLineNumber)
public void SetCallerInfo(string callerClassName, string callerMemberName, string callerFilePath, int callerLineNumber)
{
GetCallSiteInformationInternal().SetCallerInfo(callerMemberName, callerFilePath, callerLineNumber);
GetCallSiteInformationInternal().SetCallerInfo(callerClassName, callerMemberName, callerFilePath, callerLineNumber);
}

internal string AddCachedLayoutValue(Layout layout, string value)
Expand Down

0 comments on commit b143e9c

Please sign in to comment.