C# 将文本文件行划分为数据库的两个不同表
很抱歉,如果我昨天不清楚的话,我想做的是逐行获取一个文本文件,并根据该行开头的内容决定将其发送到哪个表。我在寻找将以单词或“[”开头的某些行发送到某个表的最佳(最有效)方式时遇到问题。这是我正在加载的文本文件的一个示例,它是日志,异常、消息和源可以在文本文件的任何位置。以“[”开头的行转到(LogTable)并以“[”开头的行“Exception”将被设置为(ExceptionTable),但每个“Exception”都连接到上一个日志行,因此我也在寻找一种方法来连接这两个,以便它们可以在数据库中链接。我尝试循环遍历文件中的每一行,确定它需要使用linq查询到哪个表,并将更改保存到mySQL数据库C# 将文本文件行划分为数据库的两个不同表,c#,arrays,linq,text-files,C#,Arrays,Linq,Text Files,很抱歉,如果我昨天不清楚的话,我想做的是逐行获取一个文本文件,并根据该行开头的内容决定将其发送到哪个表。我在寻找将以单词或“[”开头的某些行发送到某个表的最佳(最有效)方式时遇到问题。这是我正在加载的文本文件的一个示例,它是日志,异常、消息和源可以在文本文件的任何位置。以“[”开头的行转到(LogTable)并以“[”开头的行“Exception”将被设置为(ExceptionTable),但每个“Exception”都连接到上一个日志行,因此我也在寻找一种方法来连接这两个,以便它们可以在数据库
[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”循环,因此当它到达“Exception”时:“行,它抛出了一个异常。有没有比我在这里做的更有效的方法?我还没有找到另一种方法?再次感谢
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();
}
foreach(文件路径中的字符串)
{
string[]lines=System.IO.File.ReadAllLines;
对于(int i=0;i
使用SSI进行集成,这将更容易,并有助于拆分文本文件
您先按行拆分,然后按“:”,在我看来,您的异常跨越了3行,因此在lines数组中有3个不同的值,因此您的第二次拆分无法按预期进行 实现我认为您正试图实现的目标的一种方法是遍历lines数组,然后在遇到这些情况时处理它们,如下所示:
foreach (string s in filePaths) {
//goes through each line in each file
string[] lines = System.IO.File.ReadAllLines(s);
for (int i=0;i < lines.Length; i++)
{
if (line[i].Contains("Exception:"))
{
// Handle line[i] as Exception, line[i+1] as Message, line[i+2] as Source
}
else if (line[i].Contains("["))
{
// Same as you had before
}
}
// same as before
foreach(文件路径中的字符串){
//遍历每个文件中的每一行
string[]lines=System.IO.File.ReadAllLines;
对于(int i=0;i
}以下是我认为您正在尝试做的事情……以一种比for循环更为关键的方式 注意,这个例子是在LinqPad(LinqPad.com)中运行的,我推荐它使用linq代码。它给出了预期的结果
void Main()
{
string [] lines = {
"[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]"
};
var exceptions = lines.Where(l => l.Contains("Exception:"))
.Select(line =>
{
var items = line.Split(':');
var result = new
{
OlogException = items.ElementAtOrDefault(1),
OlogMessage = items.ElementAtOrDefault(2),
OlogSource = items.ElementAtOrDefault(3),
OlogServerStackTrace = items.ElementAtOrDefault(4)
};
return result;
});
var groups = lines.Where(l => l.StartsWith("["))
.Select(line =>
{
var items = line.Split('[', ']', '(', ')');
var result = new
{
OlogDate = items.ElementAtOrDefault(1),
OlogLevel = items.ElementAtOrDefault(3),
OlogLogger = items.ElementAtOrDefault(5),
OlogThread = items.ElementAtOrDefault(7),
OlogProperty = items.ElementAtOrDefault(9),
OlogMethod = items.ElementAtOrDefault(11),
OlogException = items.ElementAtOrDefault(13)
};
return result;
});
exceptions.Dump();
groups.Dump();
}
这是不起作用的代码。
它怎么不起作用?它不起作用,因为他没有为每一行循环。我发现了两个错误,一个是在(.Select)处的异常查询(错误:“bool”不包含“Select”的定义),另一个是在(.Filter)处的组查询处(错误:“System.Array”不包含“Filter”的定义)只是想知道为什么它会给我这样的错误。Thanks@SamanthaIrenSweeney-好的,修好了,我应该是你