/
LoggingContext.cs
265 lines (240 loc) · 11.8 KB
/
LoggingContext.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
using System;
using System.Diagnostics;
using Microsoft.Build.Exceptions;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;
namespace Microsoft.Build.BackEnd.Logging
{
/// <summary>
/// This object encapsulates the logging service plus the current BuildEventContext and
/// hides the requirement to pass BuildEventContexts to the logging service or query the
/// host for the logging service all of the time.
/// </summary>
internal class LoggingContext
{
/// <summary>
/// The logging service to which this context is attached
/// </summary>
private readonly ILoggingService _loggingService;
/// <summary>
/// The build event context understood by the logging service.
/// </summary>
private BuildEventContext _eventContext;
/// <summary>
/// True if this context is still valid (i.e. hasn't been "finished")
/// </summary>
private bool _isValid;
protected bool _hasLoggedErrors;
/// <summary>
/// Constructs the logging context from a logging service and an event context.
/// </summary>
/// <param name="loggingService">The logging service to use</param>
/// <param name="eventContext">The event context</param>
public LoggingContext(ILoggingService loggingService, BuildEventContext eventContext)
{
ErrorUtilities.VerifyThrowArgumentNull(loggingService, nameof(loggingService));
ErrorUtilities.VerifyThrowArgumentNull(eventContext, nameof(eventContext));
_loggingService = loggingService;
_eventContext = eventContext;
_isValid = false;
_hasLoggedErrors = false;
}
/// <summary>
/// Constructs a logging context from another logging context. This is used primarily in
/// the constructors for other logging contexts to populate the logging service parameter,
/// while the event context will come from a call into the logging service itself.
/// </summary>
/// <param name="baseContext">The context from which this context is being created.</param>
public LoggingContext(LoggingContext baseContext)
{
_loggingService = baseContext._loggingService;
_eventContext = null;
_isValid = baseContext._isValid;
}
/// <summary>
/// Retrieves the logging service
/// </summary>
public ILoggingService LoggingService
{
[DebuggerStepThrough]
get
{ return _loggingService; }
}
/// <summary>
/// Retrieves the build event context
/// UNDONE: (Refactor) We eventually want to remove this because all logging should go
/// through a context object. This exists only so we can make certain
/// logging calls in code which has not yet been fully refactored.
/// </summary>
public BuildEventContext BuildEventContext
{
[DebuggerStepThrough]
get
{
return _eventContext;
}
protected set
{
ErrorUtilities.VerifyThrow(_eventContext == null, "eventContext should be null");
_eventContext = value;
}
}
/// <summary>
/// Returns true if the context is still valid, false if the
/// appropriate 'Finished' call has been invoked.
/// </summary>
public bool IsValid
{
[DebuggerStepThrough]
get
{
return _isValid;
}
[DebuggerStepThrough]
protected set
{
_isValid = value;
}
}
internal bool HasLoggedErrors { get { return _hasLoggedErrors; } set { _hasLoggedErrors = value; } }
/// <summary>
/// Helper method to create a message build event from a string resource and some parameters
/// </summary>
/// <param name="importance">Importance level of the message</param>
/// <param name="messageResourceName">string within the resource which indicates the format string to use</param>
/// <param name="messageArgs">string resource arguments</param>
internal void LogComment(MessageImportance importance, string messageResourceName, params object[] messageArgs)
{
ErrorUtilities.VerifyThrow(_isValid, "must be valid");
_loggingService.LogComment(_eventContext, importance, messageResourceName, messageArgs);
}
/// <summary>
/// Helper method to create a message build event from a string
/// </summary>
/// <param name="importance">Importance level of the message</param>
/// <param name="message">message to log</param>
internal void LogCommentFromText(MessageImportance importance, string message)
{
ErrorUtilities.VerifyThrow(_isValid, "must be valid");
_loggingService.LogCommentFromText(_eventContext, importance, message);
}
/// <summary>
/// Helper method to create a message build event from a string
/// </summary>
/// <param name="importance">Importance level of the message</param>
/// <param name="message">Message to log</param>
/// <param name="messageArgs">Format string arguments</param>
internal void LogCommentFromText(MessageImportance importance, string message, params object[] messageArgs)
{
ErrorUtilities.VerifyThrow(_isValid, "must be valid");
_loggingService.LogCommentFromText(_eventContext, importance, message, messageArgs);
}
/// <summary>
/// Log an error
/// </summary>
/// <param name="file">The file in which the error occurred</param>
/// <param name="messageResourceName">The resource name for the error</param>
/// <param name="messageArgs">Parameters for the resource string</param>
internal void LogError(BuildEventFileInfo file, string messageResourceName, params object[] messageArgs)
{
ErrorUtilities.VerifyThrow(_isValid, "must be valid");
_loggingService.LogError(_eventContext, file, messageResourceName, messageArgs);
_hasLoggedErrors = true;
}
/// <summary>
/// Log an error
/// </summary>
/// <param name="subcategoryResourceName">The resource name which indicates the subCategory</param>
/// <param name="file">The file in which the error occurred</param>
/// <param name="messageResourceName">The resource name for the error</param>
/// <param name="messageArgs">Parameters for the resource string</param>
internal void LogErrorWithSubcategory(string subcategoryResourceName, BuildEventFileInfo file, string messageResourceName, params object[] messageArgs)
{
ErrorUtilities.VerifyThrow(_isValid, "must be valid");
_loggingService.LogError(_eventContext, subcategoryResourceName, file, messageResourceName, messageArgs);
_hasLoggedErrors = true;
}
/// <summary>
/// Log an error
/// </summary>
/// <param name="subcategoryResourceName">The resource name which indicates the subCategory</param>
/// <param name="errorCode"> Error code</param>
/// <param name="helpKeyword">Help keyword</param>
/// <param name="file">The file in which the error occurred</param>
/// <param name="message">Error message</param>
internal void LogErrorFromText(string subcategoryResourceName, string errorCode, string helpKeyword, BuildEventFileInfo file, string message)
{
ErrorUtilities.VerifyThrow(_isValid, "must be valid");
_loggingService.LogErrorFromText(_eventContext, subcategoryResourceName, errorCode, helpKeyword, file, message);
_hasLoggedErrors = true;
}
/// <summary>
/// Log an invalid project file exception
/// </summary>
/// <param name="invalidProjectFileException">The invalid Project File Exception which is to be logged</param>
internal void LogInvalidProjectFileError(InvalidProjectFileException invalidProjectFileException)
{
ErrorUtilities.VerifyThrow(_isValid, "must be valid");
_loggingService.LogInvalidProjectFileError(_eventContext, invalidProjectFileException);
_hasLoggedErrors = true;
}
/// <summary>
/// Log an error based on an exception
/// </summary>
/// <param name="exception">The exception wich is to be logged</param>
/// <param name="file">The file in which the error occurred</param>
/// <param name="messageResourceName">The string resource which has the formatting string for the error</param>
/// <param name="messageArgs">The arguments for the error message</param>
internal void LogFatalError(Exception exception, BuildEventFileInfo file, string messageResourceName, params object[] messageArgs)
{
ErrorUtilities.VerifyThrow(_isValid, "must be valid");
_loggingService.LogFatalError(_eventContext, exception, file, messageResourceName, messageArgs);
_hasLoggedErrors = true;
}
/// <summary>
/// Log a warning
/// </summary>
/// <param name="subcategoryResourceName">The subcategory resource name</param>
/// <param name="file">The file in which the warning occurred</param>
/// <param name="messageResourceName">The string resource which contains the formatted warning string</param>
/// <param name="messageArgs">parameters for the string resource</param>
internal void LogWarning(string subcategoryResourceName, BuildEventFileInfo file, string messageResourceName, params object[] messageArgs)
{
ErrorUtilities.VerifyThrow(_isValid, "must be valid");
_loggingService.LogWarning(_eventContext, subcategoryResourceName, file, messageResourceName, messageArgs);
}
/// <summary>
/// Log a warning based on a text message
/// </summary>
/// <param name="subcategoryResourceName">The subcategory resource name</param>
/// <param name="warningCode"> Warning code</param>
/// <param name="helpKeyword"> Help keyword</param>
/// <param name="file">The file in which the warning occurred</param>
/// <param name="message">The message to be logged as a warning</param>
internal void LogWarningFromText(string subcategoryResourceName, string warningCode, string helpKeyword, BuildEventFileInfo file, string message)
{
ErrorUtilities.VerifyThrow(_isValid, "must be valid");
_loggingService.LogWarningFromText(_eventContext, subcategoryResourceName, warningCode, helpKeyword, file, message);
}
/// <summary>
/// Will Log a build Event. Will also take into account OnlyLogCriticalEvents when determining if to drop the event or to log it.
/// </summary>
/// <param name="buildEvent">The event to log</param>
internal void LogBuildEvent(BuildEventArgs buildEvent)
{
ErrorUtilities.VerifyThrow(IsValid, "must be valid");
LoggingService.LogBuildEvent(buildEvent);
}
/// <summary>
/// Log an error based on an exception
/// </summary>
/// <param name="exception">The exception to be logged</param>
/// <param name="file">The file in which the error occurred</param>
internal void LogFatalBuildError(Exception exception, BuildEventFileInfo file)
{
ErrorUtilities.VerifyThrow(IsValid, "must be valid");
LoggingService.LogFatalBuildError(BuildEventContext, exception, file);
_hasLoggedErrors = true;
}
}
}