0

对不起,如果我不清楚酵母日,我要做的是逐行获取一个文本文件,并根据行开头的内容确定将其发送到哪个表。我在寻找以单词或“[”开头的某些行到某个表的最佳(最有效)方式时遇到问题。这是我正在加载的文本文件的示例,它是日志,异常、消息和源可以在文本文件中的任何位置。以“[”开头的行转到(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();
               }
4

3 回答 3

0

使用 SSIS 进行集成,它会更容易,它可以帮助您拆分文本文件 http://social.technet.microsoft.com/wiki/contents/articles/3172.aspx

于 2012-08-15T21:34:37.110 回答
0

您按行拆分,然后按“:”拆分,在我看来,您的异常跨越 3 行,因此行数组中有 3 个不同的值,因此您的第二次拆分不会按预期工作。

完成我认为您要完成的事情的一种方法是遍历行数组,然后在遇到这些情况时处理它们,如下所示:

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

}

于 2012-08-15T21:59:12.130 回答
0

这就是我认为您正在尝试做的事情......以一种比 for 循环更灵活的方式。

注意,这个例子是在我推荐使用 linq 代码的 LinqPad (linqpad.com) 中运行的。它给出了预期的结果。

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();


}
于 2012-08-15T22:13:25.033 回答