* Makefile: Don't build make-map.exe.
[mono.git] / mcs / class / System / System.Diagnostics / EventLogImpl.cs
1 //
2 // System.Diagnostics.EventLogImpl.cs
3 //
4 // Authors:
5 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
6 //   Atsushi Enomoto  <atsushi@ximian.com>
7 //   Gert Driesen (drieseng@users.sourceforge.net)
8 //
9 // (C) 2003 Andreas Nahr
10 // (C) 2006 Novell, Inc.
11 //
12 //
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:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
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.
31 //
32
33 using System;
34 using System.ComponentModel;
35 using System.ComponentModel.Design;
36 using System.Diagnostics;
37 using System.Globalization;
38
39 using Microsoft.Win32;
40
41 namespace System.Diagnostics
42 {
43         internal abstract class EventLogImpl
44         {
45                 readonly EventLog _coreEventLog;
46
47                 protected EventLogImpl (EventLog coreEventLog)
48                 {
49                         _coreEventLog = coreEventLog;
50                 }
51
52                 public event EntryWrittenEventHandler EntryWritten;
53
54                 protected EventLog CoreEventLog {
55                         get { return _coreEventLog; }
56                 }
57
58                 public int EntryCount {
59                         get {
60                                 if (_coreEventLog.Log == null || _coreEventLog.Log.Length == 0) {
61                                         throw new ArgumentException ("Log property is not set.");
62                                 }
63
64                                 if (!EventLog.Exists (_coreEventLog.Log, _coreEventLog.MachineName)) {
65                                         throw new InvalidOperationException (string.Format (
66                                                 CultureInfo.InvariantCulture, "The event log '{0}' on "
67                                                 + " computer '{1}' does not exist.", _coreEventLog.Log,
68                                                 _coreEventLog.MachineName));
69                                 }
70
71                                 return GetEntryCount ();
72                         }
73                 }
74
75                 public EventLogEntry this[int index] {
76                         get {
77                                 if (_coreEventLog.Log == null || _coreEventLog.Log.Length == 0) {
78                                         throw new ArgumentException ("Log property is not set.");
79                                 }
80
81                                 if (!EventLog.Exists (_coreEventLog.Log, _coreEventLog.MachineName)) {
82                                         throw new InvalidOperationException (string.Format (
83                                                 CultureInfo.InvariantCulture, "The event log '{0}' on "
84                                                 + " computer '{1}' does not exist.", _coreEventLog.Log,
85                                                 _coreEventLog.MachineName));
86                                 }
87
88                                 if (index < 0 || index >= EntryCount)
89                                         throw new ArgumentException ("Index out of range");
90
91                                 return GetEntry (index);
92                         }
93                 }
94
95                 public string LogDisplayName {
96                         get {
97 #if NET_2_0
98                                 // to-do perform valid character checks
99                                 if (_coreEventLog.Log != null && _coreEventLog.Log.Length == 0) {
100                                         throw new InvalidOperationException ("Event log names must"
101                                                 + " consist of printable characters and cannot contain"
102                                                 + " \\, *, ?, or spaces.");
103                                 }
104 #endif
105                                 if (_coreEventLog.Log != null) {
106                                         if (!EventLog.Exists (_coreEventLog.Log, _coreEventLog.MachineName)) {
107                                                 throw new InvalidOperationException (string.Format (
108                                                         CultureInfo.InvariantCulture, "Cannot find Log {0}"
109                                                         + " on computer {1}.", _coreEventLog.Log,
110                                                         _coreEventLog.MachineName));
111                                         }
112                                 }
113
114                                 return GetLogDisplayName ();
115                         }
116                 }
117
118                 public EventLogEntry [] GetEntries ()
119                 {
120                         string logName = CoreEventLog.Log;
121                         if (logName == null || logName.Length == 0)
122                                 throw new ArgumentException ("Log property value has not been specified.");
123
124                         if (!EventLog.Exists (logName))
125                                 throw new InvalidOperationException (string.Format (
126                                         CultureInfo.InvariantCulture, "The event log '{0}' on "
127                                         + " computer '{1}' does not exist.", logName,
128                                         _coreEventLog.MachineName));
129
130                         int entryCount = GetEntryCount ();
131                         EventLogEntry [] entries = new EventLogEntry [entryCount];
132                         for (int i = 0; i < entryCount; i++) {
133                                 entries [i] = GetEntry (i);
134                         }
135                         return entries;
136                 }
137
138                 public abstract void BeginInit ();
139
140                 public abstract void Clear ();
141
142                 public abstract void Close ();
143
144                 public abstract void CreateEventSource (EventSourceCreationData sourceData);
145
146                 public abstract void Delete (string logName, string machineName);
147
148                 public abstract void DeleteEventSource (string source, string machineName);
149
150                 public abstract void Dispose (bool disposing);
151
152                 public abstract void EndInit ();
153
154                 public abstract bool Exists (string logName, string machineName);
155
156                 protected abstract int GetEntryCount ();
157
158                 protected abstract EventLogEntry GetEntry (int index);
159
160                 public EventLog [] GetEventLogs (string machineName)
161                 {
162                         string [] logNames = GetLogNames (machineName);
163                         EventLog [] eventLogs = new EventLog [logNames.Length];
164                         for (int i = 0; i < logNames.Length; i++) {
165                                 EventLog eventLog = new EventLog (logNames [i], machineName);
166                                 eventLogs [i] = eventLog;
167                         }
168                         return eventLogs;
169                 }
170
171                 protected abstract string GetLogDisplayName ();
172
173                 public abstract string LogNameFromSourceName (string source, string machineName);
174
175                 public abstract bool SourceExists (string source, string machineName);
176
177                 public abstract void WriteEntry (string [] replacementStrings, EventLogEntryType type, uint instanceID, short category, byte[] rawData);
178
179                 protected abstract string FormatMessage (string source, uint messageID, string [] replacementStrings);
180
181                 protected abstract string [] GetLogNames (string machineName);
182
183                 protected void ValidateCustomerLogName (string logName, string machineName)
184                 {
185                         if (logName.Length >= 8) {
186                                 string significantName = logName.Substring (0, 8);
187                                 if (string.Compare (significantName, "AppEvent", true) == 0 || string.Compare (significantName, "SysEvent", true) == 0 || string.Compare (significantName, "SecEvent", true) == 0)
188                                         throw new ArgumentException (string.Format (
189                                                 CultureInfo.InvariantCulture, "The log name: '{0}' is"
190                                                 + " invalid for customer log creation.", logName));
191
192                                 // the first 8 characters of the log name are used as  filename
193                                 // for .evt file and as such no two logs with 8 characters or 
194                                 // more should have the same first 8 characters (or the .evt
195                                 // would be overwritten)
196                                 //
197                                 // this check is not strictly necessary on unix
198                                 string [] logs = GetLogNames (machineName);
199                                 for (int i = 0; i < logs.Length; i++) {
200                                         string log = logs [i];
201                                         if (log.Length >= 8 && string.Compare (log, 0, significantName, 0, 8, true) == 0)
202                                                 throw new ArgumentException (string.Format (
203                                                         CultureInfo.InvariantCulture, "Only the first eight"
204                                                         + " characters of a custom log name are significant,"
205                                                         + " and there is already another log on the system"
206                                                         + " using the first eight characters of the name given."
207                                                         + " Name given: '{0}', name of existing log: '{1}'.",
208                                                         logName, log));
209                                 }
210                         }
211
212                         // LAMESPEC: check if the log name matches an existing source
213                         // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=186552
214                         if (SourceExists (logName, machineName)) {
215                                 if (machineName == ".")
216                                         throw new ArgumentException (string.Format (
217                                                 CultureInfo.InvariantCulture, "Log {0} has already been"
218                                                 + " registered as a source on the local computer.", 
219                                                 logName));
220                                 else
221                                         throw new ArgumentException (string.Format (
222                                                 CultureInfo.InvariantCulture, "Log {0} has already been"
223                                                 + " registered as a source on the computer {1}.",
224                                                 logName, machineName));
225                         }
226                 }
227         }
228 }