//添加数据库文件IO引用
using System.IO;
using System.Text;
using System.Data;
using System.Data.SqlClient;
public class IISLogFile
{
private string LogSoftware;
private string LogVersion;
private string LogDate;
private string LogFields;
private string LogContent;
public IISLogFile()
{}
public IISLogFile(string lSoftware, string lVersion, string lDate,string lFields,string lContent)
{
LogSoftware=lSoftware;
LogVersion=lVersion;
LogDate=lDate;
LogFields=lFields;
LogContent=lContent;
}
public string lSoftware
{
get
{
return LogSoftware;
}
set
{
LogSoftware = value;
}
}
public string lVersion
{
get
{
return LogVersion;
}
set
{
LogVersion = value;
}
}
public string lDate
{
get
{
return LogDate;
}
set
{
LogDate = value;
}
}
public string lFields
{
get
{
return lFields;
}
set
{
LogFields = value;
}
}
public string lContent
{
get
{
return LogContent;
}
set
{
LogContent = value;
}
}
}
//IIS6.0日志文件分析代码_1生成访问字段记录到数组中,只能处理小体积的LOG文件.系统构思有些欠缺.
//比如文本文件读取到100行好象就CPU占用率很高.然后程序卡死.升级可考虑用文本文件分割方法设计.
//升级可考虑直接读一条记录马上写到数据里.
//窗体变量定义
/// <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;
}
//IIS6.0日志文件分析代码_2生成访问记录到文本文件:解决掉了上一方式中打开LOG文件按行读取反映慢,
//卡死现象.进一步升级方向;将文本文件的内容导入数据库中.将对比访问字段FIELDS内容名称.写入数据库对应字段中.
private void button3_Click(object sender, System.EventArgs e)
{
this.button3.Enabled=false;
this.richTextBox1.Text="";
//调用打开文件对话框获取要打开的日志文件Log文件,获取文件所在路径名称
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();
BinaryFileName=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中显示出内容
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();
while(strLine!=null)
{
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(9,strLine.Length-9));
}
else
{
MyContentArray.Add(strLine);
if(splitLineNumber!=LineSoftware)
{
MyLineArray.Add(LineContent);
splitLineNumber+=1;
}
LineContent+=1;
}
}
}
}
strLine=myStreamReader.ReadLine();
totalLogFileLine+=1;
}
myStreamReader.Close();//关闭此StreamReader对象
fs.Close();
//定义记录字段名称
//定义记录字段内容
ArrayList LogFieldsTitleArray=new ArrayList();
string strt=MyFieldsArray[0].ToString();
string[] tArray=strt.Split(' ');
int totalFieldsLength=tArray.Length;
string[,] LogArrayList=new string[LineContent,totalFieldsLength];
for(int i=0;i<LineContent;i++)
{
string strRecord=MyContentArray[i].ToString();
//this.richTextBox1.Text+="第"+i.ToString()+"行:"+strRecord+"/n";
string[] recordArray=strRecord.Split(' ');
int j=0;
foreach(string record in recordArray)
{
LogArrayList[i,j]=record.ToString();
j+=1;
}
}
//测试将数组中的记录字段内容写入文本文件
string outpath = BinaryFilePath.Insert(BinaryFilePath.LastIndexOf('.'), "-back");
FileStream aFile = new FileStream(outpath,FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(aFile);
for(int m=0;m<LineContent;m++)
{
for(int n=0;n<totalFieldsLength;n++)
{
sw.WriteLine(LogArrayList[m,n]);
}
}
sw.Close();
//清除掉所有数组内容
MySoftwareArray=null;
MyVersionArray=null;
MyDateArray=null;
MyFieldsArray=null;
MyContentArray=null;
MyLineArray=null;
LogFieldsTitleArray=null;
LogArrayList=null;
//定义记录字段名称
//定义记录字段内容
/*
* 单独分析一条记录实例
ArrayList LogFieldsTitleArray=new ArrayList();
ArrayList LogFieldsContentArray=new ArrayList();
string strt="#Fields: date time s-sitename s-ip cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs(User-Agent) sc-status sc-substatus sc-win32-status";
string[] tArray=strt.Substring(9,strt.Length-9).Split(' ');
foreach(string i in tArray)
{
LogFieldsTitleArray.Add(i.ToString());
}
//string strc="2007-06-16 00:14:19 W3SVC1 218.16.120.31 GET /ArticleShow.asp ID=276 80 - 128.194.135.94 IRLbot/3.0+(compatible;+MSIE+6.0;+http://irl.cs.tamu.edu/crawler) 200 0 0";
string strc="2007-06-16 00:20:55 W3SVC1 218.16.120.31 GET /robots.txt - 80 - 65.55.209.134 msnbot/1.0+(+http://search.msn.com/msnbot.htm) 404 0 2";
string[] cArray=strc.Split(' ');
foreach(string j in cArray)
{
LogFieldsContentArray.Add(j.ToString());
}
for(int k=0;k<LogFieldsTitleArray.Count;k++)
{
this.richTextBox1.Text+=LogFieldsTitleArray[k].ToString()+"字段内容是:" +LogFieldsContentArray[k].ToString()+ "/n";
}
*/
this.textBox2.Text=BinaryFilePath;
this.textBox3.Text=LineContent.ToString();
this.textBox4.Text=totalLogFileLine.ToString();
this.textBox5.Text=BinaryFileLength.ToString();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,"错误");
}
}
this.button3.Enabled=true;
}
//IIS6.0日志文件分析代码_3线程读取文件到数据库(已经测试),只是欠缺将日志文件批量入库.定义一个数组.存储文件列表.
//按顺序读取文件到数据库中就好了.复习了线程操作.数组控制.文件访问,数据库操作等等关键技术.
//针对批量日志文件处理思路:
//方法一:单线程单文件处理
//1.选择日志文件所在目录,将所有的日志文件列表名称,处理结果状态,当前处理状态存入数据表中
//2.开启单线程日志列表文件数据表中读取文件名,将所有的日志字段名称定义到数组中,按读取行文本文件中日志相关记录行数分段将读取的将对应的全部字段内容写入到数据表中并更新
//3.循环读取该文件日志数据直到完成,将处理结果写入到数据库中
//方法二单线程多文件处理
//1.选择日志文件目录,遍历目录下所有的日志文件(除开今天的),将所有文件路径写入到数组中
//2.开启线程循环根据传入参数(单独的日志文件路径)将日志记录写到数据库表中
//3.线程读取日志文件结束,返回结果变量,继续从数组中获取下一条日志文件数据,循环直到完成全部日志文件的数据读取
//方法三单线程多文件处理
//1.打开对话框选择要读取的日志文件,根据所在目录名称的区分不同IIS站点.
//2.开启单线程依次读取日志文件内容到数据库中
//3.线程读取完成.停留一段时间后继续读取.直到将所有的日志文件内容写入数据库完成.
//方法四
//1.选择日志文件列表,将所有日志文件备份处理成固定格式的文本文件,将文本文件作为数据源,按数据库定义的字段名成读入到数据表中,测试处理日志文件大小3.380M.日志行20434,文件
//备注:开启线程作用,可以显示当前日志处理的进度,可以防止程序运行时候出现“假死”或者“卡死”现象。
//预先处理日志文件数据成有效格式的文本文件作用可以减少数组跟数据判断的时间内存支出现象。
//添加数据库文件IO引用
using System.IO;
using System.Text;
using System.Data;
using System.Data.SqlClient;
//使用线程操作的引用
using System.Threading;
//按钮事件
private void button6_Click(object sender, System.EventArgs e)
{
//开始线程下载文件:稳定性能比较好,不出现卡死的情况,线程监督要继续突出显示
this.button6.Enabled=false;
IISLogFileClass myClass=new IISLogFileClass();
Thread myThread= new Thread(new ThreadStart(myClass.ReadFromLogFile));
myClass.LogFilePath=@"C:/WINDOWS/system32/LogFiles/W3SVC1/ex070412.log";
myThread.Start();
if(myThread.Join(50000))//主线程等待子线程的停止时间单位:毫秒
{
this.textBox2.Text=myClass.LogFilePath;
this.textBox3.Text=myClass.LineContent.ToString();
this.textBox4.Text=myClass.LineFile.ToString();
this.textBox5.Text=myClass.FileLength.ToString();
}
this.button6.Enabled=true;
}
//定义日志文件处理类.
public class IISLogFileClass
{
public string LogFilePath;//下载文件保存地址
public long FileLength;//定义线程返回传参数
public int LineSoftware=0;//定义线程返回传参数
public int LineVersion=0;//定义线程返回传参数
public int LineDate=0;//定义线程返回传参数
public int LineFields=0;//定义线程返回传参数
public int LineContent=0;//定义线程返回传参数
public int LineFile=0;//定义线程返回传参数
public void ReadFromLogFile()
{
//针对大日志文件处理思路:
//1.将所有的日志字段名称定义到数组中,将对应的全部字段内容写入到文本文件中
//2.将文本文件作为数据源,把所有字段的内容一条一条的作为记录写入到数据库中对应字段里
//定义要添加的字段数量
int totalFieldsLength=0;
//定义字段下标数组,数据库中的内容必须跟下面的数组中一致
ArrayList IntLogFieldsArray=new ArrayList();
try
{
//打开数据库连接
string strCon = "Initial Catalog='HMMISDATA';Server='192.168.1.250';User ID='XQF';Password='123456';Persist Security Info=True";
SqlConnection myConn = new SqlConnection ( strCon ) ;
myConn.Open();
DataSet tempDataSet=new DataSet();
SqlDataAdapter tempAdapter = new SqlDataAdapter("SELECT * FROM IISLogFiledsList WHERE 1=0", myConn);
SqlCommandBuilder tempBuilder=new SqlCommandBuilder(tempAdapter);
tempAdapter.Fill(tempDataSet);
FileStream fs = new FileStream(LogFilePath, FileMode.OpenOrCreate, FileAccess.Read); //打开日志文件
FileLength=fs.Length;
StreamReader myStreamReader=new StreamReader(fs);//使用StreamReader类来读取文件
myStreamReader.BaseStream.Seek(0,SeekOrigin.Begin);//循环从数据流中读取每一行,直到文件的最后一行
string strLine=myStreamReader.ReadLine();
ArrayList MySoftwareArray=new ArrayList();
ArrayList MyVersionArray=new ArrayList();
ArrayList MyDateArray=new ArrayList();
ArrayList MyFieldsArray=new ArrayList();
while(strLine!=null)
{
LineFile+=1;
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(9,strLine.Length-9));
if(LineFields==1)
{
//定义要添加的记录字段名称
string strt=MyFieldsArray[0].ToString();
string[] tArray=strt.Split(' ');
totalFieldsLength=tArray.Length;
for(int k=0;k<totalFieldsLength;k++)
{
switch(tArray[k].ToString())
{
case "date":IntLogFieldsArray.Add(1);break;
case "time":IntLogFieldsArray.Add(2);break;
case "c-ip":IntLogFieldsArray.Add(3);break;
case "cs-username":IntLogFieldsArray.Add(4);break;
case "s-sitename":IntLogFieldsArray.Add(5);break;
case "s-computername":IntLogFieldsArray.Add(6);break;
case "s-ip":IntLogFieldsArray.Add(7);break;
case "s-port":IntLogFieldsArray.Add(8);break;
case "cs-method":IntLogFieldsArray.Add(9);break;
case "cs-uri-stem":IntLogFieldsArray.Add(10);break;
case "cs-uri-query":IntLogFieldsArray.Add(11);break;
case "sc-status":IntLogFieldsArray.Add(12);break;
case "sc-substatus":IntLogFieldsArray.Add(13);break;
case "sc-win32-status":IntLogFieldsArray.Add(14);break;
case "sc-bytes":IntLogFieldsArray.Add(15);break;
case "cs-bytes":IntLogFieldsArray.Add(16);break;
case "cs-version":IntLogFieldsArray.Add(17);break;
case "cs-timetaken":IntLogFieldsArray.Add(18);break;
case "cs-host":IntLogFieldsArray.Add(19);break;
case "cs(User-Agent)":IntLogFieldsArray.Add(20);break;
case "cs-cookie":IntLogFieldsArray.Add(21);break;
case "cs-referer":IntLogFieldsArray.Add(22);break;
}
}
}
}
else
{
LineContent+=1;
string[] currentContentArray=strLine.Split(' ');
//循环将所有日志记录写入数据库中
DataRow tempDataRow = tempDataSet.Tables[0].NewRow();//定义新记录行
for(int n=0;n<totalFieldsLength-1;n++)
{
tempDataRow[Int32.Parse(IntLogFieldsArray[n].ToString())]=currentContentArray[n];
//Console.WriteLine(IntLogFieldsArray[n].ToString()+":"+LogArrayList[m,n]);
}
tempDataSet.Tables[0].Rows.Add(tempDataRow);//添加新记录行
}
}
}
}
strLine=myStreamReader.ReadLine();
}
myStreamReader.Close();//关闭此StreamReader对象
fs.Close();
tempAdapter.Update(tempDataSet);
myConn.Close ( ) ;//关闭连接
// 清除掉所有数组内容
MySoftwareArray=null;
MyVersionArray=null;
MyDateArray=null;
MyFieldsArray=null;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,"错误");
}
}
//定义类代码结束