Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text.RegularExpressions;
- namespace TesterCon7
- {
- class OutputItem
- {
- public DateTime Date;
- public string IPPort;
- public string State;
- }
- class Program2
- {
- //nastaveni stavu PushButton
- static readonly String OkState = "OK";
- static readonly String FailState = "Nefunkcni";
- static readonly String UnknownState = "Nenalezen odpovidajici stav";
- static readonly string InputFile = "20170228_1.log";
- static readonly Regex[] parser = new Regex[]{
- new Regex(@"<(.*?)>: --- Open a new connection: ([0-9.]+) - ([0-9]+) -------", RegexOptions.Compiled),
- new Regex(@".*", RegexOptions.Compiled)
- };
- static readonly Dictionary<string, OutputItem> OutputStates = new Dictionary<string, OutputItem>();
- private static void ProcessLogRecord(Match[] parsed)
- {
- DateTime dt = DateTime.Parse(parsed[0].Groups[1].Value, System.Globalization.CultureInfo.GetCultureInfo("en-us"));
- if (dt > DateTime.Now.AddHours(-1000))
- {
- var item = new OutputItem();
- item.Date = dt;
- item.State = UnknownState;
- if (parsed[1].Value.Contains("succesfull")) item.State = OkState;
- else if (parsed[1].Value.Contains("failed")) item.State = FailState;
- item.IPPort = string.Format("{0}:{1}", parsed[0].Groups[2].Value, parsed[0].Groups[3].Value);
- if (OutputStates.ContainsKey(item.IPPort))
- {
- if (item.Date > OutputStates[item.IPPort].Date) OutputStates[item.IPPort] = item;
- }
- else
- {
- OutputStates[item.IPPort] = item;
- }
- }
- }
- static void Main(string[] args)
- {
- Match[] parsed = new Match[parser.Count()];
- int index = 0;
- try
- {
- foreach (var line in File.ReadLines(InputFile))
- {
- parsed[index] = parser[index].Match(line);
- if (!parsed[index++].Success) index = 0;
- if (index >= parsed.Length)
- {
- ProcessLogRecord(parsed);
- index = 0;
- }
- }
- Console.WriteLine("zacina vypis");
- foreach (var x in OutputStates)
- {
- Console.WriteLine(String.Format("ip:{0} {1} ... {2}", x.Value.IPPort, x.Value.Date, x.Value.State));
- }
- Console.WriteLine("konci vypis");
- }
- catch (Exception e)
- {
- Console.Error.WriteLine("Error: {0}", e);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement