New test.
[mono.git] / mcs / class / System / System.Diagnostics / EventLogImpl.cs
index 08aa4b81fc26a465499a3e94c3de42289eab689b..d72dc58b6c3837ecdff7118e8307a51337cff013 100644 (file)
@@ -3,8 +3,11 @@
 //
 // Authors:
 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
+//   Atsushi Enomoto  <atsushi@ximian.com>
+//   Gert Driesen (drieseng@users.sourceforge.net)
 //
 // (C) 2003 Andreas Nahr
+// (C) 2006 Novell, Inc.
 //
 //
 // Permission is hereby granted, free of charge, to any person obtaining
@@ -112,88 +115,114 @@ namespace System.Diagnostics
                        }
                }
 
+               public EventLogEntry [] GetEntries ()
+               {
+                       string logName = CoreEventLog.Log;
+                       if (logName == null || logName.Length == 0)
+                               throw new ArgumentException ("Log property value has not been specified.");
+
+                       if (!EventLog.Exists (logName))
+                               throw new InvalidOperationException (string.Format (
+                                       CultureInfo.InvariantCulture, "The event log '{0}' on "
+                                       + " computer '{1}' does not exist.", logName,
+                                       _coreEventLog.MachineName));
+
+                       int entryCount = GetEntryCount ();
+                       EventLogEntry [] entries = new EventLogEntry [entryCount];
+                       for (int i = 0; i < entryCount; i++) {
+                               entries [i] = GetEntry (i);
+                       }
+                       return entries;
+               }
+
                public abstract void BeginInit ();
 
                public abstract void Clear ();
 
                public abstract void Close ();
 
+               public abstract void CreateEventSource (EventSourceCreationData sourceData);
+
+               public abstract void Delete (string logName, string machineName);
+
+               public abstract void DeleteEventSource (string source, string machineName);
+
                public abstract void Dispose (bool disposing);
 
                public abstract void EndInit ();
 
-               public abstract EventLogEntry[] GetEntries ();
+               public abstract bool Exists (string logName, string machineName);
 
                protected abstract int GetEntryCount ();
 
                protected abstract EventLogEntry GetEntry (int index);
 
-               protected abstract string GetLogDisplayName ();
-
-               protected abstract void WriteEventLogEntry (EventLogEntry entry);
-
-               public void WriteEntry (string message, EventLogEntryType type, int eventID, short category, byte[] rawData)
-               {
-                       EventLogEntry entry = new EventLogEntry (string.Empty, category, 0, eventID,
-                               _coreEventLog.Source, message, string.Empty, _coreEventLog.MachineName,
-                               type, DateTime.Now, DateTime.Now, rawData, new string [] { message }, eventID);
-                       WriteEventLogEntry (entry);
-                       if (EntryWritten != null)
-                               EntryWritten (null, new EntryWrittenEventArgs (entry));
-               }
-       }
-
-       // Empty implementation that does not need any specific platform
-       // but should be enough to get applications to run that WRITE to eventlog
-       internal class NullEventLog : EventLogImpl
-       {
-               public NullEventLog (EventLog coreEventLog)
-                       : base (coreEventLog)
-               {
-               }
-
-               public override void BeginInit ()
-               {
-               }
-
-               public override void Clear ()
+               public EventLog [] GetEventLogs (string machineName)
                {
+                       string [] logNames = GetLogNames (machineName);
+                       EventLog [] eventLogs = new EventLog [logNames.Length];
+                       for (int i = 0; i < logNames.Length; i++) {
+                               EventLog eventLog = new EventLog (logNames [i], machineName);
+                               eventLogs [i] = eventLog;
+                       }
+                       return eventLogs;
                }
 
-               public override void Close ()
-               {
-               }
+               protected abstract string GetLogDisplayName ();
 
-               public override void Dispose (bool disposing)
-               {
-               }
+               public abstract string LogNameFromSourceName (string source, string machineName);
 
-               public override void EndInit ()
-               {
-               }
+               public abstract bool SourceExists (string source, string machineName);
 
-               public override EventLogEntry[] GetEntries ()
-               {
-                       return new EventLogEntry[0];
-               }
+               public abstract void WriteEntry (string [] replacementStrings, EventLogEntryType type, uint instanceID, short category, byte[] rawData);
 
-               protected override int GetEntryCount ()
-               {
-                       return 0;
-               }
+               protected abstract string FormatMessage (string source, uint messageID, string [] replacementStrings);
 
-               protected override EventLogEntry GetEntry (int index)
-               {
-                       return null;
-               }
+               protected abstract string [] GetLogNames (string machineName);
 
-               protected override string GetLogDisplayName ()
+               protected void ValidateCustomerLogName (string logName, string machineName)
                {
-                       return CoreEventLog.Log;
-               }
+                       if (logName.Length >= 8) {
+                               string significantName = logName.Substring (0, 8);
+                               if (string.Compare (significantName, "AppEvent", true) == 0 || string.Compare (significantName, "SysEvent", true) == 0 || string.Compare (significantName, "SecEvent", true) == 0)
+                                       throw new ArgumentException (string.Format (
+                                               CultureInfo.InvariantCulture, "The log name: '{0}' is"
+                                               + " invalid for customer log creation.", logName));
+
+                               // the first 8 characters of the log name are used as  filename
+                               // for .evt file and as such no two logs with 8 characters or 
+                               // more should have the same first 8 characters (or the .evt
+                               // would be overwritten)
+                               //
+                               // this check is not strictly necessary on unix
+                               string [] logs = GetLogNames (machineName);
+                               for (int i = 0; i < logs.Length; i++) {
+                                       string log = logs [i];
+                                       if (log.Length >= 8 && string.Compare (log, 0, significantName, 0, 8, true) == 0)
+                                               throw new ArgumentException (string.Format (
+                                                       CultureInfo.InvariantCulture, "Only the first eight"
+                                                       + " characters of a custom log name are significant,"
+                                                       + " and there is already another log on the system"
+                                                       + " using the first eight characters of the name given."
+                                                       + " Name given: '{0}', name of existing log: '{1}'.",
+                                                       logName, log));
+                               }
+                       }
 
-               protected override void WriteEventLogEntry (EventLogEntry entry)
-               {
+                       // LAMESPEC: check if the log name matches an existing source
+                       // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=186552
+                       if (SourceExists (logName, machineName)) {
+                               if (machineName == ".")
+                                       throw new ArgumentException (string.Format (
+                                               CultureInfo.InvariantCulture, "Log {0} has already been"
+                                               + " registered as a source on the local computer.", 
+                                               logName));
+                               else
+                                       throw new ArgumentException (string.Format (
+                                               CultureInfo.InvariantCulture, "Log {0} has already been"
+                                               + " registered as a source on the computer {1}.",
+                                               logName, machineName));
+                       }
                }
        }
 }