Skip to content

Commit 6033778

Browse files
committed
NLog.Targets.Stackify - LogEvent Properties are different from LogEvent Context
1 parent f1e111a commit 6033778

File tree

2 files changed

+35
-33
lines changed

2 files changed

+35
-33
lines changed

Src/NLog.Targets.Stackify/StackifyTarget.cs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class StackifyTarget : TargetWithLayout
2525
public string uri { get; set; }
2626
public string globalContextKeys { get; set; }
2727
public string mappedContextKeys { get; set; }
28+
public string mappedLogicalContextKeys { get; set; }
2829
public string callContextKeys { get; set; }
2930
public bool? logMethodNames { get; set; }
3031
public bool? logAllParams { get; set; }
@@ -33,6 +34,7 @@ public class StackifyTarget : TargetWithLayout
3334

3435
private List<string> _GlobalContextKeys = new List<string>();
3536
private List<string> _MappedContextKeys = new List<string>();
37+
private List<string> _MappedLogicalContextKeys = new List<string>();
3638
private List<string> _CallContextKeys = new List<string>();
3739

3840
private LogClient _logClient = null;
@@ -66,13 +68,17 @@ protected override void InitializeTarget()
6668
_MappedContextKeys = mappedContextKeys.Split(',').Select(s => s.Trim()).ToList();
6769
}
6870

71+
if (!String.IsNullOrEmpty(mappedLogicalContextKeys))
72+
{
73+
_MappedLogicalContextKeys = mappedLogicalContextKeys.Split(',').Select(s => s.Trim()).ToList();
74+
}
75+
6976
if (!String.IsNullOrEmpty(callContextKeys))
7077
{
7178
_CallContextKeys = callContextKeys.Split(',').Select(s => s.Trim()).ToList();
7279
}
7380

74-
75-
_HasContextKeys = _GlobalContextKeys.Any() || _MappedContextKeys.Any() || _CallContextKeys.Any();
81+
_HasContextKeys = _GlobalContextKeys.Any() || _MappedContextKeys.Any() || _MappedLogicalContextKeys.Any() || _CallContextKeys.Any();
7682
}
7783

7884
protected override void Write(LogEventInfo logEvent)
@@ -143,10 +149,10 @@ private Dictionary<string, object> GetDiagnosticContextProperties()
143149
properties.Add(mdcKey.ToLower(), mdcValue);
144150
}
145151
}
146-
}
147-
148-
// MappedDiagnosticsLogicalContext (Async CallContext)
149-
foreach (string mdlcKey in _MappedContextKeys)
152+
}
153+
154+
// MappedDiagnosticsLogicalContext (Async CallContext)
155+
foreach (string mdlcKey in _MappedLogicalContextKeys)
150156
{
151157
if (NLog.MappedDiagnosticsLogicalContext.Contains(mdlcKey))
152158
{
@@ -242,8 +248,21 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
242248

243249
object debugObject = null;
244250

245-
if ((loggingEvent.Parameters != null) && (loggingEvent.Parameters.Length > 0))
246-
{
251+
if ((logAllProperties ?? true) && loggingEvent.Properties.Count > 0)
252+
{
253+
Dictionary<string, object> args = new Dictionary<string, object>();
254+
foreach (KeyValuePair<object, object> eventProperty in loggingEvent.Properties)
255+
{
256+
string propertyKey = eventProperty.Key.ToString();
257+
if (!string.IsNullOrEmpty(propertyKey))
258+
{
259+
args[propertyKey] = eventProperty.Value;
260+
}
261+
}
262+
debugObject = args;
263+
}
264+
else if (loggingEvent.Parameters != null && loggingEvent.Parameters.Length > 0)
265+
{
247266
Dictionary<string, object> args = (logAllParams ?? true) ? new Dictionary<string, object>() : null;
248267
if (args != null || (logLastParameter ?? true))
249268
{
@@ -284,7 +303,6 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
284303
}
285304
}
286305

287-
288306
StackifyError error = null;
289307

290308
if (loggingEvent.Exception != null && loggingEvent.Exception is StackifyError)
@@ -297,22 +315,6 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
297315
}
298316

299317
var diags = GetDiagnosticContextProperties();
300-
301-
if (logAllProperties ?? true)
302-
{
303-
if (loggingEvent.Properties.Count > 0)
304-
{
305-
foreach (KeyValuePair<object, object> eventProperty in loggingEvent.Properties)
306-
{
307-
string propertyKey = eventProperty.Key.ToString();
308-
if (!string.IsNullOrEmpty(propertyKey))
309-
{
310-
diags[propertyKey.ToLower()] = eventProperty.Value;
311-
}
312-
}
313-
}
314-
}
315-
316318
if (diags != null && diags.ContainsKey("transid"))
317319
{
318320
msg.TransID = diags["transid"].ToString();

Src/StackifyLib/Utils/HelperFunctions.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,21 +127,22 @@ public static string SerializeDebugData(object logObject, bool serializeSimpleTy
127127
}
128128
else
129129
{
130-
131130
if (!typeInfo.ContainsGenericParameters)
132131
{
133132
jObject.Add("objectType", type.FullName);
134133
}
135-
#if NET451 || NET45 || NET40
136134
else
137135
{
138-
136+
#if NET451 || NET45 || NET40
139137
var genericArgs = typeInfo.GetGenericArguments();
140-
141-
if (genericArgs.Any())
138+
#else
139+
var genericArgs = typeInfo.IsGenericTypeDefinition ?
140+
type.GetTypeInfo().GenericTypeParameters :
141+
type.GetTypeInfo().GenericTypeArguments;
142+
#endif
143+
if (genericArgs != null && genericArgs.Length > 0)
142144
{
143145
var childtype = genericArgs.First();
144-
145146
#if NET40
146147
var childtypeinfo = childtype;
147148
#else
@@ -162,7 +163,6 @@ public static string SerializeDebugData(object logObject, bool serializeSimpleTy
162163
jObject.Add("objectType", type.FullName);
163164
}
164165
}
165-
#endif
166166
}
167167
}
168168
else if (token is JValue)
@@ -186,7 +186,7 @@ public static string SerializeDebugData(object logObject, bool serializeSimpleTy
186186
}
187187

188188
string data = null;
189-
if (properties != null && properties.Any())
189+
if (properties != null && properties.Count > 0)
190190
{
191191

192192
if (jObject == null)

0 commit comments

Comments
 (0)