我正在解析一些日志文件以生成报告,但String.IndexOf()
在几行中得到了奇怪的结果。在这里使用 C#。用一些代码最容易解释。
String line = "Jun 29 14:34:19 localhost axis2_http_server: CEF:0|AOPTIX|IRIS|4.1.0.1664.2839|AD214030000301-2610017|114|SDK_ACCESS|4|time=1340980459 comp=10 compinfo=CAPTURE from=8 result=0 user=Admin thread=1305:1962 msg=ation=0.00, faceColor=11.49 HR Face is ICAO compliant, inter-pupil distance=140.00. No match found on LEFT eye (database is empty). LEFT_uid=-1, blacklist=0 No match found on RIGHT eye (database is empty). RIGHT_uid=-1, blacklist=0 CAPTURE successfully completed - SOAP response sent. ";
int ctIndex = line.IndexOf("CaptureTime=");
return ctIndex;
预期:-1
实际:11
这只发生在大约一演出的日志文件中的 2 行。
实际实现的方法
private TimeSpan parseDuration(string line)
{
int ctIndex = line.IndexOf("CaptureTime=");
ctIndex = ctIndex + "CaptureTime=".Length;
int endIndex = line.IndexOf(" ", ctIndex);
string sDuration = line.Substring(ctIndex, endIndex - ctIndex);
long duration;
if (!long.TryParse(sDuration, out duration))
{
Console.WriteLine("Error Parsing Duration");
return TimeSpan.Zero;
}
duration *= 1000;
TimeSpan tsDuration = new TimeSpan(duration*1000);
return tsDuration;
}
换行代码
try{
StreamReader sr = new StreamReader(FilePath);
string line = sr.ReadLine();
while(line != null)
{
TimeSpan ts = parseDuration(line);
line = sr.ReadLine();
}
catch(Exception ex){}
finally{sr.close();}