程序其实很简单直接贴代码了
eventLog1是一个控件
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace SetEventLog { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { if (System.Diagnostics.EventLog.SourceExists("ZhyScoure")) //判断是否存在事件源 { System.Diagnostics.EventLog.SourceExists("ZhyScoure"); //删除事件源注册 } System.Diagnostics.EventLog.CreateEventSource("ZhyScoure", "NewLog1"); //创建日志信息 eventLog1.Log = "NewLog1"; //设置日志名称 eventLog1.Source = "ZhyScoure"; //事件源名称 eventLog1.MachineName = "."; } private void btnWriteLog_Click(object sender, EventArgs e) { if (System.Diagnostics.EventLog.Exists("NewLog1")) { if (textBox1.Text != "") { eventLog1.WriteEntry(textBox1.Text.ToString()); MessageBox.Show("日志写入成功"); textBox1.Text = ""; } else { MessageBox.Show("日志内容不能为空"); } } else { MessageBox.Show("日志不存在"); } } } }
日志写入了那到哪儿去了呢?
键盘:WIn+R
ok了