Skip to content

Commit f89b80e

Browse files
author
mwatson
committed
Performance optimization to only get one method from the stack and simpler method names for logging. NLog bug fix with duplicate logging of diagnostic data. NLog support for NDC
1 parent 0336d55 commit f89b80e

File tree

11 files changed

+56
-20
lines changed

11 files changed

+56
-20
lines changed
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

Dlls/StackifyLib/StackifyLib.dll

0 Bytes
Binary file not shown.

Src/StackifyLib.log4net.v1_2_10/StackifyAppender.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ internal LogMsg Translate(LoggingEvent loggingEvent)
150150

151151
if (logMethodNames)
152152
{
153-
var frames = StackifyLib.Logger.GetCurrentStackTrace(loggingEvent.LoggerName);
153+
var frames = StackifyLib.Logger.GetCurrentStackTrace(loggingEvent.LoggerName, 1, true);
154154

155155
if (frames.Any())
156156
{

Src/StackifyLib.log4net/StackifyAppender.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ internal LogMsg Translate(LoggingEvent loggingEvent)
141141

142142
if (logMethodNames)
143143
{
144-
var frames = StackifyLib.Logger.GetCurrentStackTrace(loggingEvent.LoggerName);
144+
var frames = StackifyLib.Logger.GetCurrentStackTrace(loggingEvent.LoggerName, 1, true);
145145

146146
if (frames.Any())
147147
{
@@ -285,7 +285,7 @@ private Dictionary<string, object> GetDiagnosticContextProperties()
285285
return null;
286286
}
287287

288-
288+
289289
Dictionary<string, object> properties = new Dictionary<string, object>();
290290

291291

Src/StackifyLib.nLog/StackifyTarget.cs

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,23 @@ protected override void Write(LogEventInfo logEvent)
9393

9494
private Dictionary<string, object> GetDiagnosticContextProperties()
9595
{
96-
if (!_HasContextKeys)
96+
97+
98+
Dictionary<string, object> properties = new Dictionary<string, object>();
99+
100+
101+
string ndc = NLog.NestedDiagnosticsContext.TopMessage;
102+
103+
if (!String.IsNullOrEmpty(ndc))
97104
{
98-
return null;
105+
properties["ndc"] = ndc;
99106
}
100107

101-
Dictionary<string, object> properties = new Dictionary<string, object>();
108+
109+
if (!_HasContextKeys)
110+
{
111+
return properties;
112+
}
102113

103114
// GlobalDiagnosticsContext
104115

@@ -114,7 +125,6 @@ private Dictionary<string, object> GetDiagnosticContextProperties()
114125
}
115126
}
116127
}
117-
118128
// MappedDiagnosticsContext
119129

120130
foreach (string mdcKey in _MappedContextKeys)
@@ -185,7 +195,7 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
185195

186196
if (logMethodNames)
187197
{
188-
var frames = StackifyLib.Logger.GetCurrentStackTrace(loggingEvent.LoggerName);
198+
var frames = StackifyLib.Logger.GetCurrentStackTrace(loggingEvent.LoggerName, 1, true);
189199

190200
if (frames.Any())
191201
{
@@ -217,14 +227,7 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
217227
}
218228

219229

220-
if ((loggingEvent.Parameters != null) && (loggingEvent.Parameters.Length > 0))
221-
{
222-
msg.data = StackifyLib.Utils.HelperFunctions.SerializeDebugData(loggingEvent.Parameters[0], true, diags);
223-
}
224-
else
225-
{
226-
msg.data = StackifyLib.Utils.HelperFunctions.SerializeDebugData(null, false, diags);
227-
}
230+
228231

229232
string formattedMessage;
230233

@@ -239,7 +242,29 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
239242
formattedMessage = loggingEvent.FormattedMessage;
240243
}
241244

242-
msg.Msg = formattedMessage;
245+
msg.Msg = (formattedMessage ?? "").Trim();
246+
247+
object debugObject = null;
248+
249+
if ((loggingEvent.Parameters != null) && (loggingEvent.Parameters.Length > 0))
250+
{
251+
debugObject = loggingEvent.Parameters[0];
252+
253+
//if the debug param is the same as the logging message itself, suppress it
254+
if (debugObject != null && debugObject.ToString() == msg.Msg)
255+
{
256+
debugObject = null;
257+
}
258+
}
259+
260+
if (debugObject != null)
261+
{
262+
msg.data = StackifyLib.Utils.HelperFunctions.SerializeDebugData(debugObject, true, diags);
263+
}
264+
else
265+
{
266+
msg.data = StackifyLib.Utils.HelperFunctions.SerializeDebugData(null, false, diags);
267+
}
243268

244269

245270
if (msg.Msg != null && error != null)
0 Bytes
Binary file not shown.

Src/StackifyLib/Logger.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ public static void QueueLogObject(StackifyLib.Models.LogMsg msg, Exception excep
223223
/// </summary>
224224
/// <param name="declaringClassName"></param>
225225
/// <returns></returns>
226-
public static List<TraceFrame> GetCurrentStackTrace(string declaringClassName)
226+
public static List<TraceFrame> GetCurrentStackTrace(string declaringClassName, int maxFrames = 99, bool simpleMethodNames = false)
227227
{
228228
List<TraceFrame> frames = new List<TraceFrame>();
229229

@@ -257,8 +257,13 @@ public static List<TraceFrame> GetCurrentStackTrace(string declaringClassName)
257257
var f2 = new TraceFrame();
258258
f2.CodeFileName = frame2.GetFileName();
259259
f2.LineNum = frame2.GetFileLineNumber();
260-
f2.Method = ErrorItem.GetMethodFullName(frame2.GetMethod());
260+
f2.Method = ErrorItem.GetMethodFullName(frame2.GetMethod(), simpleMethodNames);
261261
frames.Add(f2);
262+
263+
if (frames.Count > maxFrames)
264+
{
265+
return frames;
266+
}
262267
}
263268

264269
}

0 commit comments

Comments
 (0)