Skip to content

Commit

Permalink
Handle and log string format exception (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
vishal-gandhi authored and simonporter committed Nov 19, 2016
1 parent 5440b1f commit 3e2c6f0
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions Framework/Tracing/TraceHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,23 @@ static string GetFormattedString(string iid, string eid, string message, params
{
formatted = "msg=" + message;
}
return string.Format(formatted, args);

if (args == null)
{
return formatted;
}

try
{
return string.Format(formatted, args);
}
catch (FormatException ex)
{
source.TraceEvent(TraceEventType.Error, 0,
"String FormatException for '{0}'. Args count: {1}. Exception: {2}", formatted, args.Length, ex);

return formatted;
}
}

static void ExceptionHandlingWrapper(Action innerFunc)
Expand All @@ -207,7 +223,7 @@ static void ExceptionHandlingWrapper(Action innerFunc)
}
try
{
source.TraceEvent(TraceEventType.Critical, 0,
source.TraceEvent(TraceEventType.Error, 0,
$"Failed to log actual trace because one or more trace listeners threw an exception: {exception}");
}
catch (Exception anotherException)
Expand Down

0 comments on commit 3e2c6f0

Please sign in to comment.