mscorlib(4.0.0.0) API with additions
EventLogEntry.cs
1 using Microsoft.Win32;
6 using System.Text;
7 
8 namespace System.Diagnostics
9 {
11  [Serializable]
12  [ToolboxItem(false)]
13  [DesignTimeVisible(false)]
14  public sealed class EventLogEntry : Component, ISerializable
15  {
16  private static class FieldOffsets
17  {
18  internal const int LENGTH = 0;
19 
20  internal const int RESERVED = 4;
21 
22  internal const int RECORDNUMBER = 8;
23 
24  internal const int TIMEGENERATED = 12;
25 
26  internal const int TIMEWRITTEN = 16;
27 
28  internal const int EVENTID = 20;
29 
30  internal const int EVENTTYPE = 24;
31 
32  internal const int NUMSTRINGS = 26;
33 
34  internal const int EVENTCATEGORY = 28;
35 
36  internal const int RESERVEDFLAGS = 30;
37 
38  internal const int CLOSINGRECORDNUMBER = 32;
39 
40  internal const int STRINGOFFSET = 36;
41 
42  internal const int USERSIDLENGTH = 40;
43 
44  internal const int USERSIDOFFSET = 44;
45 
46  internal const int DATALENGTH = 48;
47 
48  internal const int DATAOFFSET = 52;
49 
50  internal const int RAWDATA = 56;
51  }
52 
53  internal byte[] dataBuf;
54 
55  internal int bufOffset;
56 
57  private EventLogInternal owner;
58 
59  private string category;
60 
61  private string message;
62 
63  private static readonly DateTime beginningOfTime = new DateTime(1970, 1, 1, 0, 0, 0);
64 
65  private const int OFFSETFIXUP = 56;
66 
69  [MonitoringDescription("LogEntryMachineName")]
70  public string MachineName
71  {
72  get
73  {
74  int i;
75  for (i = bufOffset + 56; CharFrom(dataBuf, i) != 0; i += 2)
76  {
77  }
78  i += 2;
79  char c = CharFrom(dataBuf, i);
80  StringBuilder stringBuilder = new StringBuilder();
81  while (c != 0)
82  {
83  stringBuilder.Append(c);
84  i += 2;
85  c = CharFrom(dataBuf, i);
86  }
87  return stringBuilder.ToString();
88  }
89  }
90 
93  [MonitoringDescription("LogEntryData")]
94  public byte[] Data
95  {
96  get
97  {
98  int num = IntFrom(dataBuf, bufOffset + 48);
99  byte[] array = new byte[num];
100  Array.Copy(dataBuf, bufOffset + IntFrom(dataBuf, bufOffset + 52), array, 0, num);
101  return array;
102  }
103  }
104 
107  [MonitoringDescription("LogEntryIndex")]
108  public int Index
109  {
110  get
111  {
112  return IntFrom(dataBuf, bufOffset + 8);
113  }
114  }
115 
119  [MonitoringDescription("LogEntryCategory")]
120  public string Category
121  {
122  get
123  {
124  if (category == null)
125  {
126  string messageLibraryNames = GetMessageLibraryNames("CategoryMessageFile");
127  string text = owner.FormatMessageWrapper(messageLibraryNames, (uint)CategoryNumber, null);
128  if (text == null)
129  {
130  category = "(" + CategoryNumber.ToString(CultureInfo.CurrentCulture) + ")";
131  }
132  else
133  {
134  category = text;
135  }
136  }
137  return category;
138  }
139  }
140 
143  [MonitoringDescription("LogEntryCategoryNumber")]
144  public short CategoryNumber
145  {
146  get
147  {
148  return ShortFrom(dataBuf, bufOffset + 28);
149  }
150  }
151 
154  [MonitoringDescription("LogEntryEventID")]
155  [Obsolete("This property has been deprecated. Please use System.Diagnostics.EventLogEntry.InstanceId instead. http://go.microsoft.com/fwlink/?linkid=14202")]
156  public int EventID
157  {
158  get
159  {
160  return IntFrom(dataBuf, bufOffset + 20) & 0x3FFFFFFF;
161  }
162  }
163 
166  [MonitoringDescription("LogEntryEntryType")]
168  {
169  get
170  {
171  return (EventLogEntryType)ShortFrom(dataBuf, bufOffset + 24);
172  }
173  }
174 
178  [MonitoringDescription("LogEntryMessage")]
179  [Editor("System.ComponentModel.Design.BinaryEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
180  public string Message
181  {
182  get
183  {
184  if (message == null)
185  {
186  string messageLibraryNames = GetMessageLibraryNames("EventMessageFile");
187  int num = IntFrom(dataBuf, bufOffset + 20);
188  string text = owner.FormatMessageWrapper(messageLibraryNames, (uint)num, ReplacementStrings);
189  if (text == null)
190  {
191  StringBuilder stringBuilder = new StringBuilder(SR.GetString("MessageNotFormatted", num, Source));
192  string[] replacementStrings = ReplacementStrings;
193  for (int i = 0; i < replacementStrings.Length; i++)
194  {
195  if (i != 0)
196  {
197  stringBuilder.Append(", ");
198  }
199  stringBuilder.Append("'");
200  stringBuilder.Append(replacementStrings[i]);
201  stringBuilder.Append("'");
202  }
203  text = stringBuilder.ToString();
204  }
205  else
206  {
207  text = ReplaceMessageParameters(text, ReplacementStrings);
208  }
209  message = text;
210  }
211  return message;
212  }
213  }
214 
217  [MonitoringDescription("LogEntrySource")]
218  public string Source
219  {
220  get
221  {
222  StringBuilder stringBuilder = new StringBuilder();
223  int num = bufOffset + 56;
224  for (char c = CharFrom(dataBuf, num); c != 0; c = CharFrom(dataBuf, num))
225  {
226  stringBuilder.Append(c);
227  num += 2;
228  }
229  return stringBuilder.ToString();
230  }
231  }
232 
235  [MonitoringDescription("LogEntryReplacementStrings")]
236  public string[] ReplacementStrings
237  {
238  get
239  {
240  string[] array = new string[ShortFrom(dataBuf, bufOffset + 26)];
241  int num = 0;
242  int num2 = bufOffset + IntFrom(dataBuf, bufOffset + 36);
243  StringBuilder stringBuilder = new StringBuilder();
244  while (num < array.Length)
245  {
246  char c = CharFrom(dataBuf, num2);
247  if (c != 0)
248  {
249  stringBuilder.Append(c);
250  }
251  else
252  {
253  array[num] = stringBuilder.ToString();
254  num++;
255  stringBuilder = new StringBuilder();
256  }
257  num2 += 2;
258  }
259  return array;
260  }
261  }
262 
265  [MonitoringDescription("LogEntryResourceId")]
266  [ComVisible(false)]
267  public long InstanceId
268  {
269  get
270  {
271  return (uint)IntFrom(dataBuf, bufOffset + 20);
272  }
273  }
274 
277  [MonitoringDescription("LogEntryTimeGenerated")]
278  public DateTime TimeGenerated
279  {
280  get
281  {
282  return beginningOfTime.AddSeconds(IntFrom(dataBuf, bufOffset + 12)).ToLocalTime();
283  }
284  }
285 
288  [MonitoringDescription("LogEntryTimeWritten")]
289  public DateTime TimeWritten
290  {
291  get
292  {
293  return beginningOfTime.AddSeconds(IntFrom(dataBuf, bufOffset + 16)).ToLocalTime();
294  }
295  }
296 
300  [MonitoringDescription("LogEntryUserName")]
301  public string UserName
302  {
303  get
304  {
305  int num = IntFrom(dataBuf, bufOffset + 40);
306  if (num == 0)
307  {
308  return null;
309  }
310  byte[] array = new byte[num];
311  Array.Copy(dataBuf, bufOffset + IntFrom(dataBuf, bufOffset + 44), array, 0, array.Length);
312  int userNameSize = 256;
313  int domainNameSize = 256;
314  int eUse = 0;
315  StringBuilder stringBuilder = new StringBuilder(userNameSize);
316  StringBuilder stringBuilder2 = new StringBuilder(domainNameSize);
317  StringBuilder stringBuilder3 = new StringBuilder();
318  if (Microsoft.Win32.UnsafeNativeMethods.LookupAccountSid(MachineName, array, stringBuilder, ref userNameSize, stringBuilder2, ref domainNameSize, ref eUse) != 0)
319  {
320  stringBuilder3.Append(stringBuilder2.ToString());
321  stringBuilder3.Append("\\");
322  stringBuilder3.Append(stringBuilder.ToString());
323  }
324  return stringBuilder3.ToString();
325  }
326  }
327 
328  internal EventLogEntry(byte[] buf, int offset, EventLogInternal log)
329  {
330  dataBuf = buf;
331  bufOffset = offset;
332  owner = log;
333  GC.SuppressFinalize(this);
334  }
335 
336  private EventLogEntry(SerializationInfo info, StreamingContext context)
337  {
338  dataBuf = (byte[])info.GetValue("DataBuffer", typeof(byte[]));
339  string @string = info.GetString("LogName");
340  string string2 = info.GetString("MachineName");
341  owner = new EventLogInternal(@string, string2, "");
342  GC.SuppressFinalize(this);
343  }
344 
345  private char CharFrom(byte[] buf, int offset)
346  {
347  return (char)ShortFrom(buf, offset);
348  }
349 
354  public bool Equals(EventLogEntry otherEntry)
355  {
356  if (otherEntry == null)
357  {
358  return false;
359  }
360  int num = IntFrom(dataBuf, bufOffset + 0);
361  int num2 = IntFrom(otherEntry.dataBuf, otherEntry.bufOffset + 0);
362  if (num != num2)
363  {
364  return false;
365  }
366  int num3 = bufOffset;
367  int num4 = bufOffset + num;
368  int num5 = otherEntry.bufOffset;
369  int num6 = num3;
370  while (num6 < num4)
371  {
372  if (dataBuf[num6] != otherEntry.dataBuf[num5])
373  {
374  return false;
375  }
376  num6++;
377  num5++;
378  }
379  return true;
380  }
381 
382  private int IntFrom(byte[] buf, int offset)
383  {
384  return (-16777216 & (buf[offset + 3] << 24)) | (0xFF0000 & (buf[offset + 2] << 16)) | (0xFF00 & (buf[offset + 1] << 8)) | (0xFF & buf[offset]);
385  }
386 
387  internal string ReplaceMessageParameters(string msg, string[] insertionStrings)
388  {
389  int num = msg.IndexOf('%');
390  if (num < 0)
391  {
392  return msg;
393  }
394  int num2 = 0;
395  int length = msg.Length;
396  StringBuilder stringBuilder = new StringBuilder();
397  string messageLibraryNames = GetMessageLibraryNames("ParameterMessageFile");
398  while (num >= 0)
399  {
400  string text = null;
401  int i;
402  for (i = num + 1; i < length && char.IsDigit(msg, i); i++)
403  {
404  }
405  uint result = 0u;
406  if (i != num + 1)
407  {
408  uint.TryParse(msg.Substring(num + 1, i - num - 1), out result);
409  }
410  if (result != 0)
411  {
412  text = owner.FormatMessageWrapper(messageLibraryNames, result, insertionStrings);
413  }
414  if (text != null)
415  {
416  if (num > num2)
417  {
418  stringBuilder.Append(msg, num2, num - num2);
419  }
420  stringBuilder.Append(text);
421  num2 = i;
422  }
423  num = msg.IndexOf('%', num + 1);
424  }
425  if (length - num2 > 0)
426  {
427  stringBuilder.Append(msg, num2, length - num2);
428  }
429  return stringBuilder.ToString();
430  }
431 
432  private static RegistryKey GetSourceRegKey(string logName, string source, string machineName)
433  {
434  RegistryKey registryKey = null;
435  RegistryKey registryKey2 = null;
436  try
437  {
438  registryKey = EventLog.GetEventLogRegKey(machineName, writable: false);
439  if (registryKey == null)
440  {
441  return null;
442  }
443  registryKey2 = ((logName != null) ? registryKey.OpenSubKey(logName, writable: false) : registryKey.OpenSubKey("Application", writable: false));
444  return registryKey2?.OpenSubKey(source, writable: false);
445  }
446  finally
447  {
448  registryKey?.Close();
449  registryKey2?.Close();
450  }
451  }
452 
453  private string GetMessageLibraryNames(string libRegKey)
454  {
455  string text = null;
456  RegistryKey registryKey = null;
457  try
458  {
459  registryKey = GetSourceRegKey(owner.Log, Source, owner.MachineName);
460  if (registryKey != null)
461  {
462  text = (string)registryKey.GetValue(libRegKey);
463  }
464  }
465  finally
466  {
467  registryKey?.Close();
468  }
469  if (text == null)
470  {
471  return null;
472  }
473  if (owner.MachineName != ".")
474  {
475  string[] array = text.Split(';');
476  StringBuilder stringBuilder = new StringBuilder();
477  for (int i = 0; i < array.Length; i++)
478  {
479  if (array[i].Length >= 2 && array[i][1] == ':')
480  {
481  stringBuilder.Append("\\\\");
482  stringBuilder.Append(owner.MachineName);
483  stringBuilder.Append("\\");
484  stringBuilder.Append(array[i][0]);
485  stringBuilder.Append("$");
486  stringBuilder.Append(array[i], 2, array[i].Length - 2);
487  stringBuilder.Append(';');
488  }
489  }
490  if (stringBuilder.Length == 0)
491  {
492  return null;
493  }
494  return stringBuilder.ToString(0, stringBuilder.Length - 1);
495  }
496  return text;
497  }
498 
499  private short ShortFrom(byte[] buf, int offset)
500  {
501  return (short)((0xFF00 & (buf[offset + 1] << 8)) | (0xFF & buf[offset]));
502  }
503 
508  {
509  int num = IntFrom(dataBuf, bufOffset + 0);
510  byte[] array = new byte[num];
511  Array.Copy(dataBuf, bufOffset, array, 0, num);
512  info.AddValue("DataBuffer", array, typeof(byte[]));
513  info.AddValue("LogName", owner.Log);
514  info.AddValue("MachineName", owner.MachineName);
515  }
516  }
517 }
string MachineName
Gets the name of the computer on which this entry was generated.
unsafe override string ToString()
Converts the value of this instance to a T:System.String.
static void SuppressFinalize(object obj)
Requests that the common language runtime not call the finalizer for the specified object.
Definition: GC.cs:308
EventLogEntryType
Specifies the event type of an event log entry.
Definition: __Canon.cs:3
long InstanceId
Gets the resource identifier that designates the message text of the event entry.
Represents an instant in time, typically expressed as a date and time of day. To browse the ....
Definition: DateTime.cs:13
Describes the source and destination of a given serialized stream, and provides an additional caller-...
Encapsulates a single record in the event log. This class cannot be inherited.
string Source
Gets the name of the application that generated this event.
DateTime ToLocalTime()
Converts the value of the current T:System.DateTime object to local time.
Definition: DateTime.cs:1460
DateTime TimeGenerated
Gets the local time at which this event was generated.
StringBuilder Append(char value, int repeatCount)
Appends a specified number of copies of the string representation of a Unicode character to this inst...
EventLogEntryType EntryType
Gets the event type of this entry.
DateTime TimeWritten
Gets the local time at which this event was written to the log.
string Message
Gets the localized message associated with this event entry.
int Length
Gets or sets the length of the current T:System.Text.StringBuilder object.
Provides the base implementation for the T:System.ComponentModel.IComponent interface and enables obj...
Definition: Component.cs:9
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition: Array.cs:17
string [] ReplacementStrings
Gets the replacement strings associated with the event log entry.
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
Represents a mutable string of characters. This class cannot be inherited.To browse the ....
Controls the system garbage collector, a service that automatically reclaims unused memory.
Definition: GC.cs:11
static CultureInfo CurrentCulture
Gets or sets the T:System.Globalization.CultureInfo object that represents the culture used by the cu...
Definition: CultureInfo.cs:120
short CategoryNumber
Gets the category number of the event log entry.
static void Copy(Array sourceArray, Array destinationArray, int length)
Copies a range of elements from an T:System.Array starting at the first element and pastes them into ...
Definition: Array.cs:1275
string Category
Gets the text associated with the P:System.Diagnostics.EventLogEntry.CategoryNumber property for this...
Allows an object to control its own serialization and deserialization.
Definition: ISerializable.cs:8
string UserName
Gets the name of the user who is responsible for this event.
int EventID
Gets the application-specific event identifier for the current event entry.
int Index
Gets the index of this entry in the event log.
Specifies that the class can be serialized.
byte [] Data
Gets the binary data associated with the entry.
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
DateTime AddSeconds(double value)
Returns a new T:System.DateTime that adds the specified number of seconds to the value of this instan...
Definition: DateTime.cs:816
void GetObjectData(SerializationInfo info, StreamingContext context)
Populates a T:System.Runtime.Serialization.SerializationInfo with the data needed to serialize the ta...
bool Equals(EventLogEntry otherEntry)
Performs a comparison between two event log entries.