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
これは、約1ギガのログファイルの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();}