2
對於在過去的6週中學習c#的人來說,我有點困難。C#匹配IIS和Oracle服務器日誌循環
我有兩個逗號分隔的文件,我流入一個數組。第一個列有d:m:y h:m:s。我需要將它與第二個文本文件(逗號分隔)匹配,我需要將第二個文件中的d:m:y h:m:s與第一個文件相匹配。唯一的問題是,秒必須匹配相同的秒或第二+ 1,我需要打印行到第三個文件。
例如,我可以有:
File 1 File 2
d:m:y h:m:s d:m:y h:m:s,UUID - 1
d:m:y h:m:s d:m:y h:m:s+1,UUID - 2
d:m:y h:m:s d:m:y h:m:s+1,UUID - 3
另外,UUID必須是唯一的1:1的關係。 文件1是一個IIS服務器日誌。 文件2是一個Oracle服務器日誌。
最終目標是匹配服務器日誌之間的時間差異以找出哪些進程花費的時間太長。
這是否有意義?代碼如下。
private void btnRun_Click(object sender, EventArgs e)
{
// open server file
StreamReader readServerFile = new StreamReader(strServerFile);
readServerFile.ReadLine();
// open message file
StreamReader readMessageLog = new StreamReader(strMessageLog);
readMessageLog.ReadLine();
// write line to file - false does not append
StreamWriter writer = new StreamWriter(strSavedLog, true);
string strHeader = "Ast Timestamp,Method,Service,IP Address,Time Taken, Time Taken(s),Log Filename,UUID,Request Created,Request Type,Response Created,Result Description,Sender,Time Span,Resp-Req(s)";
writer.WriteLine(strHeader);
// loop through server file and copy to save log
foreach (string strServerLine in File.ReadLines(strServerFile))
{
string[] ServerLogWord = strServerLine.Split(',');
if (ServerLogWord[0] != "Ast Timestamp")
{
// extract - server log "Ast Timestamp" convert to date
string strServerLogAstTimestamp = ServerLogWord[0];
string AstServerLogTimestamp = strServerLogAstTimestamp.ToString();
DateTime ServerFileLogDateTime = DateTime.ParseExact(AstServerLogTimestamp, "dd/MM/yyyy hh:mm:ss tt", null);
// convert time taken in MS to Sec
int timeTaken = Convert.ToInt32(ServerLogWord[4]);
int timeTakenSeconds = timeTaken/1000;
// loop through message file
foreach (string strMessageLine in File.ReadLines(strMessageLog))
{
string[] MessageLogWord = strMessageLine.Split(',');
if (MessageLogWord[0] != "ID")
{
// extract - message log - "requestcreated" convert to date
string strMessageLogRequestCreated = MessageLogWord[2];
string AstMessageLogRequestCreated = strMessageLogRequestCreated.ToString();
DateTime MessageLogDateTimeRequestCreated = Convert.ToDateTime(AstMessageLogRequestCreated);
// extract - message log - "responsecreated" convert to date
string strMessageLogResponseCreated = MessageLogWord[5];
string AstMessageLogResponseCreated = strMessageLogResponseCreated.ToString();
DateTime MessageLogDateTimeResponseCreated = Convert.ToDateTime(AstMessageLogResponseCreated);
// time calculations
// calculate timespan
TimeSpan ts = MessageLogDateTimeResponseCreated.Subtract(ServerFileLogDateTime);
// ***** Change x to modify the time range, must add + or -: ----- 'AddSeconds(x)'
DateTime desiredTime = ServerFileLogDateTime.AddSeconds(1);
// calculate the time
TimeSpan respReqTimeTaken = MessageLogDateTimeResponseCreated.Subtract(MessageLogDateTimeRequestCreated);
// if timestamps match
if (MessageLogDateTimeResponseCreated == ServerFileLogDateTime && MessageLogDateTimeRequestCreated <= desiredTime)
{
if (ServerFileLogDateTime)
{
string SavedLog = string.Join(",", AstServerLogTimestamp, ServerLogWord[1], ServerLogWord[2], ServerLogWord[3], ServerLogWord[4], timeTakenSeconds, ServerLogWord[5], MessageLogWord[1], AstMessageLogRequestCreated, MessageLogWord[3], AstMessageLogResponseCreated, MessageLogWord[7], MessageLogWord[9], ts, respReqTimeTaken);
// write line to save log
writer.WriteLine(SavedLog);
}
}
}
}
}
}
writer.Close();
MessageBox.Show("Complete");
}
謝謝埃裏克。但是,我將如何與Oracle日誌相匹配 – Deke
您的目的是「找出哪些進程花費的時間太長」。只需檢查IIS日誌,就可以找到「哪個請求需要大部分時間才能運行」。不建議在IIS和Oracle日誌之間匹配時間戳。 –
嗨埃裏克我很感激幫助。我正在嘗試爲客戶端屏幕上的某些Web服務找到底部,或者完全崩潰。我希望能夠完成的是找出哪些進程正在崩潰/掛起系統。它是一個與IIS,Oracle和ArcSDE通信的ASP網站。因此試圖比較服務器日誌。 – Deke