对于过去 6 周左右正在学习 c# 的人,我有一个有点困难的问题。
我有两个逗号分隔的文件,我将它们流式传输到一个数组中。第一个有一个列 d:m:yh:m:s。我需要将它与第二个文本文件匹配,逗号分隔,我需要将第二个文件中的 d:m:yh: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");
}