EventLog.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Diagnostics;
  33. using System.ComponentModel;
  34. using System.ComponentModel.Design;
  35. namespace System.Diagnostics
  36. {
  37. [DefaultEvent ("EntryWritten"), InstallerType (typeof (EventLogInstaller))]
  38. [Designer ("Microsoft.VisualStudio.Install.EventLogInstallableComponentDesigner, " + Consts.AssemblyMicrosoft_VisualStudio, typeof (IDesigner))]
  39. public class EventLog : Component, ISupportInitialize
  40. {
  41. private string source;
  42. private string logName;
  43. private string machineName;
  44. private bool doRaiseEvents = false;
  45. private ISynchronizeInvoke synchronizingObject = null;
  46. private EventLogImpl Impl;
  47. public EventLog()
  48. : this ("")
  49. {
  50. }
  51. public EventLog(string logName)
  52. : this (logName, ".")
  53. {
  54. }
  55. public EventLog(string logName, string machineName)
  56. : this (logName, machineName, "")
  57. {
  58. }
  59. public EventLog(string logName, string machineName, string source)
  60. {
  61. this.source = source;
  62. this.machineName = machineName;
  63. this.logName = logName;
  64. this.Impl = new EventLogImpl (this);
  65. EventLogImpl.EntryWritten += new EntryWrittenEventHandler (EntryWrittenHandler);
  66. }
  67. private void EntryWrittenHandler (object sender, EntryWrittenEventArgs e)
  68. {
  69. if (doRaiseEvents)
  70. OnEntryWritten (e.Entry);
  71. }
  72. [Browsable (false), DefaultValue (false)]
  73. [MonitoringDescription ("If enabled raises event when a log is written.")]
  74. public bool EnableRaisingEvents {
  75. get {return doRaiseEvents;}
  76. set {doRaiseEvents = value;}
  77. }
  78. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  79. [MonitoringDescription ("The entries in the log.")]
  80. public EventLogEntryCollection Entries {
  81. get {return Impl.Entries;}
  82. }
  83. [ReadOnly (true), DefaultValue (""), RecommendedAsConfigurable (true)]
  84. [TypeConverter ("System.Diagnostics.Design.LogConverter, " + Consts.AssemblySystem_Design)]
  85. [MonitoringDescription ("Name of the log that is read and written.")]
  86. public string Log {
  87. get {return logName;}
  88. set {logName = value;}
  89. }
  90. [Browsable (false)]
  91. public string LogDisplayName {
  92. get {return Impl.LogDisplayName;}
  93. }
  94. [ReadOnly (true), DefaultValue ("."), RecommendedAsConfigurable (true)]
  95. [MonitoringDescription ("Name of the machine that this log get written to.")]
  96. public string MachineName {
  97. get {return machineName;}
  98. set {machineName = value;}
  99. }
  100. [ReadOnly (true), DefaultValue (""), RecommendedAsConfigurable (true)]
  101. [TypeConverter ("System.Diagnostics.Design.StringValueConverter, " + Consts.AssemblySystem_Design)]
  102. [MonitoringDescription ("The application name that writes the log.")]
  103. public string Source {
  104. get {return source;}
  105. set {source = value;}
  106. }
  107. [Browsable (false), DefaultValue (null)]
  108. [MonitoringDescription ("An object that synchronizes event handler calls.")]
  109. public ISynchronizeInvoke SynchronizingObject {
  110. get {return synchronizingObject;}
  111. set {synchronizingObject = value;}
  112. }
  113. public void BeginInit()
  114. {
  115. Impl.BeginInit();
  116. }
  117. public void Clear()
  118. {
  119. Impl.Clear();
  120. }
  121. public void Close()
  122. {
  123. Impl.Close();
  124. }
  125. public static void CreateEventSource(string source, string logName)
  126. {
  127. CreateEventSource (source, logName, ".");
  128. }
  129. public static void CreateEventSource(string source,
  130. string logName,
  131. string machineName)
  132. {
  133. EventLogImpl.CreateEventSource (source, logName, machineName);
  134. }
  135. public static void Delete(string logName)
  136. {
  137. Delete (logName, ".");
  138. }
  139. public static void Delete(string logName, string machineName)
  140. {
  141. EventLogImpl.Delete (logName, machineName);
  142. }
  143. public static void DeleteEventSource(string source)
  144. {
  145. DeleteEventSource (source, ".");
  146. }
  147. public static void DeleteEventSource(string source,
  148. string machineName)
  149. {
  150. EventLogImpl.DeleteEventSource (source, machineName);
  151. }
  152. protected override void Dispose(bool disposing)
  153. {
  154. Impl.Dispose (disposing);
  155. }
  156. public void EndInit()
  157. {
  158. Impl.EndInit();
  159. }
  160. public static bool Exists(string logName)
  161. {
  162. return Exists (logName, ".");
  163. }
  164. public static bool Exists(string logName, string machineName)
  165. {
  166. return EventLogImpl.Exists (logName, machineName);
  167. }
  168. public static EventLog[] GetEventLogs()
  169. {
  170. return GetEventLogs (".");
  171. }
  172. public static EventLog[] GetEventLogs(string machineName)
  173. {
  174. return EventLogImpl.GetEventLogs (machineName);
  175. }
  176. public static string LogNameFromSourceName(string source,
  177. string machineName)
  178. {
  179. return EventLogImpl.LogNameFromSourceName (source, machineName);
  180. }
  181. public static bool SourceExists(string source)
  182. {
  183. return SourceExists (source, ".");
  184. }
  185. public static bool SourceExists(string source, string machineName)
  186. {
  187. return EventLogImpl.SourceExists (source, machineName);
  188. }
  189. public void WriteEntry(string message)
  190. {
  191. WriteEntry (message, EventLogEntryType.Information);
  192. }
  193. public void WriteEntry(string message, EventLogEntryType type)
  194. {
  195. WriteEntry (message, type, 0);
  196. }
  197. public void WriteEntry(string message, EventLogEntryType type,
  198. int eventID)
  199. {
  200. WriteEntry (message, type, eventID, 0);
  201. }
  202. public void WriteEntry(string message, EventLogEntryType type,
  203. int eventID,
  204. short category)
  205. {
  206. WriteEntry (message, type, eventID, category, null);
  207. }
  208. public void WriteEntry(string message, EventLogEntryType type,
  209. int eventID,
  210. short category, byte[] rawData)
  211. {
  212. Impl.WriteEntry (message, type, eventID, category, rawData);
  213. }
  214. public static void WriteEntry(string source, string message)
  215. {
  216. WriteEntry (source, message, EventLogEntryType.Information);
  217. }
  218. public static void WriteEntry(string source, string message,
  219. EventLogEntryType type)
  220. {
  221. WriteEntry (source, message, EventLogEntryType.Information, 0);
  222. }
  223. public static void WriteEntry(string source, string message,
  224. EventLogEntryType type, int eventID)
  225. {
  226. WriteEntry (source, message, EventLogEntryType.Information, eventID, 0);
  227. }
  228. public static void WriteEntry(string source, string message,
  229. EventLogEntryType type, int eventID, short category)
  230. {
  231. WriteEntry (source, message, EventLogEntryType.Information, eventID, category, null);
  232. }
  233. public static void WriteEntry(string source, string message,
  234. EventLogEntryType type, int eventID, short category,
  235. byte[] rawData)
  236. {
  237. EventLogImpl.WriteEntry (source, message, type, eventID, category, rawData);
  238. }
  239. internal void OnEntryWritten (EventLogEntry newEntry)
  240. {
  241. if (EntryWritten != null)
  242. EntryWritten (this, new EntryWrittenEventArgs (newEntry));
  243. }
  244. [MonitoringDescription ("Raised for each EventLog entry written.")]
  245. public event EntryWrittenEventHandler EntryWritten;
  246. }
  247. }