c# - Read logfile with regex -
i need read added text logfile , extract parts of save in database (number > s3.20.140, start scan, scanid, scantime, result). need read following block:
[2015-06-23 13:45:01.768] . [2015-06-23 13:45:01.768] scan requested [2015-06-23 13:45:01.768] random selection - s3.20.140 3 - 3 [2015-06-23 13:45:01.768] sv_et_cmd_type.sv_et_cmd_scan: s3.20.140 [2015-06-23 13:45:01.784] notification: activity=scan_started scanid=14 [2015-06-23 13:45:07.884] scumsgclient: receive 235 rectangles [2015-06-23 13:45:07.884] total scan 14 time: - 6.1 sec [2015-06-23 13:45:07.915] hip detection result "objects detected" [2015-06-23 13:45:07.915] scan results ready. [2015-06-23 13:45:11.128] user cleared scan 14 [2015-06-23 13:45:11.128] .
this block same every scan, there other information in logfile not wanna process. best approach this?
try this
using system; using system.collections.generic; using system.linq; using system.text; using system.text.regularexpressions; using system.io; namespace consoleapplication1 { class program { const string filename = @"c:\temp\test.txt"; static void main(string[] args) { streamreader reader = new streamreader(filename); string inputline = ""; string pattern1 = @"^\[(?'datetime'[^\]]*)\](?'message'[^$]*)"; regex expr = new regex(pattern1, regexoptions.multiline); while ((inputline = reader.readline()) != null) { inputline = inputline.trim(); match match = expr.match(inputline); string x = match.groups["datetime"].value; datetime date = datetime.parse(match.groups["datetime"].value); string message = match.groups["message"].value; if (message.contains(":")) { string[] array = message.split(new char[] { ':' }); switch (array[0].trim()) { case "sv_et_cmd_type.sv_et_cmd_scan" : console.writeline("number : {0}", array[1].trim()); break; } } } console.readline(); } } }
Comments
Post a Comment