首页  编辑  

大日志文件的检索

Tags: /C#/文件目录操作/   Date Created:
//
//
//<166>Mar 31 2007 23:38:50: %PIX-6-302013: Built outbound TCP connection 731528465 for outside:62.241.53.2/443 (62.241.53.2/443) to inside:10.65.160.105/2918 (61.167.117.238/35049)
//
//<167>Mar 31 2007 23:38:50: %PIX-7-710005: UDP request discarded from 10.65.156.20/137 to inside:10.65.255.255/netbios-ns
//
string vFileName = @"C:\temp\sunday2007-04-01.log" ; //
DateTime vDateTime = DateTime . Parse( "Apr 01 2007 01:09:25" ); //
byte [] vBuffer = new byte[ 0x1000 ]; //
int vReadLength ; //
long vCurrPostion ; //
long vBeginPostion ; //
long vEndPostion ; //
FileStream vFileStream = new FileStream (vFileName, FileMode . Open, FileAccess . Read);
vBeginPostion = 0 ;
vEndPostion = vFileStream . Length;
while ( true )
{
   vCurrPostion = vBeginPostion + (vEndPostion - vBeginPostion) / 2 ; //
   vFileStream . Seek(vCurrPostion, SeekOrigin . Begin);
   vReadLength = vFileStream . Read(vBuffer, 0 , vBuffer . Length);
    string vText = Encoding . ASCII . GetString(vBuffer, 0 , vReadLength);
    Match vMatch = Regex . Match(vText,
        @"(\r\n)?<\d+>(?<datetime>\w+ \d+ \d+ \d+:\d+:\d+):" );
    if ( ! vMatch . Success) break ; //
    DateTime vTempTime = DateTime . Parse(vMatch . Result( "${datetime}" ));
    if (vTempTime == vDateTime)
   {
       vBeginPostion = vCurrPostion;
       vEndPostion = vCurrPostion;
   }
    else if (vDateTime > vTempTime)
   {
       vBeginPostion = vCurrPostion; //
   }
    else
   {
       vEndPostion = vCurrPostion; //
   }
    if (vEndPostion - vBeginPostion < 0x1000 ) break ;
}
vCurrPostion = Math . Min(vBeginPostion, vEndPostion); //
//
string vTemp = string . Empty; //
vBeginPostion = Math . Max(vCurrPostion - 0x1000 , 0 );
vEndPostion = vBeginPostion + 0x1000 ;
while ( true)
{
    bool vLoop = false ; //
   vFileStream . Seek(vBeginPostion, SeekOrigin . Begin);
   vReadLength = vFileStream . Read(vBuffer, 0 , vBuffer . Length);
    string vText = Encoding . ASCII . GetString(vBuffer, 0 , vReadLength) + vTemp;
    MatchCollection vMatches = Regex . Matches(vText,
        @"(\r\n)?<\d+>(?<datetime>\w+ \d+ \d+ \d+:\d+:\d+):[^\r\n]+\r\n" );
    if (vMatches . Count <= 0 ) break;
    for ( int i = 0 ; i < vMatches . Count; i ++ )
   {
        DateTime vTempTime = DateTime . Parse(vMatches[i] . Result( "${datetime}" ));
        if (vTempTime == vDateTime)
       {      
            if (i == 0 && vBeginPostion > 0 )
           {
                //
                if (vBeginPostion - 0x1000 >= 0 )
               {
                   vTemp = vText . Substring( 0 , 180 );
                   vBeginPostion = vBeginPostion - 0x1000 ;
                   vLoop = true ;
               }
                else
               {
                   vTemp = string . Empty;
                   vBeginPostion = 0 ;
               }
           }
            Console . WriteLine(vMatches[i]);
       }
   }
    if ( ! vLoop)
   {
       vTemp = vText . Substring( Math . Max(vText . Length - 180 , 0 ));
        break ;
   }
}
//
while ( true)
{
    bool vLoop = false ; //
   vFileStream . Seek(vEndPostion, SeekOrigin . Begin);
   vReadLength = vFileStream . Read(vBuffer, 0 , vBuffer . Length);
    string vText = vTemp + Encoding . ASCII . GetString(vBuffer, 0 , vReadLength);
    MatchCollection vMatches = Regex . Matches(vText,
        @"(\r\n)?<\d+>(?<datetime>\w+ \d+ \d+ \d+:\d+:\d+):[^\r\n]+\r\n" );
    if (vMatches . Count <= 0 ) break ;
    for ( int i = 0 ; i < vMatches . Count; i ++ )
   {
        DateTime vTempTime = DateTime . Parse(vMatches[i] . Result( "${datetime}" ));
        if (vTempTime == vDateTime)
       {
            if (i == 0 && vEndPostion < vFileStream . Length)
           {
                //
                if (vEndPostion + 0x1000 <= vFileStream . Length - 1 )
               {
                   vTemp = vText . Substring( 0 , 180 );
                   vEndPostion = vEndPostion + 0x1000 ;
                   vLoop = true ;
               }
                else
               {
                   vTemp = vText . Substring( 0 , 180 );
                   vEndPostion = vFileStream . Length - vEndPostion;
               }
           }
            Console . WriteLine(vMatches[i]);
       }
   }
    if ( ! vLoop) break ;
}
vFileStream . Close();