Winform 组件EventLog

using System.Diagnostics;
private void button6_Click(object sender, EventArgs e)
           {
               if (EventLog.SourceExists("source1"))
               {
                   EventLog.DeleteEventSource("source1");
                   EventLog.CreateEventSource("source1", "newlog1");
               }
               else
               {
                   EventLog.CreateEventSource("source1", "newlog1");
               }
               eventLog1.Source = "source1";
            
           }
           private void Form1_Load(object sender, EventArgs e)
           {
           }
           private void button5_Click(object sender, EventArgs e)
           {
               eventLog1.Source = "source1";
               eventLog1.WriteEntry("This is a message .");
               eventLog1.WriteEntry("this is a error message", EventLogEntryType.Error);
           }
           private void button4_Click(object sender, EventArgs e)
           {
               foreach (EventLogEntry en in eventLog1.Entries)
               {
                   MessageBox.Show(en.Message);
               }
           }
           private void button3_Click(object sender, EventArgs e)
           {
               eventLog1.Clear();
           }
           private void button2_Click(object sender, EventArgs e)
           {
               if (EventLog.SourceExists("source1"))
               {
                   EventLog.Delete("newlog1");
               }
           }
           private void button1_Click(object sender, EventArgs e)
           {
               if (EventLog.SourceExists("source1"))
               {
                   EventLog.DeleteEventSource("source1");
               }
           }

  • 如果您正计划将项写入到日志中,则必须完成以下两件事中的一件。必须使用 EventLog.CreateEventSource 方法注册一个源并将 EventLog 组件的 Source 属性设置为相同的源;或者必须设置该组件的 MachineNameLog 和 Source 属性。 注意   可以直接在组件上设置 Log 属性,或者也可以在将您的组件注册为事件源的代码中设置该属性。将该组件注册为一个源后,在服务器的注册表中就已经设置好了一个连接,它将您的组件与某特定日志联系起来。
  • 如果打算读取日志条目或监视日志,则必须设置 MachineName 和 Log 属性以指示您正在监视的日志。这种情况下不需要设置 Source 属性。
  • 如果您希望组件可以接收 EntryWritten 事件的通知,则必须将 EventLog.EnableRaisingEvents 设置为true。有关更多信息,请参见处理 EntryWritten 事件。

你可能感兴趣的:(Winform 组件EventLog)