zoukankan      html  css  js  c++  java
  • IIS6.0日志文件分析代码_2生成访问记录到文本文件

    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;
      }

  • 相关阅读:
    php的错误处理机制
    php的自动加载函数spl_autoload_register和__autoload
    mysql日志配置
    01,易语言网页填表
    mac 的全文搜索
    react native 热更新
    码云的使用教程
    autolayout实现水平三等分
    mac命令行快捷键
    什么是延展性(Malleability,可鍛性)
  • 原文地址:https://www.cnblogs.com/xqf222/p/3306889.html
Copyright © 2011-2022 走看看