0

私がやろうとしていることは、行ごとにテキストファイルを取得し、行の始まりに応じて、送信するテーブルを決定することです。単語または「[」で始まる特定の行を特定のテーブルに送信するための最良の (最も効率的な) 方法を見つけるのに問題があります。これは、読み込んでいるテキスト ファイルの例です。これはログであり、例外、メッセージ、およびソースは、テキスト ファイルのどこにでも配置できます。"[" で始まる行は (LogTable) に、"Exception" で始まる行は (ExceptionTable) になりますが、すべての "Exception" は前のログ行に接続されているため、方法も探しています。 2 つを接続して、データベースでリンクできるようにします。

[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();
               }
4

3 に答える 3

0

行ごとに分割してから「:」で分割しています。例外が3行にまたがっているため、行配列に3つの異なる値があるように見えるため、2番目の分割は期待どおりに機能しません。

あなたが達成しようとしていると私が思うことを達成する1つの方法は、行配列を反復処理し、次のようなケースに遭遇したときにケースを処理することです:

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

SSIS を使用して統合を行うと、簡単になり、テキスト ファイルを分割するのに役立ちます http://social.technet.microsoft.com/wiki/contents/articles/3172.aspx

于 2012-08-15T21:34:37.110 に答える
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 に答える