Skip to content

Commit 8928796

Browse files
committed
NLog.Targets.Stackify - Added logAllProperties for structured logging
1 parent ed40f4c commit 8928796

File tree

3 files changed

+38
-36
lines changed

3 files changed

+38
-36
lines changed

Src/NLog.Targets.Stackify/NLog.Targets.Stackify.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<AssemblyTitle>NLog.Targets.Stackify</AssemblyTitle>
55
<VersionPrefix>2.0.0</VersionPrefix>
6-
<TargetFrameworks>netstandard1.3;net40;net45</TargetFrameworks>
6+
<TargetFrameworks>netstandard1.5;net40;net45</TargetFrameworks>
77
<AssemblyName>NLog.Targets.Stackify</AssemblyName>
88
<PackageId>NLog.Targets.Stackify</PackageId>
99
<PackageTags>stackify;errors;logs</PackageTags>
@@ -27,8 +27,8 @@
2727
<ProjectReference Include="..\StackifyLib\StackifyLib.csproj" />
2828
</ItemGroup>
2929

30-
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.3' ">
31-
<PackageReference Include="NLog" Version="5.0.0-beta05" />
30+
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.5' ">
31+
<PackageReference Include="NLog" Version="4.5.0-rc02" />
3232
</ItemGroup>
3333

3434
<ItemGroup Condition=" '$(TargetFramework)' == 'net40' ">

Src/NLog.Targets.Stackify/StackifyTarget.cs

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
#if NET45 || NET40
88
using System.Runtime.Remoting.Messaging;
99
#endif
10-
using System.Security;
11-
using System.Text;
12-
using System.Threading.Tasks;
1310
using StackifyLib;
1411
using System.Diagnostics;
1512
using StackifyLib.Internal.Logs;
@@ -31,6 +28,7 @@ public class StackifyTarget : TargetWithLayout
3128
public string callContextKeys { get; set; }
3229
public bool? logMethodNames { get; set; }
3330
public bool? logAllParams { get; set; }
31+
public bool? logAllProperties { get; set; }
3432

3533
private List<string> _GlobalContextKeys = new List<string>();
3634
private List<string> _MappedContextKeys = new List<string>();
@@ -99,32 +97,26 @@ protected override void Write(LogEventInfo logEvent)
9997
{
10098
StackifyAPILogger.Log(ex.ToString());
10199
}
102-
103100
}
104101

105102

106103
private Dictionary<string, object> GetDiagnosticContextProperties()
107104
{
108-
109-
110105
Dictionary<string, object> properties = new Dictionary<string, object>();
111106

112-
113107
string ndc = NLog.NestedDiagnosticsContext.TopMessage;
114108

115109
if (!String.IsNullOrEmpty(ndc))
116110
{
117111
properties["ndc"] = ndc;
118112
}
119113

120-
121114
if (!_HasContextKeys)
122115
{
123116
return properties;
124117
}
125118

126119
// GlobalDiagnosticsContext
127-
128120
foreach (string gdcKey in _GlobalContextKeys)
129121
{
130122
if (NLog.GlobalDiagnosticsContext.Contains(gdcKey))
@@ -137,8 +129,8 @@ private Dictionary<string, object> GetDiagnosticContextProperties()
137129
}
138130
}
139131
}
140-
// MappedDiagnosticsContext
141132

133+
// MappedDiagnosticsContext
142134
foreach (string mdcKey in _MappedContextKeys)
143135
{
144136
if (NLog.MappedDiagnosticsContext.Contains(mdcKey))
@@ -150,6 +142,20 @@ private Dictionary<string, object> GetDiagnosticContextProperties()
150142
properties.Add(mdcKey.ToLower(), mdcValue);
151143
}
152144
}
145+
}
146+
147+
// MappedDiagnosticsLogicalContext (Async CallContext)
148+
foreach (string mdlcKey in _MappedContextKeys)
149+
{
150+
if (NLog.MappedDiagnosticsLogicalContext.Contains(mdlcKey))
151+
{
152+
string mdcValue = NLog.MappedDiagnosticsLogicalContext.Get(mdlcKey);
153+
154+
if (mdcValue != null)
155+
{
156+
properties.Add(mdlcKey.ToLower(), mdcValue);
157+
}
158+
}
153159
}
154160

155161
#if NET45 || NET40
@@ -170,7 +176,6 @@ private Dictionary<string, object> GetDiagnosticContextProperties()
170176

171177
internal LogMsg Translate(LogEventInfo loggingEvent)
172178
{
173-
174179
if (loggingEvent == null)
175180
return null;
176181

@@ -180,14 +185,11 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
180185

181186
StackifyLib.Models.LogMsg msg = new LogMsg();
182187

183-
184188
if (loggingEvent.Level != null)
185189
{
186190
msg.Level = loggingEvent.Level.Name;
187191
}
188192

189-
190-
191193
if (loggingEvent.HasStackTrace && loggingEvent.UserStackFrame != null)
192194
{
193195
var frame = loggingEvent.UserStackFrame;
@@ -201,10 +203,8 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
201203
msg.SrcLine = frame.GetFileLineNumber();
202204
}
203205
}
204-
205206
}
206207

207-
208208
//if it wasn't set above for some reason we will do it this way as a fallback
209209
if (string.IsNullOrEmpty(msg.SrcMethod))
210210
{
@@ -294,19 +294,29 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
294294
}
295295

296296
var diags = GetDiagnosticContextProperties();
297+
298+
if (logAllProperties ?? true)
299+
{
300+
if (loggingEvent.Properties.Count > 0)
301+
{
302+
foreach (KeyValuePair<object, object> eventProperty in loggingEvent.Properties)
303+
{
304+
string propertyKey = eventProperty.Key.ToString();
305+
if (!string.IsNullOrEmpty(propertyKey))
306+
{
307+
string propertyValue = eventProperty.Value != null ? Convert.ToString(eventProperty.Value, loggingEvent.FormatProvider ?? CultureInfo.CurrentCulture) : null;
308+
diags[propertyKey.ToLower()] = propertyValue;
309+
}
310+
}
311+
}
312+
}
313+
297314
if (diags != null && diags.ContainsKey("transid"))
298315
{
299316
msg.TransID = diags["transid"].ToString();
300317
diags.Remove("transid");
301318
}
302319

303-
304-
305-
306-
307-
308-
309-
310320
if (debugObject != null)
311321
{
312322
msg.data = StackifyLib.Utils.HelperFunctions.SerializeDebugData(debugObject, true, diags);
@@ -315,7 +325,6 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
315325
{
316326
msg.data = StackifyLib.Utils.HelperFunctions.SerializeDebugData(null, false, diags);
317327
}
318-
319328

320329
if (msg.Msg != null && error != null)
321330
{
@@ -357,11 +366,8 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
357366
msg.Msg += " #errorgoverned";
358367
}
359368

360-
361369
return msg;
362370
}
363371

364-
365-
366372
}
367373
}

Src/StackifyLib/Utils/HelperFunctions.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class HelperFunctions
1616
{
1717
static List<string> _BadTypes = new List<string>() { "log4net.Util.SystemStringFormat", "System.Object[]" };
1818
static JsonSerializer serializer = new JsonSerializer { ReferenceLoopHandling = ReferenceLoopHandling.Ignore };
19+
static JsonSerializerSettings serializerSettings = new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore, ReferenceLoopHandling = ReferenceLoopHandling.Ignore };
1920

2021
/// <summary>
2122
/// Trying to serialize something that the user passed in. Sometimes this is used to serialize what we know is additional debug and sometimes it is the primary logged item. This is why the serializeSimpleTypes exists. For additional debug stuff we always serialize it. For the primary logged object we won't because it doesn't make any sense to put a string in the json as well as the main message. It's meant for objects.
@@ -221,12 +222,7 @@ public static string SerializeDebugData(object logObject, bool serializeSimpleTy
221222

222223
if (jObject != null)
223224
{
224-
return JsonConvert.SerializeObject(jObject,
225-
new JsonSerializerSettings()
226-
{
227-
NullValueHandling = NullValueHandling.Ignore,
228-
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
229-
});
225+
return JsonConvert.SerializeObject(jObject, serializerSettings);
230226
}
231227

232228
return null;

0 commit comments

Comments
 (0)