2 // System.Diagnostics.EventLog.cs
5 // Jonathan Pryor (jonpryor@vt.edu)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
9 // (C) 2003 Andreas Nahr
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Diagnostics;
35 using System.ComponentModel;
36 using System.ComponentModel.Design;
38 namespace System.Diagnostics
40 [DefaultEvent ("EntryWritten")]
41 [InstallerType (typeof (EventLogInstaller))]
42 [Designer ("Microsoft.VisualStudio.Install.EventLogInstallableComponentDesigner, " + Consts.AssemblyMicrosoft_VisualStudio)]
43 public class EventLog : Component, ISupportInitialize
46 private string source;
47 private string logName;
48 private string machineName;
49 private bool doRaiseEvents = false;
50 private ISynchronizeInvoke synchronizingObject = null;
52 private EventLogImpl Impl;
59 public EventLog(string logName)
64 public EventLog(string logName, string machineName)
65 : this (logName, machineName, "")
69 public EventLog(string logName, string machineName, string source)
72 this.machineName = machineName;
73 this.logName = logName;
75 this.Impl = new EventLogImpl (this);
76 EventLogImpl.EntryWritten += new EntryWrittenEventHandler (EntryWrittenHandler);
79 private void EntryWrittenHandler (object sender, EntryWrittenEventArgs e)
82 OnEntryWritten (e.Entry);
85 [Browsable (false), DefaultValue (false)]
86 [MonitoringDescription ("If enabled raises event when a log is written.")]
87 public bool EnableRaisingEvents {
88 get {return doRaiseEvents;}
89 set {doRaiseEvents = value;}
92 [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
93 [MonitoringDescription ("The entries in the log.")]
94 public EventLogEntryCollection Entries {
95 get {return Impl.Entries;}
98 [ReadOnly (true), DefaultValue (""), RecommendedAsConfigurable (true)]
99 [TypeConverter ("System.Diagnostics.Design.LogConverter, " + Consts.AssemblySystem_Design)]
100 [MonitoringDescription ("Name of the log that is read and written.")]
102 get {return logName;}
103 set {logName = value;}
107 public string LogDisplayName {
108 get {return Impl.LogDisplayName;}
111 [ReadOnly (true), DefaultValue ("."), RecommendedAsConfigurable (true)]
112 [MonitoringDescription ("Name of the machine that this log get written to.")]
113 public string MachineName {
114 get {return machineName;}
115 set {machineName = value;}
118 [ReadOnly (true), DefaultValue (""), RecommendedAsConfigurable (true)]
119 [TypeConverter ("System.Diagnostics.Design.StringValueConverter, " + Consts.AssemblySystem_Design)]
120 [MonitoringDescription ("The application name that writes the log.")]
121 public string Source {
123 set {source = value;}
126 [Browsable (false), DefaultValue (null)]
127 [MonitoringDescription ("An object that synchronizes event handler calls.")]
128 public ISynchronizeInvoke SynchronizingObject {
129 get {return synchronizingObject;}
130 set {synchronizingObject = value;}
133 public void BeginInit()
148 public static void CreateEventSource(string source, string logName)
150 CreateEventSource (source, logName, ".");
153 public static void CreateEventSource(string source,
157 EventLogImpl.CreateEventSource (source, logName, machineName);
160 public static void Delete(string logName)
162 Delete (logName, ".");
165 public static void Delete(string logName, string machineName)
167 EventLogImpl.Delete (logName, machineName);
170 public static void DeleteEventSource(string source)
172 DeleteEventSource (source, ".");
175 public static void DeleteEventSource(string source,
178 EventLogImpl.DeleteEventSource (source, machineName);
181 protected override void Dispose(bool disposing)
183 Impl.Dispose (disposing);
186 public void EndInit()
191 public static bool Exists(string logName)
193 return Exists (logName, ".");
196 public static bool Exists(string logName, string machineName)
198 return EventLogImpl.Exists (logName, machineName);
201 public static EventLog[] GetEventLogs()
203 return GetEventLogs (".");
206 public static EventLog[] GetEventLogs(string machineName)
208 return EventLogImpl.GetEventLogs (machineName);
211 public static string LogNameFromSourceName(string source,
214 return EventLogImpl.LogNameFromSourceName (source, machineName);
217 public static bool SourceExists(string source)
219 return SourceExists (source, ".");
222 public static bool SourceExists(string source, string machineName)
224 return EventLogImpl.SourceExists (source, machineName);
227 public void WriteEntry(string message)
229 WriteEntry (message, EventLogEntryType.Information);
232 public void WriteEntry(string message, EventLogEntryType type)
234 WriteEntry (message, type, 0);
237 public void WriteEntry(string message, EventLogEntryType type,
240 WriteEntry (message, type, eventID, 0);
243 public void WriteEntry(string message, EventLogEntryType type,
247 WriteEntry (message, type, eventID, category, null);
250 public void WriteEntry(string message, EventLogEntryType type,
252 short category, byte[] rawData)
254 Impl.WriteEntry (message, type, eventID, category, rawData);
257 public static void WriteEntry(string source, string message)
259 WriteEntry (source, message, EventLogEntryType.Information);
262 public static void WriteEntry(string source, string message,
263 EventLogEntryType type)
265 WriteEntry (source, message, EventLogEntryType.Information, 0);
268 public static void WriteEntry(string source, string message,
269 EventLogEntryType type, int eventID)
271 WriteEntry (source, message, EventLogEntryType.Information, eventID, 0);
274 public static void WriteEntry(string source, string message,
275 EventLogEntryType type, int eventID, short category)
277 WriteEntry (source, message, EventLogEntryType.Information, eventID, category, null);
280 public static void WriteEntry(string source, string message,
281 EventLogEntryType type, int eventID, short category,
284 EventLogImpl.WriteEntry (source, message, type, eventID, category, rawData);
287 internal void OnEntryWritten (EventLogEntry newEntry)
289 if (EntryWritten != null)
290 EntryWritten (this, new EntryWrittenEventArgs (newEntry));
293 [MonitoringDescription ("Raised for each EventLog entry written.")]
294 public event EntryWrittenEventHandler EntryWritten;