IIS6.0日志文件分析代码_1生成访问字段记录到数组中

本工具为IIS6.0日志文件分析软件,能够读取并解析日志文件,提取关键信息如软件版本、日期等,并将其记录到数据表中。目前仅适用于小型日志文件,存在CPU占用率高及程序卡顿等问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

IIS6.0日志文件分析代码_1生成访问字段记录到数组中,只能处理小体积的LOG文件.系统构思有些欠缺.比如文本文件读取到100行好象就CPU占用率很高.然后程序卡死.升级可考虑用文本文件分割方法设计.升级可考虑直接读一条记录马上写到数据里.

//添加数据库文件IO引用
using System.IO;
using System.Text;
using System.Data;
using System.Data.SqlClient;

//窗体变量定义

/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

private string BinaryFileType;
private string BinaryFilePath;
private int BinaryFileLength;
private int totalLogFileLine=0;
private static int splitLineNumber=0;

private void button1_Click(object sender, System.EventArgs e)
{
this.button1.Enabled=false;
//调用打开文件对话框获取要打开的文件WORD文件,RTF文件,文本文件路径名称
OpenFileDialog opd = new OpenFileDialog();
opd.InitialDirectory = @"C:/WINDOWS/system32/LogFiles";
opd.Filter = "日志文档(*.log)|*.log|Word文档(*.doc)|*.doc|文本文档(*.txt)|*.txt|RTF文档(*.rtf)|*.rtf|所有文档(*.*)|*.*";
opd.FilterIndex = 1;

if (opd.ShowDialog() == DialogResult.OK && opd.FileName.Length > 0)
{

try
{
BinaryFileType=opd.FileName.Substring(opd.FileName.LastIndexOf(".")+1).ToUpper();
BinaryFilePath=opd.FileName;
FileStream fs = new FileStream(opd.FileName, FileMode.OpenOrCreate, FileAccess.Read);
BinaryFileLength=(int)fs.Length;

StreamReader myStreamReader=new StreamReader(fs);//使用StreamReader类来读取文件
myStreamReader.BaseStream.Seek(0,SeekOrigin.Begin);//从数据流中读取每一行,直到文件的最后一行,并在richTextBox1中显示出内容

this.richTextBox1.Text="";
string strLine=myStreamReader.ReadLine();

int LineSoftware=0;
int LineVersion=0;
int LineDate=0;
int LineFields=0;
int LineContent=0;

ArrayList MySoftwareArray=new ArrayList();
ArrayList MyVersionArray=new ArrayList();
ArrayList MyDateArray=new ArrayList();
ArrayList MyFieldsArray=new ArrayList();
ArrayList MyContentArray=new ArrayList();
ArrayList MyLineArray=new ArrayList();

//定义日志数据表格
DataTable MyLogTable = new DataTable();
MyLogTable.Columns.Add(new DataColumn("LogSoftware",typeof(string)));
MyLogTable.Columns.Add(new DataColumn("LogVersion",typeof(string)));
MyLogTable.Columns.Add(new DataColumn("LogDate",typeof(string)));
MyLogTable.Columns.Add(new DataColumn("LogFields",typeof(string)));
MyLogTable.Columns.Add(new DataColumn("LogContent",typeof(string)));

while(strLine!=null)
{
this.richTextBox1.Text+= strLine+"/n";
if(strLine.Substring(0,10)=="#Software:")
{
LineSoftware+=1;
MySoftwareArray.Add(strLine.Substring(10,strLine.Length-10));
}
else
{
if(strLine.Substring(0,9)=="#Version:")
{
LineVersion+=1;
MyVersionArray.Add(strLine.Substring(9,strLine.Length-9));
}
else
{
if(strLine.Substring(0,6)=="#Date:")
{
LineDate+=1;
MyDateArray.Add(strLine.Substring(6,strLine.Length-6));
}
else
{
if(strLine.Substring(0,8)=="#Fields:")
{
LineFields+=1;
MyFieldsArray.Add(strLine.Substring(8,strLine.Length-8));
}
else
{
MyContentArray.Add(strLine);
DataRow newLogRow=MyLogTable.NewRow();
newLogRow["LogSoftware"]=MySoftwareArray[splitLineNumber].ToString();
newLogRow["LogVersion"]=MyVersionArray[splitLineNumber].ToString();
newLogRow["LogDate"]=MyDateArray[splitLineNumber].ToString();
newLogRow["LogFields"]=MyFieldsArray[splitLineNumber].ToString();
newLogRow["LogContent"]=MyContentArray[LineContent].ToString();
MyLogTable.Rows.Add(newLogRow);

if(splitLineNumber!=LineSoftware)
{
MyLineArray.Add(LineContent);
}
else
{
splitLineNumber+=1;
}

LineContent+=1;

//arr[LineContent]=new LogRsData(strLine, "Les Paul", LineContent);
}
}
}
}
strLine=myStreamReader.ReadLine();
totalLogFileLine+=1;
}

myStreamReader.Close();//关闭此StreamReader对象

fs.Close();
this.richTextBox1.Text+= ":"+ LineSoftware+":"+LineVersion+":"+LineDate+":"+LineFields+":"+LineContent+"/n";


this.dataGrid1.DataSource =MyLogTable;

this.textBox2.Text=BinaryFilePath;
this.textBox3.Text="0";
this.textBox4.Text=totalLogFileLine.ToString();
this.textBox5.Text=BinaryFileLength.ToString();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,"打开文件错误");
}

}
this.button1.Enabled=true;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值