我正在尝试创建一个程序,而且我似乎无法输出所有与应该已经在字典中编入索引的内容相匹配的内容。
这是整个代码:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text.RegularExpressions;
namespace LogParser
{
class Program
{
static void Main(string[] args)
{
Console.OutputEncoding = System.Text.Encoding.UTF8;
var counterLine = 0;
var counterTimeout = 0;
var line = string.Empty;
var previousLine = string.Empty;
var previousDt = DateTime.MaxValue;
var regexTID = new Regex(@"<\w{3}: \d{10}>");
var regex = new Regex(@"\d{2}:\d{2}:\d{2}\.\d{4}");
var TID = string.Empty;
try
{
var file = new System.IO.StreamReader(@"C:\\Users\\Jan\\Desktop\\api_fiter_sql.log"); // args[0] - ker nevemo kje bo datoteka, jo uporabnik sam doda
Console.WriteLine("V ozadju izpisujemo vse pomembne stvari, .txt dokumenta bosta vidna na vašem namizju.\n" +
"Profesionalno branje logov se začenja: (© Ascaldera D.O.O. - 2018)\n");
var writer = new StreamWriter(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "log_output.txt"), true);
var TIDwriter = new StreamWriter(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "TID_output.txt"), true);
long position = 0;
long previousPosition = 0;
var TupleList = new List<Tuple<long, long, string>>();
Dictionary<string, List<long>> TIDdict = new Dictionary<string, List<long>>(); // shranjujemo vse linije
while ((line = file.ReadLine()) != null)
{
counterLine++;
Match t = regexTID.Match(line);
if (t.Success)
{
//TIDwriter.WriteLine(TID);
TID = t.Value;
if (!TIDdict.ContainsKey(TID))
{
TIDdict.Add(TID, new List<long>());
}
TIDdict[TID].Add(position);
foreach (Match m in regex.Matches(line))
{
var dt = new DateTime();
if (DateTime.TryParseExact(m.Value, "HH:mm:ss.ffff", null, DateTimeStyles.None, out dt))
{
if ((dt - previousDt).TotalSeconds > 1)
{
counterTimeout++;
// Console.WriteLine(previousLine); // timeout
// Console.WriteLine(line + "\n\n\n"); // linija za timeoutom
writer.WriteLine(previousLine);
writer.WriteLine(line + Environment.NewLine);
TupleList.Add(new Tuple<long, long, string>(previousPosition, position, TID));
}
previousLine = line;
previousDt = dt;
previousPosition = position;
}
}
}
position += line.Length + Environment.NewLine.Length;
}
foreach (var TIDout in TupleList)
{
if ((TIDout.Item1 + TIDout.Item2 + TIDout.Item3).Equals(TIDdict[TID]))
{
Console.WriteLine(TIDout);
}
}
file.Close();
writer.Close();
TIDwriter.Close();
Console.WriteLine("\nBranje logov je končano. Prebrali smo: {0} vrstic ter izpisali " +
"{1} vrstic, kjer je bil timeout v datoteko.", counterLine, counterTimeout);
}
catch (Exception e)
{
Console.OpenStandardError();
Console.WriteLine(e.Message);
}
if (args.Length < 1)
{
Console.OpenStandardError();
Console.WriteLine("Uporaba: {0} LOG_DATOTEKA", AppDomain.CurrentDomain.FriendlyName);
Console.ReadKey();
return;
}
}
}
}
这是代码中应该输出.log文件中与字典中的内容匹配的每一行的部分。我假设我在尝试输出它时遇到了一些大错,因为它甚至没有输出像“成功!”这样的普通字符串。
foreach (var TIDout in TupleList)
{
if ((TIDout.Item1 + TIDout.Item2 + TIDout.Item3).Equals(TIDdict[TID]))
{
Console.WriteLine(TIDout);
}
}
非常感谢任何帮助。谢谢!
答案 0 :(得分:0)
控制输出到控制台的字典键(TID
)不能保证在预期时具有值。并且,如果它确实有值,则它将是文件中最后正则表达式匹配。
<强>更新强>
问题是你正确地进行了正则表达式匹配,因此永远不会填充TupleList。
Regex.Matches()返回一个MatchCollection(基本上是多个Match对象),你需要迭代它。
...
Match t = regexTID.Match(line);
if (t.Success)
{
//TIDwriter.WriteLine(TID);
TID = t.Value;
if (!TIDdict.ContainsKey(TID))
{
TIDdict.Add(TID, new List<long>());
}
TIDdict[TID].Add(position);
MatchCollection matches = Regex.Matches(line, @"\d{2}:\d{2}:\d{2}\.\d{4}");
// Use foreach-loop.
foreach (Match m in matches)
{
foreach (Capture capture in m.Captures)
{
var dt = new DateTime();
if (DateTime.TryParseExact(capture.Value, "HH:mm:ss.ffff", null, DateTimeStyles.None, out dt))
{
if ((dt - previousDt).TotalSeconds > 1)
{
counterTimeout++;
writer.WriteLine(previousLine);
writer.WriteLine(line + Environment.NewLine);
TupleList.Add(new Tuple<long, long, string>(previousPosition, position, TID));
}
previousLine = line;
previousDt = dt;
previousPosition = position;
}
}
}
}
...