对不起,如果我不清楚酵母日,我要做的是逐行获取一个文本文件,并根据行开头的内容确定将其发送到哪个表。我在寻找以单词或“[”开头的某些行到某个表的最佳(最有效)方式时遇到问题。这是我正在加载的文本文件的示例,它是日志,异常、消息和源可以在文本文件中的任何位置。以“[”开头的行转到(LogTable),以“Exception”开头的行将转到(ExceptionTable),但是每个“Exception”都连接到前一个 Log 行,所以我也在寻找一种方法连接两者,以便它们可以在数据库中链接。
[2012-07-05 00:01:07,008] [INFO ] [MessageManager] [3780] () [] [Starting ProcessNewMessageEvent]
[2012-07-05 00:01:07,008] [INFO ] [MessageManager] [3780] () [] [Method: RegValue]
[2012-07-05 00:01:07,008] [DEBUG] [MessageManager] [3780] () [] [reg: InstallPath]
Exception: System.ServiceModel.EndpointNotFoundException
Message: There was no endpoint listening at that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
Source: mscorlib
[2012-07-04 23:55:59,598] [INFO ] [MessageManager] [6616] () [] [Method: RegValue]
[2012-07-04 23:55:59,598] [DEBUG] [MessageManager] [6616] () [] [reg: InstallPath]
这段代码没有循环,它将执行第一个循环,然后它在最后一部分被“grougquery”查询捕获,并且不会循环整个“for”循环,所以当它到达“异常:”行时它会抛出一个例外。有没有比我在这里做的更有效的方法?我还没有找到另一种方式?再次感谢
foreach (string s in filePaths)
{
string[] lines = System.IO.File.ReadAllLines(s);
for (int i = 0; i < lines.Length; i++)
{
if (lines[i].Contains("Exception:"))
{
var exQuery = from exMessage in lines
let logRecord = exMessage.Split(':')
select new ExTable()
{
ExlogException = logRecord[i],
ExlogMessage = logRecord[i + 1],
ExlogSource = logRecord[i + 2],
};
foreach (var item in exQuery)
{
exception ex = new exception();
ex.LogException = item.ExlogException;
ex.LogMessage = item.ExlogMessage;
ex.LogSource = item.ExlogSource;
ex.LogServerStackTrace = item.ExlogServerStackTrace;
ex.LogExRethrown = item.ExlogRethrown;
}
}
else if (lines[i].Contains("["))
{
var groupQuery = from date in lines
let logRecord = date.Split('[', ']', '(', ')')
select new OLog()
{
OlogDate = logRecord[1],
OlogLevel = logRecord[3],
OlogLogger = logRecord[5],
OlogThread = logRecord[7],
OlogProperty = logRecord[9],
OlogMethod = logRecord[11],
OlogException = logRecord[12],
};
foreach (var item in groupQuery)
{
temp temp = new temp();
temp.logDate = item.OlogDate;
temp.logLevel = item.OlogLevel;
temp.logLogger = item.OlogLogger;
temp.logThread = item.OlogThread;
temp.logProperty = item.OlogProperty;
temp.logMethod = item.OlogMethod;
temp.logException = item.OlogException;
logEntity.temps.AddObject(temp);
}
}
}
logEntity.SaveChanges();
}