劉德軍
摘要:日志文件是記錄系統(tǒng)操作事件的記錄文件,操作系統(tǒng)有操作系統(tǒng)日志文件,數(shù)據(jù)庫系統(tǒng)有數(shù)據(jù)庫系統(tǒng)日志文件。系統(tǒng)日志文件是包含關(guān)于系統(tǒng)消息的文件,包括內(nèi)核、服務(wù)、在系統(tǒng)上運(yùn)行的應(yīng)用程序等。不同的日志文件記載不同的信息。該文針對(duì)醫(yī)院信息系統(tǒng)應(yīng)用程序中產(chǎn)生的日志文件長時(shí)間積累導(dǎo)致磁盤空間滿這一突出問題,提出特定的日志文件清除的解決方法。
關(guān)鍵詞:C#;日志;文件;清除
中圖分類號(hào):TP313 文獻(xiàn)標(biāo)識(shí)碼:A 文章編號(hào):1009-3044(2014)20-4728-02
C# Log File Removal Function
LIU De-jun
(Information Department, Sheyang County People's Hospital of Jiangsu ,Yancheng 224300,China)
Abstract: The log file is recorded file system operation event, operating system operating system log files, database system database, the system log file. The system log file contains a system message files, including kernel, service, an application running on the system. Different log files record different information. This paper aimed at the hospital information system in the application log file long time accumulation leads to the problem of disk space is full, puts forward solving methods specific log file cleared.
Key words: C#; log; file; clearance
我院信息系統(tǒng)在運(yùn)行過程,會(huì)對(duì)客戶端操作員的操作進(jìn)行日志文件記錄,隨著時(shí)間推移,日志文件越來越多,導(dǎo)致磁盤空間滿,程序運(yùn)行報(bào)錯(cuò)。考慮到工作站眾多,手動(dòng)清除不現(xiàn)實(shí),因此考慮通過程序讓工作站操作人員來完成清除工作,經(jīng)過多次測(cè)試,終于實(shí)現(xiàn)了日志文件清除功能。
1 需求
該功能的需求如下:1) 要清除的日志文件名稱含有日期格式(如:debugSql20131115.log、YB2014-02-18.txt等);2) 要保留近6個(gè)月的日志文件備查;3) 按指定格式的文件刪除。4) 因清除文件格一定且數(shù)量較多,所以系統(tǒng)要支持通配符(*)方式清除。4) 程序?qū)Ξ?dāng)前所在目錄進(jìn)行處理。
2 方法
通過使用C#的文件操作功能,對(duì)指定格式的文件名與程序所在目錄的文件進(jìn)行比對(duì),檢索出日志文件名中的日期在6個(gè)月前的文件,對(duì)檢索到的文件執(zhí)行刪除操作。
3 詳細(xì)步驟
3.1 建立C#應(yīng)用程序
建立界面如圖1,控件參數(shù)如表1的應(yīng)用程序界面(項(xiàng)目名稱為:日志清除工具)。
*_Speak.LOG
Err*.log
NH*.txt
printer*.log
YB*.txt\&]
3.2 編寫代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsApplication1
{public partial class Form1 : Form
{public Form1()
{InitializeComponent();}
private void button1_Click(object sender, EventArgs e)
{string Tdir;
string sFilename;
Tdir = System.IO.Directory.GetCurrentDirectory();
Int32 j;
j= 0;
this.toolStripStatusLabel1.Text="正在查找文件......";
for (Int32 i = 0; i < listBox1.Items.Count; i++) //遍歷文件列表
{sFilename = this.listBox1.Items[i].ToString();
DirectoryInfo di = new DirectoryInfo(Tdir);
FileInfo[] ff = di.GetFiles(sFilename); //獲取所有文件名
DateTime dt;
string dt2;
dt = DateTime.Today;
DateTime dt1 = dt.AddMonths(-6);
string dt4;
if (ff.Length != 0) //文件不為空
{foreach (FileInfo fi in ff) //遍歷目錄中的指定的文件名,可帶通配符*
{string tFilename;
switch (i) //格式為debugsql*.log,如debugsql20130101.log
{case 0:
tFilename = sFilename.Substring(8, 1).ToString();
if (tFilename == "*" && fi.ToString() != "debugSql.log")
{DateTime result = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(-6);
dt2 = result.ToString("yyyyMMdd");
dt4 = fi.Name.Substring(8, 8).ToString();
this.toolStripStatusLabel1.Text = "找到文件:" + fi + ",正在檢查條件......";
if (dt4.CompareTo(dt2) < 0)
{fi.Delete(); //執(zhí)行行刪除
this.toolStripStatusLabel1.Text = "正在刪除文件:" + fi + "......";}}
break;
case 1: //格式為:*_Speak.LOG,如-12-30_Speak.LOG
tFilename = sFilename.Substring(0, 1).ToString();
if (tFilename == "*")
{DateTime result = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(-6);
dt2 = result.ToString("yyyy-MM-dd");
dt4 = fi.Name.Substring(0, 10).ToString();
this.toolStripStatusLabel1.Text = "找到文件:" + fi + ",正在檢查條件......";
if (dt4.CompareTo(dt2) < 0)
{fi.Delete();
this.toolStripStatusLabel1.Text = "正在刪除文件:" + fi + "......";}}
break;
//case 2:...其余文件名處理方法同上,此處略
}
j++;}}}
this.toolStripStatusLabel2.Text = "處理結(jié)束......";}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{MessageBox.Show(this.listBox1.SelectedItem.ToString());}
private void Form1_Load(object sender, EventArgs e)
{this.Text=this.Text+"(當(dāng)前目錄為:"+System.IO.Directory.GetCurrentDirectory()+ "\\)";
}
private void button2_Click(object sender, EventArgs e)
{this.Close();}}}
3.3 生成可執(zhí)行文件
在C#編譯生成名稱為“日志清除工具.exe”的可執(zhí)行文件,該文件位于當(dāng)前工程目錄下“日志清除工具\bin\Debug\”,將該文件復(fù)制到相應(yīng)需清除日志文件的目錄下,在需要清除時(shí)執(zhí)行一下即可。
4 體會(huì)與思考
實(shí)現(xiàn)日志清除功能的體會(huì)與如下:1、本文只是針對(duì)當(dāng)前目錄下的指定格式的日志文件實(shí)現(xiàn)清除功能,不對(duì)當(dāng)前目錄下的子目錄下的日志文件進(jìn)行搜索及清除;2、只是對(duì)半年前的日志文件進(jìn)行清除,如需可變時(shí)間的清除時(shí),可在DateTime result = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(-6) 一行中將“6”用參數(shù)代替,根據(jù)需要通過界面或者配置文件傳入即可4、本程序開發(fā)時(shí)只針對(duì)單機(jī)環(huán)境,獲取的當(dāng)前時(shí)間為本機(jī)時(shí)間,在網(wǎng)絡(luò)環(huán)境下需獲取服務(wù)器時(shí)間作為當(dāng)前時(shí)間;5、可通過修改實(shí)現(xiàn)對(duì)可變文件類型的清除功能及對(duì)子目錄下日志文件進(jìn)行搜索及清除,筆者可以試著做做。此功能在Windows XP SP3 + Microsoft Visual Studio 2005 環(huán)境下調(diào)試通過。
參考文獻(xiàn):
[1] 明日科技,C#從入門到精通[M].3版.北京:清華大學(xué)出版社,2012.
[2] Christian Nagel,Bill Evjen.C#高級(jí)編程[M]. 李銘,譯.8版.北京:清華大學(xué)出版社,2013.
電腦知識(shí)與技術(shù)2014年20期