Skip to content

Commit 7e71630

Browse files
author
mwatson
committed
Fix log message when using our API directly. Fix bug in nlog with not picking up exception object
1 parent e76bddb commit 7e71630

File tree

4 files changed

+74
-39
lines changed

4 files changed

+74
-39
lines changed
512 Bytes
Binary file not shown.

Dlls/StackifyLib/StackifyLib.dll

0 Bytes
Binary file not shown.

Src/StackifyLib.nLog/StackifyTarget.cs

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -218,28 +218,6 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
218218
}
219219
}
220220
}
221-
222-
223-
StackifyError error = null;
224-
225-
if (loggingEvent.Exception != null && loggingEvent.Exception is StackifyError)
226-
{
227-
error = (StackifyError) loggingEvent.Exception;
228-
}
229-
else if (loggingEvent.Exception != null)
230-
{
231-
error = StackifyError.New((Exception)loggingEvent.Exception);
232-
}
233-
234-
var diags = GetDiagnosticContextProperties();
235-
if (diags != null && diags.ContainsKey("transid"))
236-
{
237-
msg.TransID = diags["transid"].ToString();
238-
diags.Remove("transid");
239-
}
240-
241-
242-
243221

244222
string formattedMessage;
245223

@@ -257,29 +235,73 @@ internal LogMsg Translate(LogEventInfo loggingEvent)
257235
msg.Msg = (formattedMessage ?? "").Trim();
258236

259237
object debugObject = null;
238+
Dictionary<string, object> args = new Dictionary<string, object>();
260239

261240
if ((loggingEvent.Parameters != null) && (loggingEvent.Parameters.Length > 0))
262241
{
263-
debugObject = loggingEvent.Parameters[0];
264242

265-
//if the debug param is the same as the logging message itself, suppress it
266-
if (debugObject != null && debugObject.ToString() == msg.Msg)
267-
{
268-
debugObject = null;
269-
}
270-
else if (logAllParams ?? true)
243+
for (int i = 0; i < loggingEvent.Parameters.Length; i++)
271244
{
272-
Dictionary<string, object> args = new Dictionary<string, object>();
245+
var item = loggingEvent.Parameters[i];
273246

274-
for (int i = 0; i < loggingEvent.Parameters.Length; i++)
247+
if (item == null)
248+
{
249+
continue;
250+
}
251+
else if (item is Exception)
252+
{
253+
if (loggingEvent.Exception == null)
254+
{
255+
loggingEvent.Exception = (Exception)item;
256+
}
257+
}
258+
else if (item.ToString() == msg.Msg)
259+
{
260+
//ignore it.
261+
}
262+
else if (logAllParams ?? true)
275263
{
276264
args["arg" + i] = loggingEvent.Parameters[i];
265+
debugObject = item;
277266
}
267+
else
268+
{
269+
debugObject = item;
270+
}
271+
}
278272

273+
if ((logAllParams ?? true) && args != null && args.Count > 1)
274+
{
279275
debugObject = args;
280276
}
281277
}
282278

279+
280+
StackifyError error = null;
281+
282+
if (loggingEvent.Exception != null && loggingEvent.Exception is StackifyError)
283+
{
284+
error = (StackifyError) loggingEvent.Exception;
285+
}
286+
else if (loggingEvent.Exception != null)
287+
{
288+
error = StackifyError.New((Exception)loggingEvent.Exception);
289+
}
290+
291+
var diags = GetDiagnosticContextProperties();
292+
if (diags != null && diags.ContainsKey("transid"))
293+
{
294+
msg.TransID = diags["transid"].ToString();
295+
diags.Remove("transid");
296+
}
297+
298+
299+
300+
301+
302+
303+
304+
283305
if (debugObject != null)
284306
{
285307
msg.data = StackifyLib.Utils.HelperFunctions.SerializeDebugData(debugObject, true, diags);

Src/StackifyLib/Logger.cs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,24 +177,37 @@ public static void QueueLogObject(StackifyLib.Models.LogMsg msg)
177177

178178
if (msg.Ex != null)
179179
{
180+
if (string.IsNullOrEmpty(msg.Level))
181+
{
182+
msg.Level = "ERROR";
183+
}
184+
185+
string origMsg = msg.Msg;
186+
187+
if (msg.Msg != null && msg.Ex != null)
188+
{
189+
msg.Msg += "\r\n" + msg.Ex.ToString();
190+
}
191+
else if (msg.Msg == null && msg.Ex != null)
192+
{
193+
msg.Msg = msg.Ex.ToString();
194+
}
195+
180196
if (!StackifyError.IgnoreError(msg.Ex) && _LogClient.ErrorShouldBeSent(msg.Ex))
181197
{
182-
if (!string.IsNullOrEmpty(msg.Msg))
198+
if (!string.IsNullOrEmpty(origMsg))
183199
{
184-
msg.Ex.SetAdditionalMessage(msg.Msg);
200+
msg.Ex.SetAdditionalMessage(origMsg);
185201
}
186202

187-
msg.Msg = msg.Ex.ToString();
203+
//remove because of so many errors
204+
msg.Ex = null;
188205
}
189206
else
190207
{
191208
msg.Msg += " #errorgoverned";
192209
}
193-
194-
if (string.IsNullOrEmpty(msg.Level))
195-
{
196-
msg.Level = "ERROR";
197-
}
210+
198211
}
199212

200213
_LogClient.QueueMessage(msg);

0 commit comments

Comments
 (0)