我需要从文件中读取时间戳,然后回顾过去30分钟以查看是否显示关键字“CM失败”。这是一个日志文件,即使在应用程序运行时也会不断更新。有任何想法吗?下面提供的代码确实回顾了过去30年的情况,但我不确定它究竟是在什么时间看的。C#Winform从文件的文本中读取时间戳
TimeSpan ts = TimeSpan.FromMinutes(30);
//fake file which is opened using Notepad++
string temp = @"C:\Temp\efilelog.txt";
private void Form1_Load(object sender, EventArgs e)
{
string str = File.ReadAllText(temp);
Regex reg = new Regex("CM failed" + DateTime.Now.Subtract(ts));
Match mat = reg.Match(str);
// Get the creation time of a well-known directory.
//DateTime dt = File.GetLastWriteTime(file);
//Console.WriteLine("The last write time for this file was {0}.", dt, ts);
if (mat.Success)
{
//send email which I already have functional
}
this.Close();
}
}
这是字符串在日志文件中的样子:6/24/2013 6:36:25 AM 0 ProcessRecordFilingRequest中的异常:DexFlow.Framework.DXException:CM中的文档存储失败。 –
如果实际格式与当前格式不同,您可以调整正则表达式模式。或者使用'DateTime.ParseExact' –
这不会返回包含单词的任何内容。我错过了什么吗? –