Frm_Main.cs

View Code 1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8 namespace SaveLog
9 {
10 public partial class Frm_Main : Form
11 {
12 public Frm_Main()
13 {
14 InitializeComponent();
15 }
16
17 private void Frm_Main_Load(object sender, EventArgs e)
18 {
19 if (System.Diagnostics.EventLog.SourceExists("ErrEventLog"))//判断是否存在事件源
20 {
21 System.Diagnostics.EventLog.DeleteEventSource("ErrEventLog");//删除事件源注册
22 }
23 System.Diagnostics.EventLog.//创建日志信息
24 CreateEventSource("ErrEventLog", "Application");
25 eventLog2.Log = "Application";//设置日志名称
26 eventLog2.Source = "ErrEventLog";//事件源名称
27 this.eventLog1.MachineName = ".";//表示本机
28 }
29 private void btn_Find_Click(object sender, EventArgs e)
30 {
31 if (eventLog1.Entries.Count > 0)//判断是否存在系统日志
32 {
33 foreach (System.Diagnostics.EventLogEntry//遍历日志信息
34 entry in eventLog1.Entries)
35 {
36 if (entry.EntryType ==//判断是否为错误日志
37 System.Diagnostics.EventLogEntryType.Error)
38 {
39 listBox1.Items.Add(entry.Message);//向控件中添加数据项
40 eventLog2.WriteEntry(entry.Message,//写入日志信息
41 System.Diagnostics.EventLogEntryType.Error);
42 }
43 }
44 }
45 else
46 {
47 MessageBox.Show("系统没有错误日志.");//弹出消息对话框
48 }
49 }
50 }
51 }
Frm_Main.designer.cs

View Code 1 namespace SaveLog
2 {
3 partial class Frm_Main
4 {
5 /// <summary>
6 /// 必需的设计器变量。
7 /// </summary>
8 private System.ComponentModel.IContainer components = null;
9
10 /// <summary>
11 /// 清理所有正在使用的资源。
12 /// </summary>
13 /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
14 protected override void Dispose(bool disposing)
15 {
16 if (disposing && (components != null))
17 {
18 components.Dispose();
19 }
20 base.Dispose(disposing);
21 }
22
23 #region Windows 窗体设计器生成的代码
24
25 /// <summary>
26 /// 设计器支持所需的方法 - 不要
27 /// 使用代码编辑器修改此方法的内容。
28 /// </summary>
29 private void InitializeComponent()
30 {
31 this.eventLog1 = new System.Diagnostics.EventLog();
32 this.btn_Find = new System.Windows.Forms.Button();
33 this.listBox1 = new System.Windows.Forms.ListBox();
34 this.eventLog2 = new System.Diagnostics.EventLog();
35 this.label1 = new System.Windows.Forms.Label();
36 ((System.ComponentModel.ISupportInitialize)(this.eventLog1)).BeginInit();
37 ((System.ComponentModel.ISupportInitialize)(this.eventLog2)).BeginInit();
38 this.SuspendLayout();
39 //
40 // eventLog1
41 //
42 this.eventLog1.Log = "System";
43 this.eventLog1.SynchronizingObject = this;
44 //
45 // btn_Find
46 //
47 this.btn_Find.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
48 this.btn_Find.Location = new System.Drawing.Point(181, 4);
49 this.btn_Find.Name = "btn_Find";
50 this.btn_Find.Size = new System.Drawing.Size(198, 23);
51 this.btn_Find.TabIndex = 0;
52 this.btn_Find.Text = "查找并保存系统错误日志信息(&D)";
53 this.btn_Find.UseVisualStyleBackColor = true;
54 this.btn_Find.Click += new System.EventHandler(this.btn_Find_Click);
55 //
56 // listBox1
57 //
58 this.listBox1.FormattingEnabled = true;
59 this.listBox1.HorizontalScrollbar = true;
60 this.listBox1.ItemHeight = 12;
61 this.listBox1.Location = new System.Drawing.Point(12, 34);
62 this.listBox1.Name = "listBox1";
63 this.listBox1.Size = new System.Drawing.Size(367, 160);
64 this.listBox1.TabIndex = 1;
65 //
66 // eventLog2
67 //
68 this.eventLog2.Log = "Application";
69 this.eventLog2.SynchronizingObject = this;
70 //
71 // label1
72 //
73 this.label1.AutoSize = true;
74 this.label1.BackColor = System.Drawing.SystemColors.ControlLight;
75 this.label1.ForeColor = System.Drawing.SystemColors.ControlDarkDark;
76 this.label1.Location = new System.Drawing.Point(10, 9);
77 this.label1.Name = "label1";
78 this.label1.Size = new System.Drawing.Size(137, 12);
79 this.label1.TabIndex = 2;
80 this.label1.Text = "显示系统错误日志信息:";
81 //
82 // Frm_Main
83 //
84 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
85 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
86 this.ClientSize = new System.Drawing.Size(391, 206);
87 this.Controls.Add(this.label1);
88 this.Controls.Add(this.listBox1);
89 this.Controls.Add(this.btn_Find);
90 this.Name = "Frm_Main";
91 this.Text = "使用EventLog组件保存Windows系统日志";
92 this.Load += new System.EventHandler(this.Frm_Main_Load);
93 ((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit();
94 ((System.ComponentModel.ISupportInitialize)(this.eventLog2)).EndInit();
95 this.ResumeLayout(false);
96 this.PerformLayout();
97
98 }
99
100 #endregion
101
102 private System.Diagnostics.EventLog eventLog1;
103 private System.Windows.Forms.Button btn_Find;
104 private System.Windows.Forms.ListBox listBox1;
105 private System.Diagnostics.EventLog eventLog2;
106 private System.Windows.Forms.Label label1;
107 }
108 }
出处:http://www.cnblogs.com/ynbt/
关于作者:专注于.Net、WCF和移动互联网开发。
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,如有问题,可以通过ynbt_wang@163.com联系我,非常感谢。 。
使用EventLog组件保存Windows 系统日志
TAG: