Skip to content

Commit f2923d6

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

File tree

5 files changed

+65
-78
lines changed

5 files changed

+65
-78
lines changed

Src/CoreConsoleApp/CoreConsoleApp.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121

2222
<ItemGroup>
2323
<PackageReference Include="log4net" Version="2.0.7" />
24-
<PackageReference Include="NLog" Version="5.0.0-beta05" />
24+
<PackageReference Include="NLog" Version="4.5.0-rc02" />
2525
<PackageReference Include="Serilog" Version="2.4.0" />
26-
<PackageReference Include="Microsoft.Extensions.Configuration" Version="1.2.0-preview1-22736" />
27-
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.2.0-preview1-22736" />
28-
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.2.0-preview1-22736" />
26+
<PackageReference Include="Microsoft.Extensions.Configuration" Version="1.1.1" />
27+
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.1.1" />
28+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.1" />
2929
</ItemGroup>
3030

3131
</Project>

Src/CoreWebApp/CoreWebApp.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.0.2" />
3737
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.0.2" />
3838
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.0.2" />
39-
<PackageReference Include="NLog" Version="5.0.0-beta05" />
40-
<PackageReference Include="NLog.Extensions.Logging" Version="1.0.0-rtm-beta2" />
39+
<PackageReference Include="NLog" Version="4.5.0-rc02" />
40+
<PackageReference Include="NLog.Extensions.Logging" Version="1.0.0-rtm-rc4" />
4141
<PackageReference Include="Serilog" Version="2.4.0" />
4242
<PackageReference Include="Serilog.Extensions.Logging" Version="1.4.0" />
4343
<PackageReference Include="Serilog.Extensions.Logging.File" Version="1.0.0" />

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: 46 additions & 55 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,8 @@ 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; }
32+
public bool? logLastParameter { get; set; }
3433

3534
private List<string> _GlobalContextKeys = new List<string>();
3635
private List<string> _MappedContextKeys = new List<string>();
@@ -99,32 +98,26 @@ protected override void Write(LogEventInfo logEvent)
9998
{
10099
StackifyAPILogger.Log(ex.ToString());
101100
}
102-
103101
}
104102

105103

106104
private Dictionary<string, object> GetDiagnosticContextProperties()
107105
{
108-
109-
110106
Dictionary<string, object> properties = new Dictionary<string, object>();
111107

112-
113108
string ndc = NLog.NestedDiagnosticsContext.TopMessage;
114109

115110
if (!String.IsNullOrEmpty(ndc))
116111
{
117112
properties["ndc"] = ndc;
118113
}
119114

120-
121115
if (!_HasContextKeys)
122116
{
123117
return properties;
124118
}
125119

126120
// GlobalDiagnosticsContext
127-
128121
foreach (string gdcKey in _GlobalContextKeys)
129122
{
130123
if (NLog.GlobalDiagnosticsContext.Contains(gdcKey))
@@ -137,8 +130,8 @@ private Dictionary<string, object> GetDiagnosticContextProperties()
137130
}
138131
}
139132
}
140-
// MappedDiagnosticsContext
141133

134+
// MappedDiagnosticsContext
142135
foreach (string mdcKey in _MappedContextKeys)
143136
{
144137
if (NLog.MappedDiagnosticsContext.Contains(mdcKey))
@@ -170,7 +163,6 @@ private Dictionary<string, object> GetDiagnosticContextProperties()
170163

171164
internal LogMsg Translate(LogEventInfo loggingEvent)
172165
{
173-
174166
if (loggingEvent == null)
175167
return null;
176168

@@ -180,14 +172,11 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
180172

181173
StackifyLib.Models.LogMsg msg = new LogMsg();
182174

183-
184175
if (loggingEvent.Level != null)
185176
{
186177
msg.Level = loggingEvent.Level.Name;
187178
}
188179

189-
190-
191180
if (loggingEvent.HasStackTrace && loggingEvent.UserStackFrame != null)
192181
{
193182
var frame = loggingEvent.UserStackFrame;
@@ -201,10 +190,8 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
201190
msg.SrcLine = frame.GetFileLineNumber();
202191
}
203192
}
204-
205193
}
206194

207-
208195
//if it wasn't set above for some reason we will do it this way as a fallback
209196
if (string.IsNullOrEmpty(msg.SrcMethod))
210197
{
@@ -240,48 +227,63 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
240227
msg.Msg = (formattedMessage ?? "").Trim();
241228

242229
object debugObject = null;
243-
Dictionary<string, object> args = new Dictionary<string, object>();
244230

245-
if ((loggingEvent.Parameters != null) && (loggingEvent.Parameters.Length > 0))
231+
if ((logAllProperties ?? true) && loggingEvent.Properties.Count > 0)
246232
{
247-
248-
for (int i = 0; i < loggingEvent.Parameters.Length; i++)
233+
Dictionary<string, object> args = new Dictionary<string, object>();
234+
foreach (KeyValuePair<object, object> eventProperty in loggingEvent.Properties)
249235
{
250-
var item = loggingEvent.Parameters[i];
251-
252-
if (item == null)
236+
string propertyKey = eventProperty.Key.ToString();
237+
if (!string.IsNullOrEmpty(propertyKey))
253238
{
254-
continue;
239+
args[propertyKey] = eventProperty.Value;
255240
}
256-
else if (item is Exception)
241+
}
242+
debugObject = args;
243+
}
244+
245+
if (loggingEvent.Parameters != null && loggingEvent.Parameters.Length > 0 && (debugObject == null || logAllParams == true))
246+
{
247+
Dictionary<string, object> args = (logAllParams ?? true) ? new Dictionary<string, object>() : null;
248+
if (args != null || (logLastParameter ?? true))
249+
{
250+
for (int i = 0; i < loggingEvent.Parameters.Length; i++)
257251
{
258-
if (loggingEvent.Exception == null)
252+
var item = loggingEvent.Parameters[i];
253+
254+
if (item == null)
259255
{
260-
loggingEvent.Exception = (Exception)item;
256+
continue;
257+
}
258+
else if (item is Exception)
259+
{
260+
if (loggingEvent.Exception == null)
261+
{
262+
loggingEvent.Exception = (Exception)item;
263+
}
264+
}
265+
else if (item.ToString() == msg.Msg)
266+
{
267+
//ignore it.
268+
}
269+
else if (args != null)
270+
{
271+
args["arg" + i] = loggingEvent.Parameters[i];
272+
debugObject = item;
273+
}
274+
else
275+
{
276+
debugObject = item;
261277
}
262278
}
263-
else if (item.ToString() == msg.Msg)
264-
{
265-
//ignore it.
266-
}
267-
else if (logAllParams ?? true)
268-
{
269-
args["arg" + i] = loggingEvent.Parameters[i];
270-
debugObject = item;
271-
}
272-
else
279+
280+
if (args != null && args.Count > 1)
273281
{
274-
debugObject = item;
282+
debugObject = args;
275283
}
276284
}
277-
278-
if ((logAllParams ?? true) && args != null && args.Count > 1)
279-
{
280-
debugObject = args;
281-
}
282285
}
283286

284-
285287
StackifyError error = null;
286288

287289
if (loggingEvent.Exception != null && loggingEvent.Exception is StackifyError)
@@ -300,13 +302,6 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
300302
diags.Remove("transid");
301303
}
302304

303-
304-
305-
306-
307-
308-
309-
310305
if (debugObject != null)
311306
{
312307
msg.data = StackifyLib.Utils.HelperFunctions.SerializeDebugData(debugObject, true, diags);
@@ -315,7 +310,6 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
315310
{
316311
msg.data = StackifyLib.Utils.HelperFunctions.SerializeDebugData(null, false, diags);
317312
}
318-
319313

320314
if (msg.Msg != null && error != null)
321315
{
@@ -357,11 +351,8 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
357351
msg.Msg += " #errorgoverned";
358352
}
359353

360-
361354
return msg;
362355
}
363356

364-
365-
366357
}
367358
}

Src/StackifyLib/Utils/HelperFunctions.cs

Lines changed: 10 additions & 14 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.
@@ -126,21 +127,22 @@ public static string SerializeDebugData(object logObject, bool serializeSimpleTy
126127
}
127128
else
128129
{
129-
130130
if (!typeInfo.ContainsGenericParameters)
131131
{
132132
jObject.Add("objectType", type.FullName);
133133
}
134-
#if NET451 || NET45 || NET40
135134
else
136135
{
137-
136+
#if NET451 || NET45 || NET40
138137
var genericArgs = typeInfo.GetGenericArguments();
139-
140-
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)
141144
{
142145
var childtype = genericArgs.First();
143-
144146
#if NET40
145147
var childtypeinfo = childtype;
146148
#else
@@ -161,7 +163,6 @@ public static string SerializeDebugData(object logObject, bool serializeSimpleTy
161163
jObject.Add("objectType", type.FullName);
162164
}
163165
}
164-
#endif
165166
}
166167
}
167168
else if (token is JValue)
@@ -185,7 +186,7 @@ public static string SerializeDebugData(object logObject, bool serializeSimpleTy
185186
}
186187

187188
string data = null;
188-
if (properties != null && properties.Any())
189+
if (properties != null && properties.Count > 0)
189190
{
190191

191192
if (jObject == null)
@@ -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)