EventLog.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. //
  2. // System.Diagnostics.EventLog.cs
  3. //
  4. // Authors:
  5. // Jonathan Pryor ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) 2002
  9. // (C) 2003 Andreas Nahr
  10. //
  11. using System;
  12. using System.Diagnostics;
  13. using System.ComponentModel;
  14. using System.ComponentModel.Design;
  15. namespace System.Diagnostics
  16. {
  17. [DefaultEvent ("EntryWritten"), InstallerType (typeof (EventLogInstaller))]
  18. [Designer ("Microsoft.VisualStudio.Install.EventLogInstallableComponentDesigner, " + Consts.AssemblyMicrosoft_VisualStudio, typeof (IDesigner))]
  19. public class EventLog : Component, ISupportInitialize
  20. {
  21. private string source;
  22. private string logName;
  23. private string machineName;
  24. private bool doRaiseEvents = false;
  25. private ISynchronizeInvoke synchronizingObject = null;
  26. private EventLogImpl Impl;
  27. public EventLog()
  28. : this ("")
  29. {
  30. }
  31. public EventLog(string logName)
  32. : this (logName, ".")
  33. {
  34. }
  35. public EventLog(string logName, string machineName)
  36. : this (logName, machineName, "")
  37. {
  38. }
  39. public EventLog(string logName, string machineName, string source)
  40. {
  41. this.source = source;
  42. this.machineName = machineName;
  43. this.logName = logName;
  44. this.Impl = new EventLogImpl (this);
  45. EventLogImpl.EntryWritten += new EntryWrittenEventHandler (EntryWrittenHandler);
  46. }
  47. private void EntryWrittenHandler (object sender, EntryWrittenEventArgs e)
  48. {
  49. if (doRaiseEvents)
  50. OnEntryWritten (e.Entry);
  51. }
  52. [Browsable (false), DefaultValue (false)]
  53. [MonitoringDescription ("If enabled raises event when a log is written.")]
  54. public bool EnableRaisingEvents {
  55. get {return doRaiseEvents;}
  56. set {doRaiseEvents = value;}
  57. }
  58. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  59. [MonitoringDescription ("The entries in the log.")]
  60. public EventLogEntryCollection Entries {
  61. get {return Impl.Entries;}
  62. }
  63. [ReadOnly (true), DefaultValue (""), RecommendedAsConfigurable (true)]
  64. [TypeConverter ("System.Diagnostics.Design.LogConverter, " + Consts.AssemblySystem_Design)]
  65. [MonitoringDescription ("Name of the log that is read and written.")]
  66. public string Log {
  67. get {return logName;}
  68. set {logName = value;}
  69. }
  70. [Browsable (false)]
  71. public string LogDisplayName {
  72. get {return Impl.LogDisplayName;}
  73. }
  74. [ReadOnly (true), DefaultValue ("."), RecommendedAsConfigurable (true)]
  75. [MonitoringDescription ("Name of the machine that this log get written to.")]
  76. public string MachineName {
  77. get {return machineName;}
  78. set {machineName = value;}
  79. }
  80. [ReadOnly (true), DefaultValue (""), RecommendedAsConfigurable (true)]
  81. [TypeConverter ("System.Diagnostics.Design.StringValueConverter, " + Consts.AssemblySystem_Design)]
  82. [MonitoringDescription ("The application name that writes the log.")]
  83. public string Source {
  84. get {return source;}
  85. set {source = value;}
  86. }
  87. [Browsable (false), DefaultValue (null)]
  88. [MonitoringDescription ("An object that synchronizes event handler calls.")]
  89. public ISynchronizeInvoke SynchronizingObject {
  90. get {return synchronizingObject;}
  91. set {synchronizingObject = value;}
  92. }
  93. public void BeginInit()
  94. {
  95. Impl.BeginInit();
  96. }
  97. public void Clear()
  98. {
  99. Impl.Clear();
  100. }
  101. public void Close()
  102. {
  103. Impl.Close();
  104. }
  105. public static void CreateEventSource(string source, string logName)
  106. {
  107. CreateEventSource (source, logName, ".");
  108. }
  109. public static void CreateEventSource(string source,
  110. string logName,
  111. string machineName)
  112. {
  113. EventLogImpl.CreateEventSource (source, logName, machineName);
  114. }
  115. public static void Delete(string logName)
  116. {
  117. Delete (logName, ".");
  118. }
  119. public static void Delete(string logName, string machineName)
  120. {
  121. EventLogImpl.Delete (logName, machineName);
  122. }
  123. public static void DeleteEventSource(string source)
  124. {
  125. DeleteEventSource (source, ".");
  126. }
  127. public static void DeleteEventSource(string source,
  128. string machineName)
  129. {
  130. EventLogImpl.DeleteEventSource (source, machineName);
  131. }
  132. protected override void Dispose(bool disposing)
  133. {
  134. Impl.Dispose (disposing);
  135. }
  136. public void EndInit()
  137. {
  138. Impl.EndInit();
  139. }
  140. public static bool Exists(string logName)
  141. {
  142. return Exists (logName, ".");
  143. }
  144. public static bool Exists(string logName, string machineName)
  145. {
  146. return EventLogImpl.Exists (logName, machineName);
  147. }
  148. public static EventLog[] GetEventLogs()
  149. {
  150. return GetEventLogs (".");
  151. }
  152. public static EventLog[] GetEventLogs(string machineName)
  153. {
  154. return EventLogImpl.GetEventLogs (machineName);
  155. }
  156. public static string LogNameFromSourceName(string source,
  157. string machineName)
  158. {
  159. return EventLogImpl.LogNameFromSourceName (source, machineName);
  160. }
  161. public static bool SourceExists(string source)
  162. {
  163. return SourceExists (source, ".");
  164. }
  165. public static bool SourceExists(string source, string machineName)
  166. {
  167. return EventLogImpl.SourceExists (source, machineName);
  168. }
  169. public void WriteEntry(string message)
  170. {
  171. WriteEntry (message, EventLogEntryType.Information);
  172. }
  173. public void WriteEntry(string message, EventLogEntryType type)
  174. {
  175. WriteEntry (message, type, 0);
  176. }
  177. public void WriteEntry(string message, EventLogEntryType type,
  178. int eventID)
  179. {
  180. WriteEntry (message, type, eventID, 0);
  181. }
  182. public void WriteEntry(string message, EventLogEntryType type,
  183. int eventID,
  184. short category)
  185. {
  186. WriteEntry (message, type, eventID, category, null);
  187. }
  188. public void WriteEntry(string message, EventLogEntryType type,
  189. int eventID,
  190. short category, byte[] rawData)
  191. {
  192. Impl.WriteEntry (message, type, eventID, category, rawData);
  193. }
  194. public static void WriteEntry(string source, string message)
  195. {
  196. WriteEntry (source, message, EventLogEntryType.Information);
  197. }
  198. public static void WriteEntry(string source, string message,
  199. EventLogEntryType type)
  200. {
  201. WriteEntry (source, message, EventLogEntryType.Information, 0);
  202. }
  203. public static void WriteEntry(string source, string message,
  204. EventLogEntryType type, int eventID)
  205. {
  206. WriteEntry (source, message, EventLogEntryType.Information, eventID, 0);
  207. }
  208. public static void WriteEntry(string source, string message,
  209. EventLogEntryType type, int eventID, short category)
  210. {
  211. WriteEntry (source, message, EventLogEntryType.Information, eventID, category, null);
  212. }
  213. public static void WriteEntry(string source, string message,
  214. EventLogEntryType type, int eventID, short category,
  215. byte[] rawData)
  216. {
  217. EventLogImpl.WriteEntry (source, message, type, eventID, category, rawData);
  218. }
  219. internal void OnEntryWritten (EventLogEntry newEntry)
  220. {
  221. if (EntryWritten != null)
  222. EntryWritten (this, new EntryWrittenEventArgs (newEntry));
  223. }
  224. [MonitoringDescription ("Raised for each EventLog entry written.")]
  225. public event EntryWrittenEventHandler EntryWritten;
  226. }
  227. }