Advertisement
Nargon

Untitled

Mar 10th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.95 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6.  
  7. namespace TesterCon7
  8. {
  9.     class OutputItem
  10.     {
  11.         public DateTime Date;
  12.         public string IPPort;
  13.         public string State;
  14.     }
  15.  
  16.     class Program2
  17.     {
  18.         //nastaveni stavu PushButton
  19.         static readonly String OkState = "OK";
  20.         static readonly String FailState = "Nefunkcni";
  21.         static readonly String UnknownState = "Nenalezen odpovidajici stav";
  22.  
  23.         static readonly string InputFile = "20170228_1.log";
  24.  
  25.         static readonly Regex[] parser = new Regex[]{
  26.             new Regex(@"<(.*?)>: --- Open a new connection: ([0-9.]+) - ([0-9]+) -------", RegexOptions.Compiled),
  27.             new Regex(@".*", RegexOptions.Compiled)
  28.         };
  29.  
  30.         static readonly Dictionary<string, OutputItem> OutputStates = new Dictionary<string, OutputItem>();
  31.  
  32.         private static void ProcessLogRecord(Match[] parsed)
  33.         {
  34.             DateTime dt = DateTime.Parse(parsed[0].Groups[1].Value, System.Globalization.CultureInfo.GetCultureInfo("en-us"));
  35.             if (dt > DateTime.Now.AddHours(-1000))
  36.             {
  37.                 var item = new OutputItem();
  38.                 item.Date = dt;
  39.  
  40.                 item.State = UnknownState;
  41.                 if (parsed[1].Value.Contains("succesfull")) item.State = OkState;
  42.                 else if (parsed[1].Value.Contains("failed")) item.State = FailState;
  43.  
  44.                 item.IPPort = string.Format("{0}:{1}", parsed[0].Groups[2].Value, parsed[0].Groups[3].Value);
  45.  
  46.                 if (OutputStates.ContainsKey(item.IPPort))
  47.                 {
  48.                     if (item.Date > OutputStates[item.IPPort].Date) OutputStates[item.IPPort] = item;
  49.                 }
  50.                 else
  51.                 {
  52.                     OutputStates[item.IPPort] = item;
  53.                 }
  54.             }
  55.         }
  56.  
  57.         static void Main(string[] args)
  58.         {
  59.             Match[] parsed = new Match[parser.Count()];
  60.             int index = 0;
  61.  
  62.             try
  63.             {
  64.                 foreach (var line in File.ReadLines(InputFile))
  65.                 {
  66.                     parsed[index] = parser[index].Match(line);
  67.  
  68.                     if (!parsed[index++].Success) index = 0;
  69.                     if (index >= parsed.Length)
  70.                     {
  71.                         ProcessLogRecord(parsed);
  72.                         index = 0;
  73.                     }
  74.                 }
  75.  
  76.                 Console.WriteLine("zacina vypis");
  77.                 foreach (var x in OutputStates)
  78.                 {
  79.                     Console.WriteLine(String.Format("ip:{0} {1} ... {2}", x.Value.IPPort, x.Value.Date, x.Value.State));
  80.                 }
  81.                 Console.WriteLine("konci vypis");
  82.  
  83.             }
  84.             catch (Exception e)
  85.             {
  86.                 Console.Error.WriteLine("Error: {0}", e);
  87.             }
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement