好在系统没有最终使用这个版本,这下可以放心的在网上发文了。其实就是简单的数据库连接啊,添加啊,删除啊什么的。
首先在数据库中写一个表Log (logdatetime,loguser,logtype,logmodule,logdescribe)后来发现这个表名跟关键字重了,不过也懒得改了。单独写了一个类用来添加数据到数据库中。代码如下:
using System;
using System.Data;
using System.Data.SqlClient;
namespace LogModule
{
/// <summary>
/// LogModule 的摘要说明。
/// </summary>
public class LogModule
{
public LogModule()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 日志添加
/// </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
{
/// <summary>
/// Form1 的摘要说明。
/// </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");
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </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
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
/// <summary>
/// 执行查找的指令
/// </summary>
/// <param name="sender">无参</param>
/// <param name="e">无参</param>
private void button1_Click(object sender, System.EventArgs e)
{
this.find(comboBox1.Text, comboBox2.Text, comboBox3.Text, comboBox4.Text, comboBox5.Text);
}
/// <summary>
/// 添加一条测试的日志
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, System.EventArgs e)
{
//LogModule.log("服务控制台","操作","商品管理","测试事务添加一个新的log测试事务添加一个新的log测试事务添加一个新的log测试事务添加一个新的log测试事务添加一个新的log");
}
/// <summary>
/// 整理日志,删除两个月以前的日志
/// </summary>
/// <param name="sender">无参</param>
/// <param name="e">无参</param>
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<dateadd(ms,-3,DATEADD(mm, DATEDIFF(m,0,getdate())-1, 0))";
SqlCommand logCmd = new SqlCommand(logDel,conn);
try
{
logCmd.ExecuteNonQuery();
}
catch (System.Exception e1)
{
Console.WriteLine(e1.Message);
}
//释放资源
logCmd.Dispose();
}
}
/// <summary>
/// 日志查找
/// C端最核心的代码了...写的有点长
/// </summary>
public void find(string logdfirst, string logdlast, string loguser, string logtype, string logmodule)
{
//创建数据连接
//string select="select logid,logdatetime,loguser,logtype,logmoduleone,logdescribeone,logmoduletwo,logdescribetwo from LogManage";
string select="select * from Log";
//清空listview的现实内容
listView1.Items.Clear();
if(logdfirst == null) logdfirst="";
if(logdlast == null) logdlast="";
if(loguser == null) loguser="";
if(logtype == null) logtype="";
if(logmodule == null) logmodule="";
//---------------------------------------------------------------------------------
//应该优化这里的代码,提高效率和减少判断的次数,现在感觉有bug了,太长了
if(logdfirst.Equals("") && logdlast.Equals("") && loguser.Equals("") && logtype.Equals("") && logmodule.Equals(""))
{
//初始状态
}
else
{
select+=" where 1>0";
if(!logdfirst.Equals(""))
{
select+=" and logdatetime>CONVERT(datetime,'"+logdfirst+" 00:00:00.000')";
}
if(!logdlast.Equals(""))
{
select+=" and logdatetime<=CONVERT(datetime,'"+logdlast+" 23:59:59.999')";
}
if(!loguser.Equals(""))
{
select+=" and loguser='"+loguser+"'";
}
if(!logtype.Equals(""))
{
select+=" and logtype='"+logtype+"'";
}
if(!logmodule.Equals(""))
{
select+=" and logmodule='"+logmodule+"'";
}
}
//select+=" order by logdatetime";
//得到最终的select查询代码
//---------------------------------------------------------------------------------
SqlCommand selectCMD = new SqlCommand (select,conn);
//创建并初始化SqlCommand对象
SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter();
sqlDataAdapter1.SelectCommand = selectCMD;
SqlDataReader myReader = selectCMD.ExecuteReader();
while(myReader.Read())
{
ListViewItem item = new ListViewItem(myReader.GetInt64(0).ToString());
item.SubItems.Add(myReader.GetDateTime(1).ToShortDateString() +
" " + myReader.GetDateTime(1).ToLongTimeString() + "." +
myReader.GetDateTime(1).Millisecond);
item.SubItems.Add(myReader.GetString(2));
item.SubItems.Add(myReader.GetString(3));
item.SubItems.Add(myReader.GetString(4));
item.SubItems.Add(myReader.GetString(5));
listView1.Items.Add(item);
}
//释放资源
myReader.Close();
selectCMD.Dispose();
sqlDataAdapter1.Dispose();
}
/*
/// <summary>
/// 日志的删除 需引入时间做为参量
/// 原先可以完成对单条记录的删除
/// </summary>
public void logKill(string dt, string hd, string tp, string dr)
{
string logDel ="delete from LogDesc where datetime = CONVERT(datetime,'"+dt+
"') and handle='"+hd+"'"+
" and type='"+tp+"' and descr='"+dr+"'";
SqlCommand logCmd = new SqlCommand(logDel,conn);
try
{
logCmd.ExecuteNonQuery();
}
catch (System.Exception e)
{
Console.WriteLine(e.Message);
}
//释放资源
logCmd.Dispose();
}
*/
/// <summary>
/// 退出系统
/// </summary>
/// <param name="sender">无参</param>
/// <param name="e">无参</param>
private void button4_Click(object sender, System.EventArgs e)
{
conn.Close();
Application.Exit();
}
/// <summary>
/// 初始化参量
/// </summary>
/// <param name="sender">无参</param>
/// <param name="e">无参</param>
private void Form1_Load(object sender, System.EventArgs e)
{
//*********************************************************************************
conn.Open();
//*********************************************************************************
this.find("","","","","");
//---------------------------------------------------------------------------------
//初始化时间的combobox内容
string logComb1="select distinct CONVERT(char(10),logdatetime,102) from Log";
SqlCommand selectCMD = new SqlCommand (logComb1,conn);
//创建并初始化SqlCommand对象
SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter();
sqlDataAdapter1.SelectCommand = selectCMD;
SqlDataReader myReader = selectCMD.ExecuteReader();
while(myReader.Read())
{
comboBox1.Items.Add(myReader.GetString(0));
comboBox2.Items.Add(myReader.GetString(0));
}
//释放资源
myReader.Close();
selectCMD.Dispose();
sqlDataAdapter1.Dispose();
//---------------------------------------------------------------------------------
//初始化操作员的combobox内容
string logComb3="select distinct loguser from Log";
selectCMD = new SqlCommand (logComb3,conn);
//创建并初始化SqlCommand对象
sqlDataAdapter1 = new SqlDataAdapter();
sqlDataAdapter1.SelectCommand = selectCMD;
myReader = selectCMD.ExecuteReader();
while(myReader.Read())
{
comboBox3.Items.Add(myReader.GetString(0));
}
//释放资源
myReader.Close();
selectCMD.Dispose();
sqlDataAdapter1.Dispose();
//---------------------------------------------------------------------------------
//初始化类型的combobox内容
string logComb4="select distinct logtype from Log";
selectCMD = new SqlCommand (logComb4,conn);
//创建并初始化SqlCommand对象
sqlDataAdapter1 = new SqlDataAdapter();
sqlDataAdapter1.SelectCommand = selectCMD;
myReader = selectCMD.ExecuteReader();
while(myReader.Read())
{
comboBox4.Items.Add(myReader.GetString(0));
}
//释放资源
myReader.Close();
selectCMD.Dispose();
sqlDataAdapter1.Dispose();
//---------------------------------------------------------------------------------
//初始化模块的combobox内容
string logComb5="select distinct logmodule from Log where logmodule!=''";
selectCMD = new SqlCommand (logComb5,conn);
//创建并初始化SqlCommand对象
sqlDataAdapter1 = new SqlDataAdapter();
sqlDataAdapter1.SelectCommand = selectCMD;
myReader = selectCMD.ExecuteReader();
while(myReader.Read())
{
comboBox5.Items.Add(myReader.GetString(0));
}
//释放资源
myReader.Close();
selectCMD.Dispose();
sqlDataAdapter1.Dispose();
}
/// <summary>
/// 单击listview在下方的textbox中显示出该条记录的详细信息
/// </summary>
/// <param name="sender">无参</param>
/// <param name="e">无参</param>
private void listView1_SelectedIndexChanged(object sender, System.EventArgs e)
{
int selectCount = listView1.SelectedItems.Count;
if (selectCount !=0)
{
string id="",dt="",us="",tp="",md="",ds="";
foreach(ListViewItem lvi in listView1.SelectedItems)
{
id=lvi.SubItems[0].Text;
dt=lvi.SubItems[1].Text;
us=lvi.SubItems[2].Text;
tp=lvi.SubItems[3].Text;
md=lvi.SubItems[4].Text;
ds=lvi.SubItems[5].Text;
textBox1.Text="LOG_ID:"+id+"\r\n时间:"+dt+"\r\n操作员:"+us+"\r\n类型:"+
tp+"\r\n模块:"+md+"\r\n描述:"+ds;
}
}
}
}
}
如果一句解释都没有呢,确实看着不太方便。但是文中确实没有什么难点,只是个个功能的简单累加,
以后使用的时候一旦忘了,拿出来看看,确实会方便些。
尽以本文献给那些,入门的,入门很就忽然忘了的,随便看看的,热爱编程的战友们。