IIS6.0記錄檔分析代碼_3線程讀取檔案到資料庫

來源:互聯網
上載者:User

 IIS6.0記錄檔分析代碼_3線程讀取檔案到資料庫(已經測試),只是欠缺將記錄檔批量入庫.定義一個數組.隱藏檔列表.按順序讀取檔案到資料庫中就好了.複習了線程操作.數組控制.檔案訪問,資料庫操作等等關鍵技術.

 //針對批量記錄檔處理思路:
   //方法一:單線程單檔案處理
   //1.選擇記錄檔所在目錄,將所有的記錄檔列表名稱,處理結果狀態,當前處理狀態存入資料表中
   //2.開啟單線程日誌列表檔案資料表中讀取檔案名稱,將所有的日誌欄位名稱定義到數組中,按讀取行文字檔中日誌相關記錄行數分段將讀取的將對應的全部欄位內容寫入到資料表中並更新
   //3.迴圈讀取該檔案日誌資料直到完成,將處理結果寫入到資料庫中   
   //方法二單線程多檔案處理
   //1.選擇記錄檔目錄,遍曆目錄下所有的記錄檔(除開今天的),將所有檔案路徑寫入到數組中
   //2.開啟線程迴圈根據傳入參數(單獨的記錄檔路徑)將日誌記錄寫到資料庫表中
   //3.線程讀取記錄檔結束,返回結果變數,繼續從數組中擷取下一條記錄檔資料,迴圈直到完成全部記錄檔的資料讀取
   //方法三單線程多檔案處理
   //1.開啟對話方塊選擇要讀取的記錄檔,根據所在目錄名稱的區分不同IIS網站.
   //2.開啟單線程依次讀取記錄檔內容到資料庫中
   //3.線程讀取完成.停留一段時間後繼續讀取.直到將所有的記錄檔內容寫入資料庫完成.
   //方法四
   //1.選擇記錄檔列表,將所有記錄檔備份處理成固定格式的文字檔,將文字檔作為資料來源,按資料庫定義的欄位名成讀入到資料表中,測試處理記錄檔大小3.380M.日誌行20434,檔案
   //備忘:開啟線程作用,可以顯示當前Tlog的進度,可以防止程式運行時候出現“假死”或者“卡死”現象。
   //預先處理記錄檔資料成有效格式的文字檔作用可以減少數組跟資料判斷的時間記憶體支出現象。
  
//添加資料庫檔案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,"錯誤");
   }
  } 
  //定義類代碼結束

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.