c# - How to extract data based on timestamps in multiline log? -


i have logs have data in format below. question - lets search log 12:22 pm, how can extract information pertaining time-stamp , output it... there way can output 12:22 pm onwards until hit next time-stamp? thats want programmatically. appreciated. thank you!

12:22 pm     abc      def        ghi     abc 12:33 pm     abc     def 

the output above mentioned case -

   12:22 pm         abc          def            ghi         abc 

also please note: there multiple log entries same timestamp, example, there 2 or 3 or maybe more entries under same timestamp, i.e, 12:22 pm

you can write static function extract required log given timestamp below:

note : i'm not handling exception scenario's[like filenotfoundexception , other exceptions], please handle them per requirement.

 static string getlogsbytimestamp(string timestamp)     {         if (!string.isnullorempty(timestamp))         {             string filepath = @"f:\log.txt"; //your logfile path here             string format = "hh:mm tt";             timespan reqtimestamp = datetime.parseexact(timestamp.trim(),                         format, cultureinfo.invariantculture).timeofday;             stringbuilder selectivelogs = new stringbuilder(string.empty);             bool startreading = false;             foreach (var line in file.readlines(filepath))             {                 string[] arritems = line.split(new[] { ' ' },                              stringsplitoptions.removeemptyentries);                 datetime logdatetime = datetime.minvalue;                 bool isdatetimevalid = false;                 if (arritems != null && arritems.length > 1)                     datetime.tryparseexact(string.format("{0}{1}{2}",                          arritems[0].trim(), " ", arritems[1].trim()),                                          format, cultureinfo.invariantculture,                                         datetimestyles.none, out logdatetime);                 timespan logtimestamp = logdatetime.timeofday;                 if ((!startreading && logtimestamp == reqtimestamp) ||                                       (startreading && !isdatetimevalid))                     startreading = true;                 if (startreading && logtimestamp > reqtimestamp)                 {                     startreading = false;                     break; // no need continue when know u                             //reached greater timestamp required.                 }                 if (startreading)                     selectivelogs.appendline(line);             }             return selectivelogs.tostring();         }         else             return string.empty;     } 

you can use above static function below:

string requiredlog = getlogsbytimestamp("12:22 pm"); 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -