-
Notifications
You must be signed in to change notification settings - Fork 169
/
Copy pathTraceUtil.cs
68 lines (62 loc) · 2.27 KB
/
TraceUtil.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Diagnostics;
using System.Text;
namespace Nexus.Client.Util
{
/// <summary>
/// Utility functions to work with the Tracer.
/// </summary>
public static class TraceUtil
{
/// <summary>
/// This writes information detailing the given exception to the trace log.
/// </summary>
/// <param name="ex">The exceptions to describe.</param>
public static void TraceException(Exception ex)
{
Trace.TraceError(CreateTraceExceptionString(ex));
}
/// <summary>
/// This builds a string detailing the given exception.
/// </summary>
/// <param name="ex">The exceptions to describe.</param>
/// <returns>A string detailing the given exception.</returns>
public static string CreateTraceExceptionString(Exception ex)
{
if (ex == null)
return "\tNO EXCEPTION.";
StringBuilder stbException = new StringBuilder();
stbException.AppendLine("Exception: ");
stbException.AppendLine("Message: ").Append("\t");
stbException.AppendLine(ex.Message);
stbException.AppendLine("Full Trace: ").Append("\t");
stbException.AppendLine(ex.ToString());
if (ex is BadImageFormatException)
{
BadImageFormatException biex = (BadImageFormatException)ex;
stbException.AppendFormat("File Name:\t{0}", biex.FileName).AppendLine();
stbException.AppendFormat("Fusion Log:\t{0}", biex.FusionLog).AppendLine();
}
while (ex.InnerException != null)
{
ex = ex.InnerException;
stbException.AppendLine("Inner Exception:");
stbException.AppendLine(ex.ToString());
}
return stbException.ToString();
}
public static void TraceAggregateException(AggregateException e)
{
var exceptionTrace = new StringBuilder("AggregateException:");
exceptionTrace.AppendLine($"Message: {e.Message}");
exceptionTrace.AppendLine($"Stacktrace:\n{e.StackTrace}");
exceptionTrace.AppendLine("Inner exceptions:\n----------------");
foreach (var exception in e.Flatten().InnerExceptions)
{
exceptionTrace.AppendLine(exception.ToString());
exceptionTrace.AppendLine("----------------");
}
Trace.TraceError(exceptionTrace.ToString());
}
}
}