Skip to content

Commit 38a304b

Browse files
committed
# Conflicts: # Dlls/StackifyLib.ELMAH/StackifyLib.ELMAH.dll # Dlls/StackifyLib.log4net.v1_2_10/StackifyLib.log4net.dll # Dlls/StackifyLib.log4net/StackifyLib.log4net.dll # Dlls/StackifyLib.nLog/StackifyLib.nLog.dll # Dlls/StackifyLib/StackifyLib.dll
2 parents e54c181 + fdda827 commit 38a304b

File tree

5 files changed

+57
-12
lines changed

5 files changed

+57
-12
lines changed
0 Bytes
Binary file not shown.

Src/StackifyLib.log4net.Sitecore/StackifyAppender.cs

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ internal LogMsg Translate(LoggingEvent loggingEvent)
176176
StackifyError error = null;
177177
object messageObject = null;
178178
string errorAdditionalMessage = null;
179-
180179
if (loggingEvent.MessageObject != null && loggingEvent.MessageObject is StackifyError)
181180
{
182181
//Message Object was an exception
@@ -211,13 +210,35 @@ internal LogMsg Translate(LoggingEvent loggingEvent)
211210
// messageObject = loggingEvent.MessageObject;
212211
//}
213212
else
214-
{
215-
messageObject = loggingEvent.MessageObject;
216-
}
217-
218-
219-
//messageObject is not an object we need to serialize.
220-
if (messageObject == null || messageObject is string || messageObject.GetType().FullName == "log4net.Util.SystemStringFormat")
213+
{
214+
// we try to retrieve the thrown exception but wait to do this check until this last condition
215+
// block since we need to use reflection which is an expensive operation
216+
Exception thrownException = GetThrownException(loggingEvent);
217+
218+
if (thrownException == null)
219+
{
220+
// the thrown exception does not exist, so we use the basic information provided in the message object
221+
messageObject = loggingEvent.MessageObject;
222+
}
223+
else
224+
{
225+
if (thrownException is StackifyError)
226+
{
227+
error = thrownException as StackifyError;
228+
}
229+
else
230+
{
231+
error = StackifyError.New(thrownException);
232+
}
233+
234+
errorAdditionalMessage = loggingEvent.RenderedMessage;
235+
messageObject = loggingEvent.MessageObject;
236+
}
237+
}
238+
239+
240+
//messageObject is not an object we need to serialize.
241+
if (messageObject == null || messageObject is string || messageObject.GetType().FullName == "log4net.Util.SystemStringFormat")
221242
{
222243
//passing null to the serialize object since we can't serialize the logged object. We only need to get potential diags.
223244
msg.data = StackifyLib.Utils.HelperFunctions.SerializeDebugData(null, false, diags);
@@ -283,7 +304,23 @@ internal LogMsg Translate(LoggingEvent loggingEvent)
283304
return msg;
284305
}
285306

286-
private Dictionary<string, object> GetDiagnosticContextProperties()
307+
private Exception GetThrownException(LoggingEvent loggingEvent)
308+
{
309+
// Since Sitecore's implementation does not expose the ExceptionObject, we need to use reflection to get access
310+
// to it, as per Matt Watson from Stackify. We know they store this Exception in a field with the name m_thrownException
311+
// so we retrieve the private field based on its name.
312+
Exception thrownException = null;
313+
Type loggingEventType = loggingEvent.GetType();
314+
var thrownExceptionFieldInfo = loggingEventType.GetField("m_thrownException", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
315+
if (thrownExceptionFieldInfo != null)
316+
{
317+
thrownException = thrownExceptionFieldInfo.GetValue(loggingEvent) as Exception;
318+
}
319+
320+
return thrownException;
321+
}
322+
323+
private Dictionary<string, object> GetDiagnosticContextProperties()
287324
{
288325
if (!_HasContextKeys)
289326
{

Src/StackifyLib/Config.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static string Get(string key, string defaultValue = null)
2727
if (key != null)
2828
{
2929
v = ConfigurationManager.AppSettings[key];
30-
if (v == null)
30+
if (string.IsNullOrEmpty(v))
3131
v = Environment.GetEnvironmentVariable(key);
3232
}
3333
}

Src/StackifyLib/Internal/Logs/LogClient.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,15 @@ public void QueueMessage(LogMsg msg)
140140
}
141141

142142
//Used by Stackify profiler only
143-
msg.SetLogMsgID(msg.id, isError, msg.Level, msg.Msg, msg.data);
143+
if (Logger.PrefixEnabled())
144+
{
145+
msg.SetLogMsgID(msg.id, isError, msg.Level, msg.Msg, msg.data);
146+
}
147+
else
148+
{
149+
msg.SetLogMsgID(msg.id, isError, msg.Level, null, null);
150+
}
151+
144152

145153
//We need to do everything up to this point for sasquatch. Even if we aren't uploading the log.
146154
if (this.CanQueue())

Src/StackifyLib/Logger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ public static bool PrefixEnabled()
308308
if (_PrefixEnabled != null)
309309
return _PrefixEnabled.Value;
310310

311-
var variable = Environment.GetEnvironmentVariable("StackSquatchUpdated");
311+
var variable = Environment.GetEnvironmentVariable("StackSquatchUpdated", EnvironmentVariableTarget.Machine);
312312

313313

314314
if (!string.IsNullOrEmpty(variable))

0 commit comments

Comments
 (0)