보고서를 만들려고하는데 몇 줄에 String.IndexOf()
이라는 별난 결과가 나오는 경우 일부 로그 파일을 구문 분석하고 있습니다. 여기에 C# 사용. 가장 간단한 코드로 설명하기 쉽습니다. 예상String.indexOf는 문자열에 검색 문자열이 없으면 음수가 아닌 인덱스를 반환합니다.
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();}
멀티 스레딩을 사용하고 있습니까? 스 니펫이 잘못된 작동을 제공합니까? 실제 코드를 보여주세요. https://compilr.com/anri82/test/main.cs – Anri
나는 그것을 시도하고 예상대로 -1을 반환합니다. – SLaks
단일 스레드 콘솔 응용 프로그램 – user1792333