1 //------------------------------------------------------------------------------
2 // <copyright file="WebEventCodes.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6 
7 namespace System.Web.Management {
8 
9     using System.Globalization;
10     using System.Collections;
11     using Debug=System.Web.Util.Debug;
12     using System.Security.Permissions;
13 
14     // this class is a container for pre-defined event codes
15     // all APIs will take integers so application defined
16     // codes or new codes added through servicing are supported
17     public sealed class WebEventCodes {
18 
WebEventCodes()19         private WebEventCodes() {
20         }
21 
WebEventCodes()22         static WebEventCodes()
23         {
24             InitEventArrayDimensions();
25         }
26 
27         // (not bit flags)
28         // we're not using an enum for extensibility reasons
29         public const int InvalidEventCode = -1;
30         public const int UndefinedEventCode = 0;
31         public const int UndefinedEventDetailCode = 0;
32 
33         // ----------------------------------
34         // Application Codes
35         // ----------------------------------
36         public const int ApplicationCodeBase = 1000;
37         public const int ApplicationStart = ApplicationCodeBase + 1;
38         public const int ApplicationShutdown = ApplicationCodeBase + 2;
39         public const int ApplicationCompilationStart = ApplicationCodeBase + 3;
40         public const int ApplicationCompilationEnd = ApplicationCodeBase + 4;
41         public const int ApplicationHeartbeat = ApplicationCodeBase + 5;
42 
43         internal const int ApplicationCodeBaseLast = ApplicationCodeBase + 5;
44 
45 
46         // ----------------------------------
47         // Request Codes
48         // ----------------------------------
49         public const int RequestCodeBase = 2000;
50         public const int RequestTransactionComplete = RequestCodeBase+1;
51         public const int RequestTransactionAbort = RequestCodeBase+2;
52 
53         internal const int RequestCodeBaseLast = RequestCodeBase+2;
54 
55 
56         // ----------------------------------
57         // Error Codes
58         // ----------------------------------
59         public const int ErrorCodeBase = 3000;
60 
61         // Errors during request processing related to client input
62         // or behavior
63         public const int RuntimeErrorRequestAbort = ErrorCodeBase + 1;
64         public const int RuntimeErrorViewStateFailure = ErrorCodeBase + 2;
65         public const int RuntimeErrorValidationFailure = ErrorCodeBase + 3;
66         public const int RuntimeErrorPostTooLarge = ErrorCodeBase + 4;
67         public const int RuntimeErrorUnhandledException = ErrorCodeBase + 5;
68 
69         // Errors related to configuration or invalid code
70         public const int WebErrorParserError = ErrorCodeBase + 6;
71         public const int WebErrorCompilationError = ErrorCodeBase + 7;
72         public const int WebErrorConfigurationError = ErrorCodeBase + 8;
73         public const int WebErrorOtherError = ErrorCodeBase + 9;
74         public const int WebErrorPropertyDeserializationError = ErrorCodeBase + 10;
75         public const int WebErrorObjectStateFormatterDeserializationError = ErrorCodeBase + 11;
76 
77         public const int RuntimeErrorWebResourceFailure = ErrorCodeBase + 12;
78 
79         internal const int ErrorCodeBaseLast = ErrorCodeBase + 12;
80 
81 
82         // ----------------------------------
83         // Audit codes
84         // ----------------------------------
85         public const int AuditCodeBase = 4000;
86 
87         // success codes
88         public const int AuditFormsAuthenticationSuccess = AuditCodeBase + 1;
89         public const int AuditMembershipAuthenticationSuccess = AuditCodeBase + 2;
90         public const int AuditUrlAuthorizationSuccess = AuditCodeBase + 3;
91         public const int AuditFileAuthorizationSuccess = AuditCodeBase + 4;
92 
93         // failure codes
94         public const int AuditFormsAuthenticationFailure = AuditCodeBase +5;
95         public const int AuditMembershipAuthenticationFailure = AuditCodeBase + 6;
96         public const int AuditUrlAuthorizationFailure = AuditCodeBase + 7;
97         public const int AuditFileAuthorizationFailure = AuditCodeBase + 8;
98         public const int AuditInvalidViewStateFailure = AuditCodeBase + 9;
99         public const int AuditUnhandledSecurityException = AuditCodeBase + 10;
100         public const int AuditUnhandledAccessException = AuditCodeBase + 11;
101 
102         internal const int AuditCodeBaseLast = AuditCodeBase + 11;
103 
104         // Misc events
105         public const int MiscCodeBase = 6000;
106 
107         public const int WebEventProviderInformation = MiscCodeBase + 1;
108 
109         internal const int MiscCodeBaseLast = MiscCodeBase + 1;
110 
111         // Last code base
112         internal const int LastCodeBase = 6000;
113 
114 
115         /////////////////////////////////////////////////////
116         // Detail Codes
117         /////////////////////////////////////////////////////
118         public const int ApplicationDetailCodeBase = 50000;
119         public const int ApplicationShutdownUnknown = ApplicationDetailCodeBase + 1;
120         public const int ApplicationShutdownHostingEnvironment = ApplicationDetailCodeBase + 2;
121         public const int ApplicationShutdownChangeInGlobalAsax = ApplicationDetailCodeBase + 3;
122         public const int ApplicationShutdownConfigurationChange = ApplicationDetailCodeBase + 4;
123         public const int ApplicationShutdownUnloadAppDomainCalled = ApplicationDetailCodeBase + 5;
124         public const int ApplicationShutdownChangeInSecurityPolicyFile = ApplicationDetailCodeBase + 6;
125         public const int ApplicationShutdownBinDirChangeOrDirectoryRename = ApplicationDetailCodeBase + 7;
126         public const int ApplicationShutdownBrowsersDirChangeOrDirectoryRename = ApplicationDetailCodeBase + 8;
127         public const int ApplicationShutdownCodeDirChangeOrDirectoryRename = ApplicationDetailCodeBase + 9;
128         public const int ApplicationShutdownResourcesDirChangeOrDirectoryRename = ApplicationDetailCodeBase + 10;
129         public const int ApplicationShutdownIdleTimeout = ApplicationDetailCodeBase + 11;
130         public const int ApplicationShutdownPhysicalApplicationPathChanged = ApplicationDetailCodeBase + 12;
131         public const int ApplicationShutdownHttpRuntimeClose = ApplicationDetailCodeBase + 13;
132         public const int ApplicationShutdownInitializationError = ApplicationDetailCodeBase + 14;
133         public const int ApplicationShutdownMaxRecompilationsReached = ApplicationDetailCodeBase + 15;
134         public const int StateServerConnectionError = ApplicationDetailCodeBase + 16;
135         public const int ApplicationShutdownBuildManagerChange = ApplicationDetailCodeBase + 17;
136 
137         // Audit detail codes
138         public const int AuditDetailCodeBase = 50200;
139         public const int InvalidTicketFailure = AuditDetailCodeBase + 1;
140         public const int ExpiredTicketFailure = AuditDetailCodeBase + 2;
141         public const int InvalidViewStateMac = AuditDetailCodeBase + 3;
142         public const int InvalidViewState = AuditDetailCodeBase + 4;
143 
144         // Web Event provider detail codes
145         public const int WebEventDetailCodeBase = 50300;
146         public const int SqlProviderEventsDropped = WebEventDetailCodeBase + 1;
147 
148         // Application extensions should start from here
149         public const int WebExtendedBase = 100000;
150 
MessageFromEventCode(int eventCode, int eventDetailCode)151         internal static string MessageFromEventCode(int eventCode, int eventDetailCode) {
152             string  msg = null;
153             string  detailMsg = null;
154 
155             if (eventDetailCode != 0) {
156                 switch(eventDetailCode) {
157                 case ApplicationShutdownUnknown:
158                     detailMsg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_detail_ApplicationShutdownUnknown);
159                     break;
160 
161                 case ApplicationShutdownHostingEnvironment:
162                     detailMsg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_detail_ApplicationShutdownHostingEnvironment);
163                     break;
164 
165                 case ApplicationShutdownChangeInGlobalAsax:
166                     detailMsg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_detail_ApplicationShutdownChangeInGlobalAsax);
167                     break;
168 
169                 case ApplicationShutdownConfigurationChange:
170                     detailMsg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_detail_ApplicationShutdownConfigurationChange);
171                     break;
172 
173                 case ApplicationShutdownUnloadAppDomainCalled:
174                     detailMsg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_detail_ApplicationShutdownUnloadAppDomainCalled);
175                     break;
176 
177                 case ApplicationShutdownChangeInSecurityPolicyFile:
178                     detailMsg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_detail_ApplicationShutdownChangeInSecurityPolicyFile);
179                     break;
180 
181                 case ApplicationShutdownBinDirChangeOrDirectoryRename:
182                     detailMsg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_detail_ApplicationShutdownBinDirChangeOrDirectoryRename);
183                     break;
184 
185                 case ApplicationShutdownBrowsersDirChangeOrDirectoryRename:
186                     detailMsg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_detail_ApplicationShutdownBrowsersDirChangeOrDirectoryRename);
187                     break;
188 
189                 case ApplicationShutdownCodeDirChangeOrDirectoryRename:
190                     detailMsg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_detail_ApplicationShutdownCodeDirChangeOrDirectoryRename);
191                     break;
192 
193                 case ApplicationShutdownResourcesDirChangeOrDirectoryRename:
194                     detailMsg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_detail_ApplicationShutdownResourcesDirChangeOrDirectoryRename);
195                     break;
196 
197                 case ApplicationShutdownIdleTimeout:
198                     detailMsg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_detail_ApplicationShutdownIdleTimeout);
199                     break;
200 
201                 case ApplicationShutdownPhysicalApplicationPathChanged:
202                     detailMsg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_detail_ApplicationShutdownPhysicalApplicationPathChanged);
203                     break;
204 
205                 case ApplicationShutdownHttpRuntimeClose:
206                     detailMsg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_detail_ApplicationShutdownHttpRuntimeClose);
207                     break;
208 
209                 case ApplicationShutdownInitializationError:
210                     detailMsg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_detail_ApplicationShutdownInitializationError);
211                     break;
212 
213                 case ApplicationShutdownMaxRecompilationsReached:
214                     detailMsg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_detail_ApplicationShutdownMaxRecompilationsReached);
215                     break;
216 
217                 case ApplicationShutdownBuildManagerChange:
218                     detailMsg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_detail_ApplicationShutdownBuildManagerChange);
219                     break;
220 
221                 case StateServerConnectionError:
222                     detailMsg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_detail_StateServerConnectionError);
223                     break;
224 
225                 case InvalidTicketFailure:
226                     detailMsg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_detail_InvalidTicketFailure);
227                     break;
228 
229                 case ExpiredTicketFailure:
230                     detailMsg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_detail_ExpiredTicketFailure);
231                     break;
232 
233                 case InvalidViewStateMac:
234                     detailMsg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_detail_InvalidViewStateMac);
235                     break;
236 
237                 case InvalidViewState:
238                     detailMsg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_detail_InvalidViewState);
239                     break;
240 
241                 case SqlProviderEventsDropped:
242                     detailMsg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_detail_SqlProviderEventsDropped);
243                     break;
244 
245                 default:
246                     break;
247                 }
248             }
249 
250             switch(eventCode) {
251             case ApplicationStart:
252                 msg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_msg_ApplicationStart);
253                 break;
254 
255             case ApplicationShutdown:
256                 msg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_msg_ApplicationShutdown);
257                 break;
258 
259             case ApplicationCompilationStart:
260                 msg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_msg_ApplicationCompilationStart);
261                 break;
262 
263             case ApplicationCompilationEnd:
264                 msg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_msg_ApplicationCompilationEnd);
265                 break;
266 
267             case ApplicationHeartbeat:
268                 msg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_msg_ApplicationHeartbeat);
269                 break;
270 
271             case RequestTransactionComplete:
272                 msg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_msg_RequestTransactionComplete);
273                 break;
274 
275             case RequestTransactionAbort:
276                 msg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_msg_RequestTransactionAbort);
277                 break;
278 
279             case RuntimeErrorRequestAbort:
280                 msg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_msg_RuntimeErrorRequestAbort);
281                 break;
282 
283             case RuntimeErrorViewStateFailure:
284                 msg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_msg_RuntimeErrorViewStateFailure);
285                 break;
286 
287             case RuntimeErrorValidationFailure:
288                 msg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_msg_RuntimeErrorValidationFailure);
289                 break;
290 
291             case RuntimeErrorPostTooLarge:
292                 msg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_msg_RuntimeErrorPostTooLarge);
293                 break;
294 
295             case RuntimeErrorUnhandledException:
296                 msg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_msg_RuntimeErrorUnhandledException);
297                 break;
298 
299             case WebErrorParserError:
300                 msg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_msg_WebErrorParserError);
301                 break;
302 
303             case WebErrorCompilationError:
304                 msg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_msg_WebErrorCompilationError);
305                 break;
306 
307             case WebErrorConfigurationError:
308                 msg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_msg_WebErrorConfigurationError);
309                 break;
310 
311             case AuditUnhandledSecurityException:
312                 msg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_msg_AuditUnhandledSecurityException);
313                 break;
314 
315             case AuditInvalidViewStateFailure:
316                 msg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_msg_AuditInvalidViewStateFailure);
317                 break;
318 
319             case AuditFormsAuthenticationSuccess:
320                 msg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_msg_AuditFormsAuthenticationSuccess);
321                 break;
322 
323             case AuditUrlAuthorizationSuccess:
324                 msg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_msg_AuditUrlAuthorizationSuccess);
325                 break;
326 
327             case AuditFileAuthorizationFailure:
328                 msg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_msg_AuditFileAuthorizationFailure);
329                 break;
330 
331             case AuditFormsAuthenticationFailure:
332                 msg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_msg_AuditFormsAuthenticationFailure);
333                 break;
334 
335             case AuditFileAuthorizationSuccess:
336                 msg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_msg_AuditFileAuthorizationSuccess);
337                 break;
338 
339             case AuditMembershipAuthenticationSuccess:
340                 msg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_msg_AuditMembershipAuthenticationSuccess);
341                 break;
342 
343             case AuditMembershipAuthenticationFailure:
344                 msg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_msg_AuditMembershipAuthenticationFailure);
345                 break;
346 
347             case AuditUrlAuthorizationFailure:
348                 msg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_msg_AuditUrlAuthorizationFailure);
349                 break;
350 
351             case AuditUnhandledAccessException:
352                 msg = WebBaseEvent.FormatResourceStringWithCache(SR.Webevent_msg_AuditUnhandledAccessException);
353                 break;
354 
355             default:
356                 Debug.Assert(false, "ASP.NET event code " + eventCode.ToString(CultureInfo.InvariantCulture) + " doesn't have message string mapped to it");
357                 return String.Empty;
358             }
359 
360             if (detailMsg != null) {
361                 msg += " " + detailMsg;
362             }
363 
364             return msg;
365         }
366 
367         // Both WebBaseEvents and HealthMonitoringSectionHelper has to store information per {event type, event code}.
368         // But for system event type, eventCode and event type has a N:1 relationship.  Meaning every event
369         // code can be mapped to one and only one event type.  So instead of using {event type, event code} as
370         // the key, we can use just the event code as the key.
371 
372         // The simplest way is to use a hashtable.  But in order to boost performance, we store those
373         // information using an array with event code as the key.  However, because the event code range is not
374         // continuous, and has large gap between categories, instead we use an NxM array, when N is number
375         // of major event code categories (e.g. ApplicationCodeBase and RequestCodeBase), and M is the
376         // max number of per category event code among all the catogories.
377 
378         // WebBaseEvents and HealthMonitoringSectionHelper will each maintain its own NxM arrays, and it
379         // depends on the following functions to calculate the sizes of the array, and to convert an event
380         // code into a (x,y) coordinate.
381 
382         internal static int[] s_eventArrayDimensionSizes = new int[2];
383 
GetEventArrayDimensionSize(int dim)384         internal static int GetEventArrayDimensionSize(int dim) {
385             Debug.Assert(dim == 0 || dim == 1, "dim == 0 || dim == 1");
386 
387             return s_eventArrayDimensionSizes[dim];
388         }
389 
390         // Convert an event code into a (x,y) coordinate.
GetEventArrayIndexsFromEventCode(int eventCode, out int index0, out int index1)391         internal static void GetEventArrayIndexsFromEventCode(int eventCode, out int index0, out int index1) {
392             index0 = eventCode/1000 - 1;
393             index1 = eventCode - (eventCode/1000)*1000 - 1;
394 
395             Debug.Assert(index0 >= 0 && index0 < GetEventArrayDimensionSize(0), "Index0 of system eventCode out of expected range: " + eventCode);
396             Debug.Assert(index1 >= 0 && index1 < GetEventArrayDimensionSize(1), "Index1 of system eventCode out of expected range: " + eventCode);
397         }
398 
InitEventArrayDimensions()399         static void InitEventArrayDimensions()
400         {
401             int sizeOf2ndDim = 0;
402             int size;
403 
404             // Below is the manual way to figure out the size of the 2nd dimension.
405 
406             size = WebEventCodes.ApplicationCodeBaseLast - WebEventCodes.ApplicationCodeBase;
407             if (size > sizeOf2ndDim) {
408                 sizeOf2ndDim = size;
409             }
410 
411             size = WebEventCodes.RequestCodeBaseLast - WebEventCodes.RequestCodeBase;
412             if (size > sizeOf2ndDim) {
413                 sizeOf2ndDim = size;
414             }
415 
416             size = WebEventCodes.ErrorCodeBaseLast - WebEventCodes.ErrorCodeBase;
417             if (size > sizeOf2ndDim) {
418                 sizeOf2ndDim = size;
419             }
420 
421             size = WebEventCodes.AuditCodeBaseLast - WebEventCodes.AuditCodeBase;
422             if (size > sizeOf2ndDim) {
423                 sizeOf2ndDim = size;
424             }
425 
426             size = WebEventCodes.MiscCodeBaseLast - WebEventCodes.MiscCodeBase;
427             if (size > sizeOf2ndDim) {
428                 sizeOf2ndDim = size;
429             }
430 
431             s_eventArrayDimensionSizes[0] = WebEventCodes.LastCodeBase/1000;
432             s_eventArrayDimensionSizes[1] = sizeOf2ndDim;
433         }
434     }
435 }
436 
437