|
3 | 3 |
|
4 | 4 | using System; |
5 | 5 | using Microsoft.Build.Utilities; |
| 6 | +using Microsoft.Build.Framework; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Globalization; |
6 | 9 |
|
7 | 10 | namespace Microsoft.NET.Build.Tasks |
8 | 11 | { |
@@ -38,10 +41,74 @@ public override bool Execute() |
38 | 41 | { |
39 | 42 | Log.LogError(e.Message); |
40 | 43 | } |
| 44 | + catch (Exception e) |
| 45 | + { |
| 46 | + LogErrorTelemetry("taskBaseCatchException", e); |
| 47 | + throw; |
| 48 | + } |
41 | 49 |
|
42 | 50 | return !Log.HasLoggedErrors; |
43 | 51 | } |
44 | 52 |
|
| 53 | + private void LogErrorTelemetry(string eventName, Exception e) |
| 54 | + { |
| 55 | + (BuildEngine as IBuildEngine5)?.LogTelemetry(eventName, new Dictionary<string, string> { |
| 56 | + {"exceptionType", e.GetType().ToString() }, |
| 57 | + {"detail", ExceptionToStringWithoutMessage(e) }}); |
| 58 | + } |
| 59 | + |
| 60 | + private static string ExceptionToStringWithoutMessage(Exception e) |
| 61 | + { |
| 62 | + const string AggregateException_ToString = "{0}{1}---> (Inner Exception #{2}) {3}{4}{5}"; |
| 63 | + if (e is AggregateException aggregate) |
| 64 | + { |
| 65 | + string text = NonAggregateExceptionToStringWithoutMessage(aggregate); |
| 66 | + |
| 67 | + for (int i = 0; i < aggregate.InnerExceptions.Count; i++) |
| 68 | + { |
| 69 | + text = string.Format(CultureInfo.InvariantCulture, |
| 70 | + AggregateException_ToString, |
| 71 | + text, |
| 72 | + Environment.NewLine, |
| 73 | + i, |
| 74 | + ExceptionToStringWithoutMessage(aggregate.InnerExceptions[i]), |
| 75 | + "<---", |
| 76 | + Environment.NewLine); |
| 77 | + } |
| 78 | + |
| 79 | + return text; |
| 80 | + } |
| 81 | + else |
| 82 | + { |
| 83 | + return NonAggregateExceptionToStringWithoutMessage(e); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private static string NonAggregateExceptionToStringWithoutMessage(Exception e) |
| 88 | + { |
| 89 | + string s; |
| 90 | + const string Exception_EndOfInnerExceptionStack = "--- End of inner exception stack trace ---"; |
| 91 | + |
| 92 | + |
| 93 | + s = e.GetType().ToString(); |
| 94 | + |
| 95 | + if (e.InnerException != null) |
| 96 | + { |
| 97 | + s = s + " ---> " + ExceptionToStringWithoutMessage(e.InnerException) + Environment.NewLine + |
| 98 | + " " + Exception_EndOfInnerExceptionStack; |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + var stackTrace = e.StackTrace; |
| 103 | + |
| 104 | + if (stackTrace != null) |
| 105 | + { |
| 106 | + s += Environment.NewLine + stackTrace; |
| 107 | + } |
| 108 | + |
| 109 | + return s; |
| 110 | + } |
| 111 | + |
45 | 112 | protected abstract void ExecuteCore(); |
46 | 113 | } |
47 | 114 | } |
0 commit comments