日志管理完成版----.net练习篇

** 好在系统没有最终使用这个版本,这下可以放心的在网上发文了。其实就是简单的数据库连接啊,添加啊,删除啊什么的。 **

** 首先在数据库中写一个表Log (logdatetime,loguser,logtype,logmodule,logdescribe)后来发现这个表名跟关键字重了,不过也懒得改了。单独写了一个类用来添加数据到数据库中。代码如下: **

using System;
using System.Data;
using System.Data.SqlClient;

namespace LogModule
{
///

1<summary>   
2/// LogModule 的摘要说明。   
3/// </summary>

public class LogModule
{
public LogModule()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

///

1<summary>   
2/// 日志添加   
3/// </summary>

public static void log(string lu,string lt,string lm, string ld)
{
if (lu!=null && lt!=null && ld!=null)
{
try
{
SqlConnection logConn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=xxxx");
logConn.Open();

string logInsert ="insert into Log(logdatetime,loguser,logtype,logmodule,logdescribe) values(getdate(), '" +
lu+"', '"+lt+"','"+lm+"','"+ld+"')";

SqlCommand logCmd = new SqlCommand(logInsert,logConn);
logCmd.ExecuteNonQuery();

//释放资源
logCmd.Dispose();
logConn.Close();
}
catch (System.Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
}

** 这样其他类就能够引用这个方法了,而且不用new一个对象,用起来还算方便。而对于日志的管理,修改是没有必要的,删除和查询则是在于用户交互的过程中完成的,所以做了个form来实现,如上例,我还是粘出全文: **

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace LogModule
{
///

1<summary>   
2/// Form1 的摘要说明。   
3/// </summary>

public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button5;
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.Windows.Forms.ColumnHeader columnHeader3;
private System.Windows.Forms.ColumnHeader columnHeader4;
private System.Windows.Forms.ColumnHeader columnHeader5;
private System.Windows.Forms.ColumnHeader columnHeader6;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.ComboBox comboBox2;
private System.Windows.Forms.ComboBox comboBox3;
private System.Windows.Forms.ComboBox comboBox4;
private System.Windows.Forms.ComboBox comboBox5;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.TextBox textBox1;
static SqlConnection conn = new SqlConnection ("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=xxxx");

///

1<summary>   
2/// 必需的设计器变量。   
3/// </summary>

private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//

}

///

1<summary>   
2/// 清理所有正在使用的资源。   
3/// </summary>

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
///

1<summary>   
2/// 设计器支持所需的方法 - 不要使用代码编辑器修改   
3/// 此方法的内容。   
4/// </summary>

private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.button5 = new System.Windows.Forms.Button();
this.listView1 = new System.Windows.Forms.ListView();
this.columnHeader5 = new System.Windows.Forms.ColumnHeader();
this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
this.columnHeader6 = new System.Windows.Forms.ColumnHeader();
this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.comboBox2 = new System.Windows.Forms.ComboBox();
this.comboBox3 = new System.Windows.Forms.ComboBox();
this.comboBox4 = new System.Windows.Forms.ComboBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.comboBox5 = new System.Windows.Forms.ComboBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(552, 16);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(104, 48);
this.button1.TabIndex = 20;
this.button1.Text = "查找日志";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(576, 336);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(80, 40);
this.button2.TabIndex = 22;
this.button2.Text = "添加日志";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(552, 72);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(104, 48);
this.button3.TabIndex = 21;
this.button3.Text = "整理日志";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(576, 424);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(80, 48);
this.button4.TabIndex = 40;
this.button4.Text = "退出系统";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button5
//
this.button5.Location = new System.Drawing.Point(576, 376);
this.button5.Name = "button5";
this.button5.Size = new System.Drawing.Size(80, 40);
this.button5.TabIndex = 23;
this.button5.Text = "测试页面";
this.button5.Click += new System.EventHandler(this.button5_Click);
//
// listView1
//
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader5,
this.columnHeader1,
this.columnHeader2,
this.columnHeader3,
this.columnHeader6,
this.columnHeader4});
this.listView1.FullRowSelect = true;
this.listView1.GridLines = true;
this.listView1.Location = new System.Drawing.Point(16, 136);
this.listView1.MultiSelect = false;
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(640, 184);
this.listView1.TabIndex = 30;
this.listView1.View = System.Windows.Forms.View.Details;
this.listView1.SelectedIndexChanged += new System.EventHandler(this.listView1_SelectedIndexChanged);
//
// columnHeader5
//
this.columnHeader5.Text = "Log_ID";
//
// columnHeader1
//
this.columnHeader1.Text = " 日期 时间";
this.columnHeader1.Width = 130;
//
// columnHeader2
//
this.columnHeader2.Text = "用户";
this.columnHeader2.Width = 80;
//
// columnHeader3
//
this.columnHeader3.Text = "类型";
//
// columnHeader6
//
this.columnHeader6.Text = "模块";
this.columnHeader6.Width = 70;
//
// columnHeader4
//
this.columnHeader4.Text = "描述";
this.columnHeader4.Width = 210;
//
// comboBox1
//
this.comboBox1.Location = new System.Drawing.Point(16, 40);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(104, 20);
this.comboBox1.TabIndex = 10;
//
// comboBox2
//
this.comboBox2.Location = new System.Drawing.Point(16, 96);
this.comboBox2.Name = "comboBox2";
this.comboBox2.Size = new System.Drawing.Size(104, 20);
this.comboBox2.TabIndex = 11;
//
// comboBox3
//
this.comboBox3.Location = new System.Drawing.Point(152, 40);
this.comboBox3.Name = "comboBox3";
this.comboBox3.Size = new System.Drawing.Size(104, 20);
this.comboBox3.TabIndex = 12;
//
// comboBox4
//
this.comboBox4.Location = new System.Drawing.Point(152, 96);
this.comboBox4.Name = "comboBox4";
this.comboBox4.Size = new System.Drawing.Size(104, 20);
this.comboBox4.TabIndex = 13;
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(64, 16);
this.label1.TabIndex = 12;
this.label1.Text = "起始时间";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 72);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(72, 16);
this.label2.TabIndex = 13;
this.label2.Text = "最终时间";
//
// label3
//
this.label3.Location = new System.Drawing.Point(152, 16);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(72, 16);
this.label3.TabIndex = 14;
this.label3.Text = "操作用户";
//
// label4
//
this.label4.Location = new System.Drawing.Point(152, 72);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(64, 16);
this.label4.TabIndex = 15;
this.label4.Text = "处理类型";
//
// label5
//
this.label5.Location = new System.Drawing.Point(296, 16);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(64, 16);
this.label5.TabIndex = 16;
this.label5.Text = "模块";
//
// comboBox5
//
this.comboBox5.Location = new System.Drawing.Point(296, 40);
this.comboBox5.Name = "comboBox5";
this.comboBox5.Size = new System.Drawing.Size(104, 20);
this.comboBox5.TabIndex = 14;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(16, 336);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ReadOnly = true;
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox1.Size = new System.Drawing.Size(544, 136);
this.textBox1.TabIndex = 20;
this.textBox1.TabStop = false;
this.textBox1.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(672, 485);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.comboBox5);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.comboBox4);
this.Controls.Add(this.comboBox3);
this.Controls.Add(this.comboBox2);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.listView1);
this.Controls.Add(this.button5);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "日志管理";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

///

1<summary>   
2/// 应用程序的主入口点。   
3/// </summary>

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

///

1<summary>   
2/// 执行查找的指令   
3/// </summary>

///

1<param name="sender"/>

无参
///

1<param name="e"/>

无参
private void button1_Click(object sender, System.EventArgs e)
{
this.find(comboBox1.Text, comboBox2.Text, comboBox3.Text, comboBox4.Text, comboBox5.Text);
}

///

1<summary>   
2/// 添加一条测试的日志   
3/// </summary>

///

1<param name="sender"/>

///

1<param name="e"/>

private void button2_Click(object sender, System.EventArgs e)
{
//LogModule.log("服务控制台","操作","商品管理","测试事务添加一个新的log测试事务添加一个新的log测试事务添加一个新的log测试事务添加一个新的log测试事务添加一个新的log");
}

///

1<summary>   
2/// 整理日志,删除两个月以前的日志   
3/// </summary>

///

1<param name="sender"/>

无参
///

1<param name="e"/>

无参
private void button3_Click(object sender, System.EventArgs e)
{
System.Windows.Forms.DialogResult chose=MessageBox.Show("是否删除日志(保留近两个月的日志)","整理日志",MessageBoxButtons.OKCancel);

if((chose.ToString()).Equals("OK"))
{
//------------------------------------------------------------------------------
//注意此处where后面的用法
string logDel ="delete from LogManage where logdatetime

  1<dateadd(ms,-3,dateadd(mm, (system.exception="" 0))";="" <summary="" catch="" console.writeline(e1.message);="" datediff(m,0,getdate())-1,="" e1)="" logcmd="new" logcmd.dispose();="" logcmd.executenonquery();="" sqlcommand="" sqlcommand(logdel,conn);="" try="" {="" }="" 释放资源="">   
  2/// 日志查找   
  3/// C端最核心的代码了...写的有点长   
  4///    
  5public void find(string logdfirst, string logdlast, string loguser, string logtype, string logmodule)   
  6{   
  7//创建数据连接   
  8//string select="select logid,logdatetime,loguser,logtype,logmoduleone,logdescribeone,logmoduletwo,logdescribetwo from LogManage";   
  9string select="select * from Log"; 
 10
 11//清空listview的现实内容   
 12listView1.Items.Clear(); 
 13
 14if(logdfirst == null) logdfirst="";   
 15if(logdlast == null) logdlast="";   
 16if(loguser == null) loguser="";   
 17if(logtype == null) logtype="";   
 18if(logmodule == null) logmodule=""; 
 19
 20//---------------------------------------------------------------------------------   
 21//应该优化这里的代码,提高效率和减少判断的次数,现在感觉有bug了,太长了 
 22
 23if(logdfirst.Equals("") &amp;&amp; logdlast.Equals("") &amp;&amp; loguser.Equals("") &amp;&amp; logtype.Equals("") &amp;&amp; logmodule.Equals(""))   
 24{   
 25//初始状态   
 26}   
 27else   
 28{   
 29select+=" where 1&gt;0"; 
 30
 31if(!logdfirst.Equals(""))   
 32{   
 33select+=" and logdatetime&gt;CONVERT(datetime,'"+logdfirst+" 00:00:00.000')";   
 34}   
 35if(!logdlast.Equals(""))   
 36{   
 37select+=" and logdatetime&lt;=CONVERT(datetime,'"+logdlast+" 23:59:59.999')";   
 38}   
 39if(!loguser.Equals(""))   
 40{   
 41select+=" and loguser='"+loguser+"'";   
 42}   
 43if(!logtype.Equals(""))   
 44{   
 45select+=" and logtype='"+logtype+"'";   
 46}   
 47if(!logmodule.Equals(""))   
 48{   
 49select+=" and logmodule='"+logmodule+"'";   
 50}   
 51} 
 52
 53//select+=" order by logdatetime";   
 54//得到最终的select查询代码 
 55
 56//---------------------------------------------------------------------------------   
 57SqlCommand selectCMD = new SqlCommand (select,conn);   
 58//创建并初始化SqlCommand对象   
 59SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter();   
 60sqlDataAdapter1.SelectCommand = selectCMD;   
 61  
 62SqlDataReader myReader = selectCMD.ExecuteReader();   
 63  
 64while(myReader.Read())   
 65{   
 66ListViewItem item = new ListViewItem(myReader.GetInt64(0).ToString()); 
 67
 68item.SubItems.Add(myReader.GetDateTime(1).ToShortDateString() +   
 69" " + myReader.GetDateTime(1).ToLongTimeString() + "." +   
 70myReader.GetDateTime(1).Millisecond);   
 71item.SubItems.Add(myReader.GetString(2));   
 72item.SubItems.Add(myReader.GetString(3));   
 73item.SubItems.Add(myReader.GetString(4));   
 74item.SubItems.Add(myReader.GetString(5));   
 75listView1.Items.Add(item);   
 76}   
 77  
 78//释放资源   
 79myReader.Close();   
 80selectCMD.Dispose();   
 81sqlDataAdapter1.Dispose();   
 82} 
 83
 84/*   
 85/// <summary>   
 86/// 日志的删除 需引入时间做为参量   
 87/// 原先可以完成对单条记录的删除   
 88/// </summary>   
 89  
 90public void logKill(string dt, string hd, string tp, string dr)   
 91{   
 92string logDel ="delete from LogDesc where datetime = CONVERT(datetime,'"+dt+   
 93"') and handle='"+hd+"'"+   
 94" and type='"+tp+"' and descr='"+dr+"'";   
 95  
 96SqlCommand logCmd = new SqlCommand(logDel,conn);   
 97  
 98try   
 99{   
100logCmd.ExecuteNonQuery();   
101}   
102catch (System.Exception e)   
103{   
104Console.WriteLine(e.Message);   
105} 
106
107//释放资源   
108logCmd.Dispose();   
109}   
110*/ 
111
112/// <summary>   
113/// 退出系统   
114/// </summary>   
115/// <param name="sender"/>无参   
116/// <param name="e"/>无参   
117private void button4_Click(object sender, System.EventArgs e)   
118{   
119conn.Close();   
120Application.Exit();   
121} 
122
123/// <summary>   
124/// 初始化参量   
125/// </summary>   
126/// <param name="sender"/>无参   
127/// <param name="e"/>无参   
128private void Form1_Load(object sender, System.EventArgs e)   
129{   
130//*********************************************************************************   
131conn.Open(); 
132
133//*********************************************************************************   
134this.find("","","","",""); 
135
136//---------------------------------------------------------------------------------   
137//初始化时间的combobox内容   
138string logComb1="select distinct CONVERT(char(10),logdatetime,102) from Log"; 
139
140SqlCommand selectCMD = new SqlCommand (logComb1,conn);   
141//创建并初始化SqlCommand对象   
142SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter();   
143sqlDataAdapter1.SelectCommand = selectCMD;   
144  
145SqlDataReader myReader = selectCMD.ExecuteReader(); 
146
147while(myReader.Read())   
148{   
149comboBox1.Items.Add(myReader.GetString(0));   
150comboBox2.Items.Add(myReader.GetString(0));   
151} 
152
153//释放资源   
154myReader.Close();   
155selectCMD.Dispose();   
156sqlDataAdapter1.Dispose(); 
157
158//---------------------------------------------------------------------------------   
159//初始化操作员的combobox内容   
160string logComb3="select distinct loguser from Log"; 
161
162selectCMD = new SqlCommand (logComb3,conn);   
163//创建并初始化SqlCommand对象   
164sqlDataAdapter1 = new SqlDataAdapter();   
165sqlDataAdapter1.SelectCommand = selectCMD;   
166  
167myReader = selectCMD.ExecuteReader(); 
168
169while(myReader.Read())   
170{   
171comboBox3.Items.Add(myReader.GetString(0));   
172} 
173
174//释放资源   
175myReader.Close();   
176selectCMD.Dispose();   
177sqlDataAdapter1.Dispose(); 
178
179//---------------------------------------------------------------------------------   
180//初始化类型的combobox内容   
181string logComb4="select distinct logtype from Log"; 
182
183selectCMD = new SqlCommand (logComb4,conn);   
184//创建并初始化SqlCommand对象   
185sqlDataAdapter1 = new SqlDataAdapter();   
186sqlDataAdapter1.SelectCommand = selectCMD;   
187  
188myReader = selectCMD.ExecuteReader(); 
189
190while(myReader.Read())   
191{   
192comboBox4.Items.Add(myReader.GetString(0));   
193} 
194
195//释放资源   
196myReader.Close();   
197selectCMD.Dispose();   
198sqlDataAdapter1.Dispose(); 
199
200//---------------------------------------------------------------------------------   
201//初始化模块的combobox内容   
202string logComb5="select distinct logmodule from Log where logmodule!=''"; 
203
204selectCMD = new SqlCommand (logComb5,conn);   
205//创建并初始化SqlCommand对象   
206sqlDataAdapter1 = new SqlDataAdapter();   
207sqlDataAdapter1.SelectCommand = selectCMD;   
208  
209myReader = selectCMD.ExecuteReader(); 
210
211while(myReader.Read())   
212{   
213comboBox5.Items.Add(myReader.GetString(0));   
214} 
215
216//释放资源   
217myReader.Close();   
218selectCMD.Dispose();   
219sqlDataAdapter1.Dispose(); 
220
221} 
222
223/// <summary>   
224/// 单击listview在下方的textbox中显示出该条记录的详细信息   
225/// </summary>   
226/// <param name="sender"/>无参   
227/// <param name="e"/>无参   
228private void listView1_SelectedIndexChanged(object sender, System.EventArgs e)   
229{   
230int selectCount = listView1.SelectedItems.Count; 
231
232  
233if (selectCount !=0)   
234{   
235string id="",dt="",us="",tp="",md="",ds="";   
236  
237foreach(ListViewItem lvi in listView1.SelectedItems)   
238{   
239id=lvi.SubItems[0].Text;   
240dt=lvi.SubItems[1].Text;   
241us=lvi.SubItems[2].Text;   
242tp=lvi.SubItems[3].Text;   
243md=lvi.SubItems[4].Text;   
244ds=lvi.SubItems[5].Text; 
245
246textBox1.Text="LOG_ID:"+id+"\r\n时间:"+dt+"\r\n操作员:"+us+"\r\n类型:"+   
247tp+"\r\n模块:"+md+"\r\n描述:"+ds;   
248  
249  
250}   
251}   
252} 
253
254}   
255} 
256
257** 如果一句解释都没有呢,确实看着不太方便。但是文中确实没有什么难点,只是个个功能的简单累加,   
258以后使用的时候一旦忘了,拿出来看看,确实会方便些。  **
259
260** 尽以本文献给那些,入门的,入门很就忽然忘了的,随便看看的,热爱编程的战友们  。 **</dateadd(ms,-3,dateadd(mm,>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus