Ejemplo n.º 1
0
		/// <summary>
		/// ITestStep.Execute() implementation
		/// </summary>
		/// <param name='testConfig'>The Xml fragment containing the configuration for this test step</param>
		/// <param name='context'>The context for the test, this holds state that is passed beteen tests</param>
		public void Execute(XmlNode testConfig, Context context)
		{
			var rawListOfMachines = context.ReadConfigAsString(testConfig, "Machine", true);
            
            var listOfMachines = new List<string>();

            if (string.IsNullOrEmpty(rawListOfMachines))
            {
                listOfMachines.Add(Environment.MachineName);
            }
            else
            {
                listOfMachines.AddRange(rawListOfMachines.Split(','));
            }

            foreach (var machine in listOfMachines)
            {
                var eventLog = context.ReadConfigAsString(testConfig, "EventLog");

                using (var log = new EventLog(eventLog, machine))
                {
                    context.LogInfo("About to clear the '{0}' event log on machine '{1}'of all entries.", eventLog,
                                    machine);
                    log.Clear();
                }
            }
		}
 public void isSimpleEntryWriten()
 {
     EventLog ea=new EventLog("Test Log",".","Test Log");
     LogManager.Log("Hello world!");
     EventLogEntry[] entries=new EventLogEntry[ea.Entries.Count];
     ea.Entries.CopyTo(entries,0);
     Assertion.AssertEquals("Hello world!",entries[0].Message);
     ea.Clear();
 }
Ejemplo n.º 3
0
        public static void DeleteSource(string source)
        {
            if (EventLog.SourceExists(source, Environment.MachineName))
            {
                EventLog.DeleteEventSource(source, Environment.MachineName); // Delete Source

                using (var elog = new EventLog(EventLogName, Environment.MachineName, source))
                {
                    elog.Clear();
                }
            }
            // recreate
            CreateEventLog();
        }
Ejemplo n.º 4
0
 private void Clear()
 {
     this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Clearing EventLog: {0}", this.LogName));
     if (System.Diagnostics.EventLog.Exists(this.LogName, this.MachineName))
     {
         using (System.Diagnostics.EventLog targetLog = new System.Diagnostics.EventLog(this.LogName, this.MachineName))
         {
             targetLog.Clear();
         }
     }
     else
     {
         this.Log.LogError(string.Format(CultureInfo.CurrentCulture, "Invalid LogName Supplied: {0}", this.LogName));
     }
 }
Ejemplo n.º 5
0
        public void MultiThreadToEventLogSink()
        {
            using (EventLog log = new EventLog(customEventLog))
            {
                log.Clear();
            }

            ThreadStart testMethod = new ThreadStart(SendTestMessagesEventLog);

            base.ThreadStress(testMethod, threadCount);

            Thread.Sleep(500);

            int count = GetCountCustomEventLog();
            Assert.AreEqual(threadCount * loopCount, count);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// ImageService constructor.
        /// </summary>
        /// <param name="args">command line args</param>
        public ImageService(string[] args)
        {
            InitializeComponent();
            info = ConfigInfomation.Instance;
            // for APP.config
            // string outputFolder = ConfigurationManager.AppSettings["OutputDir"];
            //int thumbnailSize = Int32.Parse(ConfigurationManager.AppSettings["ThumbnailSize"]);
            // string eventSourceName = ConfigurationManager.AppSettings["SourceName"];
            // string logName = ConfigurationManager.AppSettings["LogName"];
            string eventSourceName = info.eventSourceName;
            string logName         = info.logName;

            //Change to insert params (if exist)
            //if (args.Count() > 0)
            //{
            //   eventSourceName = args[0];
            //}
            //if (args.Count() > 1)
            //{
            //    logName = args[1];
            //}
            eventLog1 = new System.Diagnostics.EventLog();
            if (!System.Diagnostics.EventLog.SourceExists(eventSourceName))
            {
                System.Diagnostics.EventLog.CreateEventSource(eventSourceName, logName);
            }
            eventLog1.Source = eventSourceName;
            eventLog1.Log    = logName;
            eventLog1.Clear();
            int a = eventLog1.Entries.Count;

            //eventLog1.WriteEntry("Num of entries: " + a, EventLogEntryType.Information, eventId++);
            //eventLog1.WriteEntry("eventId: " + eventId, EventLogEntryType.Information, eventId++);

            // create LoggingService, CurrentRunLog, ImageServiceModal, ImageController
            m_currentLog             = new CurrentRunLog();
            logging                  = new LoggingService();
            logging.MessageRecieved += m_currentLog.AddToLog;  //Write log in CurrentRunLog
            logging.MessageRecieved += eventLog1_EntryWritten; //Write log in entry

            modal      = new ImageServiceModal(ConfigInfomation.Instance.outputDir, ConfigInfomation.Instance.thumbnailSize);
            controller = new ImageController(modal);
            //Adding new option of LogCommand
            controller.insertCommand((int)CommandEnum.LogCommand, new GetCurrentRunLogCommand(m_currentLog));
        }
Ejemplo n.º 7
0
        private void btnClearEventLog_Click(object sender, EventArgs e)
        {
            string eventSourceName = _serviceInfo.EventSourceName;
            string eventLogName    = _serviceInfo.EventLogName;

            if (System.Diagnostics.EventLog.SourceExists(eventSourceName) == false)
            {
                MessageBox.Show("Not Exist Log in system.");
                return;
            }

            EventLog eventLog1 = new System.Diagnostics.EventLog();

            eventLog1.Source = eventSourceName;
            eventLog1.Log    = eventLogName;
            eventLog1.Clear();
            txtProcessOutput.Text = "";
            MessageBox.Show("Event Log Clear");
        }
Ejemplo n.º 8
0
 private void ClearEventLog()
 {
   base.setAction("Clearing EventLogs...");
   Update();
   int subActions = logNames.Length;
   foreach (string strLogName in logNames)
   {
     EventLog e = new EventLog(strLogName);
     try
     {
       e.Clear();
     }
     catch (Exception) {}
     updateProgress(subActions);
   }
   if (subActions == 0)
   {
     updateProgress(1);
   }
 }
Ejemplo n.º 9
0
        /// <summary>
        /// The function stops the service flow
        /// </summary>
        protected override void OnStop()
        {
            // Update the service state to Pause Pending.
            ServiceStatus serviceStatus = new ServiceStatus();

            serviceStatus.dwCurrentState = ServiceState.SERVICE_PAUSE_PENDING;
            serviceStatus.dwWaitHint     = 100000;
            SetServiceStatus(this.ServiceHandle, ref serviceStatus);

            // Write a log entry.
            logging.Log("In OnStop", MessageTypeEnum.INFO);

            // Update the service state to Paused.
            serviceStatus.dwCurrentState = ServiceState.SERVICE_STOPPED;
            SetServiceStatus(this.ServiceHandle, ref serviceStatus);
            m_imageServer.CloseServer();
            logging.MessageRecieved -= OnMessage;
            eventLog1.Clear();
            Dispose();
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Helper function to write an entry to the Event Log.
 /// </summary>
 /// <param name="entry">The entry to enter into the Event Log.</param>
 /// <param name="type">The EventLogEntryType to be used when the entry is logged to the Event Log.</param>
 private void WriteToLog(string entry, EventLogEntryType type)
 {
     try
     {
         // Write the entry to the Event Log.
         EventLog.WriteEntry(applicationName, entry, type);
     }
     catch (SecurityException e)
     {
         //Commented as per Task No:3028
         //MessageBox.Show("Error Occured while Logging Exception.Please contact System Administrator : " + entry);
         throw new SecurityException(String.Format(resourceManager.GetString("RES_DEFAULTPUBLISHER_EVENTLOG_DENIED"), applicationName), e);
     }
     catch (Exception ex)
     {
         //MessageBox.Show("Error Occured while Logging Exception.Please contact System Administrator : " + entry);
         System.Diagnostics.EventLog objLog = new System.Diagnostics.EventLog("Application");
         objLog.Clear();
         EventLog.WriteEntry(applicationName, entry, type);
     }
 }
Ejemplo n.º 11
0
 public void WriteEvent(string strMessage, EventLogEntryType type)
 {
     if (!EventLog.SourceExists(this._sourece))
     EventLog.CreateEventSource(this._sourece, this._eventName);
       EventLog eventLog = new EventLog();
       eventLog.Log = this._eventName;
       eventLog.Source = this._sourece;
       try
       {
     eventLog.WriteEntry(strMessage, type);
       }
       catch (Exception ex)
       {
     eventLog.Clear();
     this.WriteEvent("日志文件已满,执行清除操作!", EventLogEntryType.Warning);
     eventLog.WriteEntry(strMessage, type);
       }
       finally
       {
     eventLog.Close();
     eventLog.Dispose();
       }
 }
        protected void RunAfterAllTests()
        {
            TestHelper.ClearLogs();

            // clear log entries in Application log
            EventLog log = new EventLog(APPLICATION, MACHINE_NAME, SOURCE);
            log.Clear();
        }
Ejemplo n.º 13
0
		public void Clear ()
		{
			if (EventLogImplType == NULL_IMPL)
				// test cannot pass with NULL implementation
				return;

			if (EventLog.SourceExists ("monotempsource", "."))
				Assert.Ignore ("Event log source 'monotempsource' should not exist.");

			if (EventLog.SourceExists ("monoothersource", "."))
				Assert.Ignore ("Event log source 'monoothersource' should not exist.");

			if (EventLog.Exists ("monologtemp", "."))
				Assert.Ignore ("Event log 'monologtemp' should not exist.");

			EventLog.CreateEventSource ("monotempsource", "monologtemp", ".");
			EventLog.CreateEventSource ("monoothersource", "monologtemp", ".");
			try {
				using (EventLog eventLog = new EventLog ("monologtemp", ".", "monotempsource")) {
					// MSBUG: Assert.AreEqual (0, eventLog.Entries.Count, "#A1");
					eventLog.Clear ();
					Assert.AreEqual (0, eventLog.Entries.Count, "#A2");
					Assert.IsTrue (EventLog.SourceExists ("monotempsource", "."), "#A3");
					Assert.IsTrue (EventLog.SourceExists ("monoothersource", "."), "#A4");
					Assert.IsTrue (EventLog.Exists ("monologtemp", "."), "#A5");
					Assert.IsFalse (EventLog.Exists ("monotempsource", "."), "#A6");
					Assert.IsFalse (EventLog.Exists ("monoothersource", "."), "#A7");

					EventLog.WriteEntry ("monotempsource", "Clear1");

					Assert.AreEqual (1, eventLog.Entries.Count, "#B1");
					eventLog.Clear ();
					Assert.AreEqual (0, eventLog.Entries.Count, "#B2");
					Assert.IsTrue (EventLog.SourceExists ("monotempsource", "."), "#B3");
					Assert.IsTrue (EventLog.SourceExists ("monoothersource", "."), "#B4");
					Assert.IsTrue (EventLog.Exists ("monologtemp", "."), "#B5");
					Assert.IsFalse (EventLog.Exists ("monotempsource", "."), "#B6");
					Assert.IsFalse (EventLog.Exists ("monoothersource", "."), "#B7");

					EventLog.WriteEntry ("monotempsource", "Clear2");
					eventLog.Clear ();
					EventLog.WriteEntry ("monotempsource", "Clear3");
					EventLog.WriteEntry ("monoothersource", "Clear4");

					Assert.AreEqual (2, eventLog.Entries.Count, "#C1");
					Assert.IsTrue (EventLog.SourceExists ("monotempsource", "."), "#C2");
					Assert.IsTrue (EventLog.SourceExists ("monoothersource", "."), "#C3");
					Assert.IsTrue (EventLog.Exists ("monologtemp", "."), "#C4");
					Assert.IsFalse (EventLog.Exists ("monotempsource", "."), "#C5");
					Assert.IsFalse (EventLog.Exists ("monoothersource", "."), "#C6");

					EventLogEntry entry = eventLog.Entries [0];
					Assert.IsNotNull (entry, "#D1");
					Assert.IsNotNull (entry.Category, "#D2");
					Assert.AreEqual ("(0)", entry.Category, "#D3");
					Assert.AreEqual (0, entry.CategoryNumber, "#D4");
					Assert.IsNotNull (entry.Data, "#D5");
					Assert.AreEqual (0, entry.Data.Length, "#D6");
					Assert.AreEqual (EventLogEntryType.Information, entry.EntryType, "#D7");
					Assert.AreEqual (0, entry.EventID, "#D8");
#if NET_2_0
					Assert.AreEqual (entry.EventID, entry.InstanceId, "#D9");
#endif
					Assert.IsNotNull (entry.MachineName, "#D10");
					Assert.AreEqual (Environment.MachineName, entry.MachineName, "#D11");
					Assert.IsNotNull (entry.ReplacementStrings, "#D12");
					Assert.AreEqual (1, entry.ReplacementStrings.Length, "#D13");
					Assert.AreEqual ("Clear3", entry.ReplacementStrings [0], "#D14");
					Assert.IsNotNull (entry.Source, "#D15");
					Assert.AreEqual ("monotempsource", entry.Source, "#D16");
					Assert.IsNull (entry.UserName, "#D17");

					entry = eventLog.Entries [1];
					Assert.IsNotNull (entry, "#E1");
					Assert.IsNotNull (entry.Category, "#E2");
					Assert.AreEqual ("(0)", entry.Category, "#E3");
					Assert.AreEqual (0, entry.CategoryNumber, "#E4");
					Assert.IsNotNull (entry.Data, "#E5");
					Assert.AreEqual (0, entry.Data.Length, "#E6");
					Assert.AreEqual (EventLogEntryType.Information, entry.EntryType, "#E7");
					Assert.AreEqual (0, entry.EventID, "#E8");
#if NET_2_0
					Assert.AreEqual (entry.EventID, entry.InstanceId, "#E9");
#endif
					Assert.IsNotNull (entry.MachineName, "#E10");
					Assert.AreEqual (Environment.MachineName, entry.MachineName, "#E11");
					Assert.IsNotNull (entry.ReplacementStrings, "#E12");
					Assert.AreEqual (1, entry.ReplacementStrings.Length, "#E13");
					Assert.AreEqual ("Clear4", entry.ReplacementStrings [0], "#E14");
					Assert.IsNotNull (entry.Source, "#E15");
					Assert.AreEqual ("monoothersource", entry.Source, "#E16");
					Assert.IsNull (entry.UserName, "#E17");

					eventLog.Clear ();
					Assert.AreEqual (0, eventLog.Entries.Count, "#F1");
					Assert.IsTrue (EventLog.SourceExists ("monotempsource", "."), "#F2");
					Assert.IsTrue (EventLog.SourceExists ("monoothersource", "."), "#F3");
					Assert.IsTrue (EventLog.Exists ("monologtemp", "."), "#F4");
					Assert.IsFalse (EventLog.Exists ("monotempsource", "."), "#F5");
					Assert.IsFalse (EventLog.Exists ("monoothersource", "."), "#F6");
				}
			} finally {
				if (EventLog.Exists ("monologtemp"))
					EventLog.Delete ("monologtemp");
			}
		}
Ejemplo n.º 14
0
        private void ButtonClear_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                EventLog myLog = new EventLog();

                myLog.Log = comboBoxEve.SelectedItem.ToString();
                if (myLog.Entries.Count != 0)
                {
                    myLog.Clear();
                    System.Windows.Forms.MessageBox.Show(comboBoxEve.SelectedItem.ToString() + " log has been cleared", "Alert", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show(comboBoxEve.SelectedItem.ToString() + " has no logs logged.", "Alert", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                comboBoxEve.Focus();
                ex.Message.ToString();
                //MessageBox.Show(ex.Message);
                System.Windows.Forms.MessageBox.Show(ex.Message.ToString());
            }
        }
 private void ClearLogs()
 {
     EventLog oEventLog = new EventLog();
     oEventLog.Source = "Database";
     oEventLog.Clear();
 }
		private void ClearLog(EventLog log)
		{
			log.Clear();

			EventLogViewer child = FindChildForLog(log);

			if (child != null)
			{
				child.BringToFront();
				child.Focus();
				child.RefreshGrid();
			}
		}
Ejemplo n.º 17
0
 /// <summary>
 /// Does the processing
 /// </summary>
 protected override void BeginProcessing()
 {
     string computer = string.Empty;
     foreach (string compName in ComputerName)
     {
         if ((compName.Equals("localhost", StringComparison.CurrentCultureIgnoreCase)) || (compName.Equals(".", StringComparison.OrdinalIgnoreCase)))
         {
             computer = "localhost";
         }
         else
         {
             computer = compName;
         }
         foreach (string eventString in LogName)
         {
             try
             {
                 if (!EventLog.Exists(eventString, compName))
                 {
                     ErrorRecord er = new ErrorRecord(new InvalidOperationException(StringUtil.Format(EventlogResources.LogDoesNotExist, eventString, computer)), null, ErrorCategory.InvalidOperation, null);
                     WriteError(er);
                     continue;
                 }
                 if (!ShouldProcess(StringUtil.Format(EventlogResources.ClearEventLogWarning, eventString, computer)))
                 {
                     continue;
                 }
                 EventLog Log = new EventLog(eventString, compName);
                 Log.Clear();
             }
             catch (System.IO.IOException)
             {
                 ErrorRecord er = new ErrorRecord(new System.IO.IOException(StringUtil.Format(EventlogResources.PathDoesNotExist, null, computer)), null, ErrorCategory.InvalidOperation, null);
                 WriteError(er);
                 continue;
             }
             catch (Win32Exception)
             {
                 ErrorRecord er = new ErrorRecord(new Win32Exception(StringUtil.Format(EventlogResources.NoAccess, null, computer)), null, ErrorCategory.PermissionDenied, null);
                 WriteError(er);
                 continue;
             }
             catch (InvalidOperationException)
             {
                 ErrorRecord er = new ErrorRecord(new InvalidOperationException(StringUtil.Format(EventlogResources.OSWritingError)), null, ErrorCategory.ReadError, null);
                 WriteError(er);
                 continue;
             }
         }
     }
 }
Ejemplo n.º 18
0
 private void Clear()
 {
     this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Clearing EventLog: {0}", this.LogName));
     if (System.Diagnostics.EventLog.Exists(this.LogName, this.MachineName))
     {
         using (System.Diagnostics.EventLog targetLog = new System.Diagnostics.EventLog(this.LogName, this.MachineName))
         {
             targetLog.Clear();
         }
     }
     else
     {
         this.Log.LogError(string.Format(CultureInfo.CurrentCulture, "Invalid LogName Supplied: {0}", this.LogName));
     }
 }
Ejemplo n.º 19
0
		public void Entries_Source_Null ()
		{
			if (EventLogImplType == NULL_IMPL)
				// test cannot pass with NULL implementation
				return;

			if (EventLog.SourceExists ("monotempsource", "."))
				Assert.Ignore ("Event log source 'monotempsource' should not exist.");

			if (EventLog.Exists ("monologtemp", "."))
				Assert.Ignore ("Event log 'monologtemp' should not exist.");

			EventLog.CreateEventSource ("monotempsource", "monologtemp", ".");
			try {
				using (EventLog eventLog = new EventLog ("monologtemp", ".", null)) {
					Assert.IsNotNull (eventLog.Entries, "#A1");
					Assert.AreEqual (0, eventLog.Entries.Count, "#A2");
					EventLog.WriteEntry ("monotempsource", "Entries_Source_Null1");
					Assert.AreEqual (1, eventLog.Entries.Count, "#A3");

					IEnumerator enumerator = eventLog.Entries.GetEnumerator ();
					Assert.IsNotNull (enumerator, "#B");

					try {
						object current = enumerator.Current;
						Assert.Fail ("#C1: " + current);
					} catch (InvalidOperationException ex) {
						// No current EventLog entry available, cursor is located
						// before the first or after the last element of the
						// enumeration
						Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
						Assert.IsNotNull (ex.Message, "#C3");
						Assert.IsNull (ex.InnerException, "#C4");
					}

					Assert.IsTrue (enumerator.MoveNext (), "#D1");
					Assert.IsNotNull (enumerator.Current, "#D2");
					Assert.IsFalse (enumerator.MoveNext (), "#D3");

					EventLogEntry [] entries = new EventLogEntry [1];
					eventLog.Entries.CopyTo (entries, 0);

					EventLogEntry entry = entries [0];
					Assert.IsNotNull (entry, "#E1");
					Assert.IsNotNull (entry.Source, "#E2");
					Assert.AreEqual ("monotempsource", entry.Source, "#E3");
					Assert.IsNotNull (entry.ReplacementStrings, "#E4");
					Assert.AreEqual (1, entry.ReplacementStrings.Length, "#E5");
					Assert.AreEqual ("Entries_Source_Null1", entry.ReplacementStrings [0], "#E6");

					try {
						object current = enumerator.Current;
						Assert.Fail ("#E1: " + current);
					} catch (InvalidOperationException ex) {
						// No current EventLog entry available, cursor is located
						// before the first or after the last element of the
						// enumeration
						Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#E2");
						Assert.IsNotNull (ex.Message, "#E3");
						Assert.IsNull (ex.InnerException, "#E4");
					}

					try {
						object current = enumerator.Current;
						Assert.Fail ("#F1: " + current);
					} catch (InvalidOperationException ex) {
						// No current EventLog entry available, cursor is located
						// before the first or after the last element of the
						// enumeration
						Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#F2");
						Assert.IsNotNull (ex.Message, "#F3");
						Assert.IsNull (ex.InnerException, "#F4");
					}

					EventLog.WriteEntry ("monotempsource", "Entries_Source_Null2");

#if NET_2_0
					try {
						object current = enumerator.Current;
						Assert.Fail ("#G1: " + current);
					} catch (InvalidOperationException ex) {
						// No current EventLog entry available, cursor is located
						// before the first or after the last element of the
						// enumeration
						Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#G2");
						Assert.IsNotNull (ex.Message, "#G3");
						Assert.IsNull (ex.InnerException, "#G4");
					}
#else
					entry = (EventLogEntry) enumerator.Current;
					Assert.IsNotNull (entry, "#G1");
					Assert.IsNotNull (entry.Source, "#G2");
					Assert.AreEqual ("monotempsource", entry.Source, "#G3");
					Assert.IsNotNull (entry.ReplacementStrings, "#G4");
					Assert.AreEqual (1, entry.ReplacementStrings.Length, "#G5");
					Assert.AreEqual ("Entries_Source_Null2", entry.ReplacementStrings [0], "#G6");
#endif

					Assert.IsFalse (enumerator.MoveNext (), "#H1");
					Assert.AreEqual (2, eventLog.Entries.Count, "#H2");

					entries = new EventLogEntry [1];
					try {
						eventLog.Entries.CopyTo (entries, 0);
						Assert.Fail ("#I1");
					} catch (ArgumentException ex) {
						// Destination array was not long enough. Check destIndex
						// and length, and the array's lower bounds
						Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#I2");
						Assert.IsNotNull (ex.Message, "#I3");
						Assert.IsNull (ex.InnerException, "#I4");
#if NET_2_0
						Assert.AreEqual ("", ex.ParamName, "#I5");
#else
						Assert.IsNull (ex.ParamName, "#I5");
#endif
					}

					entries = new EventLogEntry [2];
					eventLog.Entries.CopyTo (entries, 0);

					entry = entries [0];
					Assert.IsNotNull (entry, "#J1");
					Assert.IsNotNull (entry.Source, "#J2");
					Assert.AreEqual ("monotempsource", entry.Source, "#J3");
					Assert.IsNotNull (entry.ReplacementStrings, "#J4");
					Assert.AreEqual (1, entry.ReplacementStrings.Length, "#J5");
					Assert.AreEqual ("Entries_Source_Null1", entry.ReplacementStrings [0], "#J6");

					entry = entries [1];
					Assert.IsNotNull (entry, "#K1");
					Assert.IsNotNull (entry.Source, "#K2");
					Assert.AreEqual ("monotempsource", entry.Source, "#K3");
					Assert.IsNotNull (entry.ReplacementStrings, "#K4");
					Assert.AreEqual (1, entry.ReplacementStrings.Length, "#K5");
					Assert.AreEqual ("Entries_Source_Null2", entry.ReplacementStrings [0], "#K6");

					Assert.IsFalse (enumerator.MoveNext (), "#L1");
					enumerator.Reset ();
					Assert.IsTrue (enumerator.MoveNext (), "#L2");
					Assert.IsNotNull (enumerator.Current, "#L3");
					Assert.IsTrue (enumerator.MoveNext (), "#L4");
					Assert.IsNotNull (enumerator.Current, "#L5");

					Assert.IsFalse (enumerator.MoveNext (), "#M1");
					enumerator.Reset ();
					Assert.IsTrue (enumerator.MoveNext (), "#M2");
					eventLog.Clear ();
#if NET_2_0
					Assert.IsNotNull (enumerator.Current, "#M3");
#else
					try {
						object current = enumerator.Current;
						Assert.Fail ("#M3a: " + current);
					} catch (InvalidOperationException ex) {
						// No current EventLog entry available, cursor is located
						// before the first or after the last element of the
						// enumeration
						Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#M3b");
						Assert.IsNotNull (ex.Message, "#M3c");
						Assert.IsNull (ex.InnerException, "#M3d");
					}
#endif
					Assert.IsFalse (enumerator.MoveNext (), "#M4");

					try {
						object current = enumerator.Current;
						Assert.Fail ("#N1: " + current);
					} catch (InvalidOperationException ex) {
						// No current EventLog entry available, cursor is located
						// before the first or after the last element of the
						// enumeration
						Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#N2");
						Assert.IsNotNull (ex.Message, "#N3");
						Assert.IsNull (ex.InnerException, "#N4");
					}

					Assert.IsFalse (enumerator.MoveNext (), "#O1");
					enumerator.Reset ();
					Assert.IsFalse (enumerator.MoveNext (), "#O2");
				}
			} finally {
				if (EventLog.Exists ("monologtemp"))
					EventLog.Delete ("monologtemp");
			}
		}
Ejemplo n.º 20
0
        public static void Delete(string logName, string machineName)
        {
            if (!SyntaxCheck.CheckMachineName(machineName))
            {
                throw new ArgumentException(SR.GetString("InvalidParameterFormat", new object[] { "machineName" }));
            }
            if ((logName == null) || (logName.Length == 0))
            {
                throw new ArgumentException(SR.GetString("NoLogName"));
            }
            if (!ValidLogName(logName, false))
            {
                throw new InvalidOperationException(SR.GetString("BadLogName"));
            }
            new EventLogPermission(EventLogPermissionAccess.Administer, machineName).Demand();
            SharedUtils.CheckEnvironment();
            _UnsafeGetAssertPermSet().Assert();
            RegistryKey eventLogRegKey = null;
            Mutex       mutex          = null;

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                SharedUtils.EnterMutex("netfxeventlog.1.0", ref mutex);
                try
                {
                    eventLogRegKey = GetEventLogRegKey(machineName, true);
                    if (eventLogRegKey == null)
                    {
                        throw new InvalidOperationException(SR.GetString("RegKeyNoAccess", new object[] { @"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog", machineName }));
                    }
                    using (RegistryKey key2 = eventLogRegKey.OpenSubKey(logName))
                    {
                        if (key2 == null)
                        {
                            throw new InvalidOperationException(SR.GetString("MissingLog", new object[] { logName, machineName }));
                        }
                        EventLog log = new EventLog(logName, machineName);
                        try
                        {
                            log.Clear();
                        }
                        finally
                        {
                            log.Close();
                        }
                        string path = null;
                        try
                        {
                            path = (string)key2.GetValue("File");
                        }
                        catch
                        {
                        }
                        if (path != null)
                        {
                            try
                            {
                                File.Delete(path);
                            }
                            catch
                            {
                            }
                        }
                    }
                    eventLogRegKey.DeleteSubKeyTree(logName);
                }
                finally
                {
                    if (eventLogRegKey != null)
                    {
                        eventLogRegKey.Close();
                    }
                    CodeAccessPermission.RevertAssert();
                }
            }
            finally
            {
                if (mutex != null)
                {
                    mutex.ReleaseMutex();
                }
            }
        }
Ejemplo n.º 21
0
 private void button2_Click(object sender, EventArgs e)
 {
     EventLog log = new EventLog("Application", ".");
     log.Clear();
     MessageBox.Show("OK");
 }
 public void Test_CreateRivetCustomEventLog()
 {
     RivetCustomEventLogSource RivetLog = new RivetCustomEventLogSource(Custom_EventLog, Custom_Source, string.Empty);
     Assert.IsTrue(RivetLog.RivetWriteEntry(Custom_Source, Sample_ErrMsg, EventLogEntryType.Error));
     Assert.IsTrue(System.Diagnostics.EventLog.Exists(Custom_EventLog, Environment.MachineName));
     EventLog myLog = new EventLog();
     myLog.Log = RivetLog.RivetEventLogName;
     Assert.IsTrue(myLog.Entries.Count > 0);
     myLog.Clear();
     Assert.IsTrue(myLog.Entries.Count == 0);
     if (System.Diagnostics.EventLog.Exists(Custom_EventLog, Environment.MachineName))
         System.Diagnostics.EventLog.Delete(Custom_EventLog, Environment.MachineName);
     Assert.IsFalse(System.Diagnostics.EventLog.Exists(Custom_EventLog, Environment.MachineName));
 }
        public void Test_OverflowingEventLog()
        {
            RivetCustomEventLogSource RivetLog = new RivetCustomEventLogSource(Custom_EventLog, Custom_Source, string.Empty);
            RivetLog.RivetWriteEntry(Custom_Source, Sample_ErrMsg, System.Diagnostics.EventLogEntryType.Error);

            // Overflow the event log...
            EventLog myLog = new EventLog();
            myLog.Source = RivetLog.RivetEventLogName;
            myLog.Log = EventLog.LogNameFromSourceName(this.Custom_Source, Environment.MachineName);

            Console.WriteLine("Processing Event Log : " + myLog.LogDisplayName.ToString());

            Console.WriteLine("___________________________________________");

            // Clear the log;
            myLog.Clear();

            Console.WriteLine("Cleared eventlog. Current Log Entry Count : " + myLog.Entries.Count.ToString());

            // Get 102 entries into the log.
            for (int i = 0; i < 102; i++)
            {
                RivetLog.RivetWriteEntry(Custom_Source, Sample_ErrMsg + " " + Environment.TickCount.ToString()
                    , System.Diagnostics.EventLogEntryType.Information);
            }

            Console.WriteLine("Wrote 102 entries. Current Log Entry Count : " + myLog.Entries.Count.ToString());
            // Now write to the event log till we have 101 entries.
            // Which means that we've cleared the log once and have rewritten to it again, up to this message,
            //  expecting 101 final log entries.

            while (myLog.Entries.Count != 101)
            {
                RivetLog.RivetWriteEntry(Custom_Source, Sample_ErrMsg + " " + Environment.TickCount.ToString()
                    , System.Diagnostics.EventLogEntryType.Information);
                Console.WriteLine("Current Log Entry Count : " + myLog.Entries.Count.ToString());
            }

            // our log should now have only 101.  (100 of the latest entries, plus our current one.
            Console.WriteLine("Expecting 101 log entries (last saved 100 entries + the current message). Current Count : " + myLog.Entries.Count.ToString());
            Assert.IsTrue(myLog.Entries.Count == 101);

            Console.WriteLine("___________________________________________");
        }
 public static void Delete(string logName, string machineName)
 {
     if (!SyntaxCheck.CheckMachineName(machineName))
     {
         throw new ArgumentException(SR.GetString("InvalidParameterFormat", new object[] { "machineName" }));
     }
     if ((logName == null) || (logName.Length == 0))
     {
         throw new ArgumentException(SR.GetString("NoLogName"));
     }
     if (!ValidLogName(logName, false))
     {
         throw new InvalidOperationException(SR.GetString("BadLogName"));
     }
     new EventLogPermission(EventLogPermissionAccess.Administer, machineName).Demand();
     SharedUtils.CheckEnvironment();
     _UnsafeGetAssertPermSet().Assert();
     RegistryKey eventLogRegKey = null;
     Mutex mutex = null;
     RuntimeHelpers.PrepareConstrainedRegions();
     try
     {
         SharedUtils.EnterMutex("netfxeventlog.1.0", ref mutex);
         try
         {
             eventLogRegKey = GetEventLogRegKey(machineName, true);
             if (eventLogRegKey == null)
             {
                 throw new InvalidOperationException(SR.GetString("RegKeyNoAccess", new object[] { @"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog", machineName }));
             }
             using (RegistryKey key2 = eventLogRegKey.OpenSubKey(logName))
             {
                 if (key2 == null)
                 {
                     throw new InvalidOperationException(SR.GetString("MissingLog", new object[] { logName, machineName }));
                 }
                 EventLog log = new EventLog(logName, machineName);
                 try
                 {
                     log.Clear();
                 }
                 finally
                 {
                     log.Close();
                 }
                 string path = null;
                 try
                 {
                     path = (string) key2.GetValue("File");
                 }
                 catch
                 {
                 }
                 if (path != null)
                 {
                     try
                     {
                         File.Delete(path);
                     }
                     catch
                     {
                     }
                 }
             }
             eventLogRegKey.DeleteSubKeyTree(logName);
         }
         finally
         {
             if (eventLogRegKey != null)
             {
                 eventLogRegKey.Close();
             }
             CodeAccessPermission.RevertAssert();
         }
     }
     finally
     {
         if (mutex != null)
         {
             mutex.ReleaseMutex();
         }
     }
 }
Ejemplo n.º 25
0
		public void Clear_Log_DoesNotExist ()
		{
			if (EventLogImplType == NULL_IMPL)
				// test cannot pass with NULL implementation
				return;

			if (EventLog.Exists ("monologtemp", "."))
				Assert.Ignore ("Event log 'monologtemp' should not exist.");

			using (EventLog eventLog = new EventLog ("monologtemp", ".")) {
				try {
					eventLog.Clear ();
					Assert.Fail ("#1");
				} catch (InvalidOperationException ex) {
					// The event log 'monologtemp' on computer '.' does not exist
					Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
					Assert.IsNotNull (ex.Message, "#3");
					Assert.IsTrue (ex.Message.IndexOf ("'monologtemp'") != -1, "#4");
					Assert.IsTrue (ex.Message.IndexOf ("'.'") != -1, "#5");
					Assert.IsNull (ex.InnerException, "#6");
				}
				Assert.IsFalse (EventLog.Exists ("monologtemp", "."), "#7");
			}
		}
Ejemplo n.º 26
0
        /// <summary>
        /// Clear the event window viewer for SpringCard input
        /// </summary>
        private static void LogClear()
        {
            lock (locker)
            {
                try
                {
                    if (eventLog != null)
                    {
                        eventLog.Clear();

                        var eventLog1 = new EventLog("SpringCard", System.Environment.MachineName, "scCcidNetworkSvc");
                        eventLog1.Clear();
                    }
                }
                catch
                {

                }

            }
        }
Ejemplo n.º 27
0
		public void Clear_Log_Empty ()
		{
			if (EventLogImplType == NULL_IMPL)
				// test cannot pass with NULL implementation
				return;

			if (EventLog.SourceExists ("monotempsource", "."))
				Assert.Ignore ("Event log source 'monotempsource' should not exist.");

			if (EventLog.SourceExists ("monoothersource", "."))
				Assert.Ignore ("Event log source 'monoothersource' should not exist.");

			if (EventLog.Exists ("monologtemp", "."))
				Assert.Ignore ("Event log 'monologtemp' should not exist.");

			EventLog.CreateEventSource ("monotempsource", "monologtemp", ".");
			try {
				using (EventLog eventLog = new EventLog (string.Empty, ".")) {
					EventLog.WriteEntry ("monotempsource", "Clear_Log_Empty");

					// both source & log are not set
					try {
						eventLog.Clear ();
						Assert.Fail ("#A1");
					} catch (ArgumentException ex) {
						// Log property value has not been specified.
						Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
						Assert.IsNotNull (ex.Message, "#A3");
						Assert.IsNull (ex.InnerException, "#A4");
						Assert.IsNull (ex.ParamName, "#A5");
					}
					Assert.AreEqual (string.Empty, eventLog.Log, "#A6");

					// set non-existing source
					eventLog.Source = "monoothersource";

					try {
						eventLog.Clear ();
						Assert.Fail ("#B1");
					} catch (ArgumentException ex) {
						// Log property value has not been specified.
						Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
						Assert.IsNotNull (ex.Message, "#B3");
						Assert.IsNull (ex.InnerException, "#B4");
						Assert.IsNull (ex.ParamName, "#B5");
					}
					Assert.AreEqual (string.Empty, eventLog.Log, "#B6");

					// set existing source
					eventLog.Source = "monotempsource";

					Assert.IsTrue (eventLog.Entries.Count > 0, "#C1");
					eventLog.Clear ();
					Assert.AreEqual ("monologtemp", eventLog.Log, "#C2");
					Assert.AreEqual (0, eventLog.Entries.Count, "#C3");
				}
			} finally {
				if (EventLog.Exists ("monologtemp"))
					EventLog.Delete ("monologtemp");
			}
		}
Ejemplo n.º 28
0
        protected void RunAfterAllTests()
        {
            // TestHelper.StopWCFService();

            TestHelper.ClearLogs();

            // clear log entries in Application log
            EventLog log = new EventLog("Application", ".", "SourceLog4NET");
            log.Clear();
        }
Ejemplo n.º 29
0
		public void Clear_Source_Null ()
		{
			if (EventLogImplType == NULL_IMPL)
				// test cannot pass with NULL implementation
				return;

			if (EventLog.SourceExists ("monotempsource", "."))
				Assert.Ignore ("Event log source 'monotempsource' should not exist.");

			if (EventLog.Exists ("monologtemp", "."))
				Assert.Ignore ("Event log 'monologtemp' should not exist.");

			EventLog.CreateEventSource ("monotempsource", "monologtemp", ".");
			try {
				using (EventLog eventLog = new EventLog ("monologtemp", ".", null)) {
					EventLog.WriteEntry ("monotempsource", "Clear_Source_Null");

					Assert.IsTrue (eventLog.Entries.Count > 0, "#1");
					eventLog.Clear ();
					Assert.AreEqual (0, eventLog.Entries.Count, "#2");
					Assert.IsTrue (EventLog.SourceExists ("monotempsource", "."), "#3");
					Assert.IsTrue (EventLog.Exists ("monologtemp", "."), "#4");
					Assert.IsFalse (EventLog.Exists ("monotempsource", "."), "#5");
				}
			} finally {
				if (EventLog.Exists ("monologtemp"))
					EventLog.Delete ("monologtemp");
			}
		}
Ejemplo n.º 30
0
        ///// <summary>
        ///// Delete the named EventLog
        ///// </summary>
        ///// <param name="EventLogName"></param>
        ///// <param name="TargetMachineName"></param>
        ///// <returns></returns>
        //public bool DeleteRivetEventLog(string EventLogName, string TargetMachineName)
        //{
        //    bool retVal = true;
        //    try
        //    {
        //        if (TargetMachineName == string.Empty || TargetMachineName == null)
        //            TargetMachineName = Environment.MachineName;
        //        eventLogName = EventLogName;
        //        if (EventLog.Exists(EventLogName, TargetMachineName))
        //            EventLog.Delete(EventLogName, TargetMachineName);
        //    }
        //    catch (Exception)
        //    {
        //        retVal = false;
        //    }
        //    return retVal;
        //}
        ///// <summary>
        ///// Remove an associate source with this named event log;
        ///// </summary>
        ///// <param name="EventSource"></param>
        ///// <param name="EventLogName"></param>
        ///// <param name="TargetMachineName"></param>
        ///// <returns></returns>
        //public bool DeleteRivetEventSource(string EventSource, string EventLogName, string TargetMachineName)
        //{
        //    bool retVal = true;
        //    try
        //    {
        //        if (TargetMachineName == string.Empty || TargetMachineName == null)
        //            TargetMachineName = Environment.MachineName;
        //        eventSourceName = EventSource;
        //        if (EventLog.SourceExists(EventSource, TargetMachineName))
        //            EventLog.DeleteEventSource(EventSource, TargetMachineName);
        //    }
        //    catch (Exception)
        //    {
        //        retVal = false;
        //    }
        //    return retVal;
        //}
        /// <summary>
        /// Write an entry into the currently associated event log;
        /// </summary>
        /// <param name="Message"></param>
        /// <param name="EntryType"></param>
        /// <returns></returns>
        public bool RivetWriteEntry(string EventSource, string Message, System.Diagnostics.EventLogEntryType EntryType)
        {
            bool retVal = true;
            try
            {
                if (RivetEventLogName != string.Empty)
                    EventLog.WriteEntry(EventSource, Message, EntryType);
            }
            catch
            {
                try
                {
                    // Clear the log, but keep the last 100 entries.
                    retVal = false;
                    EventLog myLog = new EventLog();
                    myLog.Log = EventLog.LogNameFromSourceName(EventSource, Environment.MachineName);
                    ArrayList mySavedEntries = new ArrayList();
                    for (int i=0; i < myLog.Entries.Count; i++)
                    {
                        if ( (myLog.Entries.Count - i) > 100)
                            continue;
                        mySavedEntries.Add(myLog.Entries[i]);
                    }

                    // Clear the log (removes all entries);
                    myLog.Clear();

                    if (mySavedEntries != null )
                    {
                        for (int i = 0; i< mySavedEntries.Count; i++)
                        {
                            EventLogEntry le = (EventLogEntry) mySavedEntries[i];
                            EventLog.WriteEntry(le.Source.ToString(), le.Message.ToString(), le.EntryType);
                        }
                    }

                    // Finally, write our original message which could not fit!
                    if (RivetEventLogName != string.Empty)
                        EventLog.WriteEntry(EventSource, Message,EntryType);

                    retVal = true;
                }
                catch
                {
                    //Do nothing.  Couldn't write to log, possibly because of restricted permissions.
                }
            }

            return retVal;
        }
Ejemplo n.º 31
0
        private void clearEventsMenuItem_Click(object sender, System.EventArgs e)
        {
            if (_rightClickedNode == null)
                return;

            string logName = (string) _rightClickedNode.Tag;
            string machineName = (string) _rightClickedNode.Parent.Tag;
            using (EventLog eventLog = new EventLog (logName, machineName)) {
                eventLog.Clear ();
            }

            if (logTree.SelectedNode == _rightClickedNode)
                // only perform an actual refresh if the right-clicked node is
                // also the selected node
                RefreshEventEntryList ();
        }
Ejemplo n.º 32
0
		public void Entries ()
		{
			EventLogEntry entry = null;
			object current = null;

			if (EventLogImplType == NULL_IMPL)
				// test cannot pass with NULL implementation
				Assert.Ignore ("No EventLogImplType.");

			if (EventLog.SourceExists ("monotempsource", "."))
				Assert.Ignore ("Event log source 'monotempsource' should not exist.");

			if (EventLog.Exists ("monologtemp", "."))
				Assert.Ignore ("Event log 'monologtemp' should not exist.");

			EventLog.CreateEventSource ("monotempsource", "monologtemp", ".");
			try {
				using (EventLog eventLog = new EventLog ("monologtemp", ".", string.Empty)) {
					Assert.IsNotNull (eventLog.Entries, "#A1");
					Assert.AreEqual (0, eventLog.Entries.Count, "#A2");

					IEnumerator enumerator = eventLog.Entries.GetEnumerator ();
					Assert.IsNotNull (enumerator, "#B");

					try {
						current = enumerator.Current;
						Assert.Fail ("#C1: " + current);
					} catch (InvalidOperationException ex) {
						// No current EventLog entry available, cursor is located
						// before the first or after the last element of the
						// enumeration
						Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
						Assert.IsNotNull (ex.Message, "#C3");
						Assert.IsNull (ex.InnerException, "#C4");
					}

					Assert.IsFalse (enumerator.MoveNext (), "#D");

					try {
						current = enumerator.Current;
						Assert.Fail ("#E1: " + current);
					} catch (InvalidOperationException ex) {
						// No current EventLog entry available, cursor is located
						// before the first or after the last element of the
						// enumeration
						Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#E2");
						Assert.IsNotNull (ex.Message, "#E3");
						Assert.IsNull (ex.InnerException, "#E4");
					}

					EventLogEntry [] entries = new EventLogEntry [0];
					eventLog.Entries.CopyTo (entries, 0);

					EventLog.WriteEntry ("monotempsource", "Entries1");

					try {
						current = enumerator.Current;
						Assert.Fail ("#G1: " + current);
					} catch (InvalidOperationException ex) {
						// No current EventLog entry available, cursor is located
						// before the first or after the last element of the
						// enumeration
						Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#G2");
						Assert.IsNotNull (ex.Message, "#G3");
						Assert.IsNull (ex.InnerException, "#G4");
					}

					Assert.IsFalse (enumerator.MoveNext (), "#H1");
					Assert.AreEqual (1, eventLog.Entries.Count, "#H2");
					enumerator.Reset ();

					entries = new EventLogEntry [0];
					try {
						eventLog.Entries.CopyTo (entries, 0);
						Assert.Fail ("#I1");
					} catch (ArgumentException ex) {
						// Destination array was not long enough. Check destIndex
						// and length, and the array's lower bounds
						Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#I2");
						Assert.IsNotNull (ex.Message, "#I3");
						Assert.IsNull (ex.InnerException, "#I4");
						Assert.AreEqual ("", ex.ParamName, "#I5");
					}

					entries = new EventLogEntry [1];
					eventLog.Entries.CopyTo (entries, 0);

					entry = entries [0];
					Assert.IsNotNull (entry, "#J1");
					Assert.IsNotNull (entry.Source, "#J2");
					Assert.AreEqual ("monotempsource", entry.Source, "#J3");
					Assert.IsNotNull (entry.ReplacementStrings, "#J4");
					Assert.AreEqual (1, entry.ReplacementStrings.Length, "#J5");
					Assert.AreEqual ("Entries1", entry.ReplacementStrings [0], "#J6");

					Assert.IsTrue (enumerator.MoveNext (), "#K1");
					Assert.IsNotNull (enumerator.Current, "#K2");
					Assert.IsFalse (enumerator.MoveNext (), "#K3");
					enumerator.Reset ();
					Assert.IsTrue (enumerator.MoveNext (), "#K4");
					Assert.IsNotNull (enumerator.Current, "#K5");
					Assert.IsFalse (enumerator.MoveNext (), "#K6");

					EventLog.WriteEntry ("monotempsource", "Entries2");
					EventLog.WriteEntry ("monotempsource", "Entries3");

					Assert.IsTrue (enumerator.MoveNext (), "#L");

					entry = (EventLogEntry) enumerator.Current;
					Assert.IsNotNull (entry, "#M1");
					Assert.IsNotNull (entry.Source, "#M2");
					Assert.AreEqual ("monotempsource", entry.Source, "#M3");
					Assert.IsNotNull (entry.ReplacementStrings, "#M4");
					Assert.AreEqual (1, entry.ReplacementStrings.Length, "#M5");
					Assert.AreEqual ("Entries3", entry.ReplacementStrings [0], "#M6");

					enumerator.Reset ();
					Assert.IsNotNull (enumerator.Current, "#N1");
					Assert.IsTrue (enumerator.MoveNext (), "#N2");
					Assert.IsNotNull (enumerator.Current, "#N3");
					Assert.IsTrue (enumerator.MoveNext (), "#N4");
					Assert.IsNotNull (enumerator.Current, "#N5");
					Assert.IsTrue (enumerator.MoveNext (), "#N6");
					Assert.IsNotNull (enumerator.Current, "#N7");
					Assert.IsFalse (enumerator.MoveNext (), "#N8");
					enumerator.Reset ();

					try {
						current = enumerator.Current;
						Assert.Fail ("#O1: " + current);
					} catch (InvalidOperationException ex) {
						// No current EventLog entry available, cursor is located
						// before the first or after the last element of the
						// enumeration
						Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#O2");
						Assert.IsNotNull (ex.Message, "#O3");
						Assert.IsNull (ex.InnerException, "#O4");
					}

					Assert.IsTrue (enumerator.MoveNext (), "#P1");
					Assert.IsNotNull (enumerator.Current, "#P2");
					eventLog.Clear ();
					Assert.IsNotNull (enumerator.Current, "#P3");
					Assert.IsFalse (enumerator.MoveNext (), "#P4");
					Assert.AreEqual (0, eventLog.Entries.Count, "#P5");

					try {
						current = enumerator.Current;
						Assert.Fail ("#Q1: " + current);
					} catch (InvalidOperationException ex) {
						// No current EventLog entry available, cursor is located
						// before the first or after the last element of the
						// enumeration
						Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#Q2");
						Assert.IsNotNull (ex.Message, "#Q3");
						Assert.IsNull (ex.InnerException, "#Q4");
					}

					Assert.IsFalse (enumerator.MoveNext (), "#R1");
					enumerator.Reset ();
					Assert.IsFalse (enumerator.MoveNext (), "#R2");
				}
			} finally {
				if (EventLog.Exists ("monologtemp"))
					EventLog.Delete ("monologtemp");
			}
		}
Ejemplo n.º 33
0
        public void ClearLog(string logName)
        {
			EventLog log = new EventLog(logName);
			log.Clear();
        }
Ejemplo n.º 34
0
        public static void Delete(string logName, string machineName) {

            if (!SyntaxCheck.CheckMachineName(machineName))
                throw new ArgumentException(SR.GetString(SR.InvalidParameterFormat, "machineName"));
            if (logName == null || logName.Length==0)
                throw new ArgumentException(SR.GetString(SR.NoLogName));
            if (!ValidLogName(logName, false))
                throw new InvalidOperationException(SR.GetString(SR.BadLogName));

            EventLogPermission permission = new EventLogPermission(EventLogPermissionAccess.Administer, machineName);
            permission.Demand();

            //Check environment before even trying to play with the registry
            SharedUtils.CheckEnvironment();

            //SECREVIEW: Note that EventLog permission is demanded above.
            PermissionSet permissionSet = _UnsafeGetAssertPermSet();
            permissionSet.Assert();
            
            RegistryKey eventlogkey = null;

            Mutex mutex = null;
            RuntimeHelpers.PrepareConstrainedRegions();
            try {
                SharedUtils.EnterMutex(eventLogMutexName, ref mutex);

                try {
                    eventlogkey  = GetEventLogRegKey(machineName, true);
                    if (eventlogkey  == null) {
                        // there's not even an event log service on the machine.
                        // or, more likely, we don't have the access to read the registry.
                        throw new InvalidOperationException(SR.GetString(SR.RegKeyNoAccess, "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\EventLog", machineName));
                    }

                    using (RegistryKey logKey = eventlogkey.OpenSubKey(logName)) {
                        if (logKey == null)
                            throw new InvalidOperationException(SR.GetString(SR.MissingLog, logName, machineName));

                        //clear out log before trying to delete it
                        //that way, if we can't delete the log file, no entries will persist because it has been cleared
                        EventLog logToClear = new EventLog(logName, machineName);
                        try {
                            logToClear.Clear();
                        }
                        finally {
                            logToClear.Close();
                        }

                        // 


                        string filename = null;
                        try {
                            //most of the time, the "File" key does not exist, but we'll still give it a whirl
                            filename = (string) logKey.GetValue("File");
                        }
                        catch { }
                        if (filename != null) {
                            try {
                                File.Delete(filename);
                            }
                            catch { }
                        }
                    }

                    // now delete the registry entry
                    eventlogkey.DeleteSubKeyTree(logName);
                }
                finally {
                    if (eventlogkey != null) eventlogkey.Close();
                
                    // Revert registry and environment permission asserts
                    CodeAccessPermission.RevertAssert();
                }
            }
            finally {
                if (mutex != null) mutex.ReleaseMutex();
            }
        }