forked from dotnet/sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTaskBase.cs
More file actions
119 lines (98 loc) · 3.61 KB
/
TaskBase.cs
File metadata and controls
119 lines (98 loc) · 3.61 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//Microsoft.NET.Build.Extensions.Tasks (net7.0) has nullables disabled
#pragma warning disable IDE0240 // Remove redundant nullable directive
#nullable disable
#pragma warning restore IDE0240 // Remove redundant nullable directive
using System;
using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;
using System.Collections.Generic;
using System.Globalization;
namespace Microsoft.NET.Build.Tasks
{
public abstract class TaskBase : Task
{
private Logger _logger;
internal TaskBase(Logger logger = null)
{
_logger = logger;
}
internal new Logger Log
{
get
{
if (_logger == null)
{
_logger = new LogAdapter(base.Log);
}
return _logger;
}
}
public override bool Execute()
{
try
{
ExecuteCore();
}
catch (BuildErrorException e)
{
Log.LogError(e.Message);
}
catch (Exception e)
{
LogErrorTelemetry("taskBaseCatchException", e);
throw;
}
return !Log.HasLoggedErrors;
}
private void LogErrorTelemetry(string eventName, Exception e)
{
(BuildEngine as IBuildEngine5)?.LogTelemetry(eventName, new Dictionary<string, string> {
{"exceptionType", e.GetType().ToString() },
{"detail", ExceptionToStringWithoutMessage(e) }});
}
private static string ExceptionToStringWithoutMessage(Exception e)
{
const string AggregateException_ToString = "{0}{1}---> (Inner Exception #{2}) {3}{4}{5}";
if (e is AggregateException aggregate)
{
string text = NonAggregateExceptionToStringWithoutMessage(aggregate);
for (int i = 0; i < aggregate.InnerExceptions.Count; i++)
{
text = string.Format(CultureInfo.InvariantCulture,
AggregateException_ToString,
text,
Environment.NewLine,
i,
ExceptionToStringWithoutMessage(aggregate.InnerExceptions[i]),
"<---",
Environment.NewLine);
}
return text;
}
else
{
return NonAggregateExceptionToStringWithoutMessage(e);
}
}
private static string NonAggregateExceptionToStringWithoutMessage(Exception e)
{
string s;
const string Exception_EndOfInnerExceptionStack = "--- End of inner exception stack trace ---";
s = e.GetType().ToString();
if (e.InnerException != null)
{
s = s + " ---> " + ExceptionToStringWithoutMessage(e.InnerException) + Environment.NewLine +
" " + Exception_EndOfInnerExceptionStack;
}
var stackTrace = e.StackTrace;
if (stackTrace != null)
{
s += Environment.NewLine + stackTrace;
}
return s;
}
protected abstract void ExecuteCore();
}
}