向Windows 日志管理器写入系统程序日志信息

标准样例代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Diagnostics.Eventing;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string LogName = "Nieweiking_Log";
            string SourceName = "Nieweiking的程序";
            if (!EventLog.SourceExists(SourceName))
            {
                var EventSourceData = new EventSourceCreationData(SourceName, LogName);
                EventLog.CreateEventSource(EventSourceData);
            }

            using (var log = new EventLog(LogName, ".", SourceName))
            {
                log.WriteEntry("Message 1");
                log.WriteEntry("Message 2", EventLogEntryType.Warning);;
                log.WriteEntry("Message 2", EventLogEntryType.Information, 33);
            }
        }
    }
}

  

posted @ 2015-07-15 11:53  nieweiking  阅读(722)  评论(0编辑  收藏  举报