本文介绍了使用C#从日志文件中提取特定字段并将其导出到csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我用C#编写的代码,它显示了sample.log文件中包含的内容,该文件的大小为110MB.我添加了while循环,该循环将检查到文件末尾并打印所有记录,但是我没有得到第一条记录,也没有得到新行中的每条记录.

This is the code that I have written in C# which shows the contains in the sample.log file which is 110MB in size. I have added the while loop which will check till end of file and print all the records, but I am not getting the first record and also not getting each record in new line.

using System;
using System.IO;
using System.Text;

    class Program
    {
        static void Main(string[] args)
        {

            FileStream fs = new FileStream("sample.log", FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs);
            string StrFromFile = sr.ReadLine();

            StringBuilder ResultStr = new StringBuilder();

          while ((StrFromFile = sr.ReadLine()) != null)
          { 
            // your separator char seems to be char 1
            string[] SplitStrs = StrFromFile.Split(new char[] {(char)1});        
            for (int i = 0; i < SplitStrs.Length; i++)
            {   
                if (SplitStrs[i].StartsWith("52="))
                {
                    ResultStr.Append(SplitStrs[i] + " ");
                }
                else if (SplitStrs[i].StartsWith("55="))
                {
                    ResultStr.Append(SplitStrs[i] + " ");
                }
                else if (SplitStrs[i].StartsWith("132="))
                {
                    ResultStr.Append(SplitStrs[i] + " ");
                }
                else if (SplitStrs[i].StartsWith("133="))
                {
                    ResultStr.Append(SplitStrs[i] + " ");
                }
                else if (SplitStrs[i].StartsWith("35="))
                {
                    ResultStr.Append(SplitStrs[i] + " ");
                }   
            }  
         }
            Console.WriteLine(ResultStr);
            sr.Close();
            fs.Close();
            Console.ReadKey();
        }
    }



这是来自sample.log文件的一些数据:
** sample.log:**

8 = FIX.4.39 = 6335 = 049 = SAXOQUOTE56 = IDE34 = 457 = FX52 = 20101219-18:06:32369 = 310 = 003
8 = FIX.4.39 = 6135 = 034 = 449 = IDE50 = FX52 = 20101219-18:06:32.13056 = SAXOQUOTE10 = 169
8 = FIX.4.39 = 6335 = 049 = SAXOQUOTE56 = IDE34 = 557 = FX52 = 20101219-18:07:02369 = 410 = 003
8 = FIX.4.39 = 6135 = 034 = 549 = IDE50 = FX52 = 20101219-18:07:02.50156 = SAXOQUOTE10 = 170

另外我不明白为什么它不显示第一条记录,以及如何将结果写入CSV文件?

我正在得到这样的输出,
35 = 0 52 = 20101219-18:06:32.130 35 = 0 52 = 20101219-18:07:02 35 = 0 52 = 20101219-18:07:02.501

但我想要这样的输出,
35 = 0 52 = 20101219-18:06:32.130
35 = 0 52 = 20101219-18:07:02
35 = 0 52 = 20101219-18:07:02.501

请帮我解决这个问题.



This is some data from sample.log file :
**sample.log:**

8=FIX.4.39=6335=049=SAXOQUOTE56=IDE34=457=FX52=20101219-18:06:32369=310=003
8=FIX.4.39=6135=034=449=IDE50=FX52=20101219-18:06:32.13056=SAXOQUOTE10=169
8=FIX.4.39=6335=049=SAXOQUOTE56=IDE34=557=FX52=20101219-18:07:02369=410=003
8=FIX.4.39=6135=034=549=IDE50=FX52=20101219-18:07:02.50156=SAXOQUOTE10=170

Also I am not getting why it is not showing the first record and how do I write this result to CSV file?

I am getting output like this,
35=0 52=20101219-18:06:32.130 35=0 52=20101219-18:07:02 35=0 52=20101219-18:07:02.501

but I want the output like this,
35=0 52=20101219-18:06:32.130
35=0 52=20101219-18:07:02
35=0 52=20101219-18:07:02.501

Please help me with this.

推荐答案

string[] parts = logEntry.Split('\x01');


然后,您要做的就是依次检查每个部分,然后从"="的左侧确定是否要保留它:


Then, all you have to do is examine each part in turn, and determine from the bit to the left of the "=" if you want to keep it or not:

string logEntry = "8=FIX.4.39=6135=534=149=IDE50=FX52=20101219-18:05:01.52256=SAXOQUOTE10=171";
string[] parts = logEntry.Split('\x01');
StringBuilder sb = new StringBuilder(logEntry.Length);
string prefix = "";
foreach (string part in parts)
    {
    string[] breakdown = part.Split('=');
    switch (breakdown[0])
        {
        case "9":
        case "52":
            sb.Append(prefix + part);
            prefix = ",";
            break;
        }
    }


string[] parts = logLine.Split(' ');
var fields = (from field in parts 
              where field.StartsWith("9=") || 
                    field.StartsWith("35=") || 
                    field.StartsWith("52=") 
              select field);
StringBuilder newString = new StringBuilder("");
foreach(String extracted in fields)
{
    newString.AppendFormat("{0},", extracted); 
}
newString = newString.Trim();



最后,将新数据保存到一个csv文件中.



Finally, save the new data into a csv file.


using System;
using System.IO;
using System.Text;

    class Program
    {
        static void Main(string[] args)
        {

            FileStream fs = new FileStream("sample.log", FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(fs);
            string StrFromFile = sr.ReadLine(); Removing this line will solve ur prob
            StringBuilder ResultStr = new StringBuilder();

          while ((StrFromFile = sr.ReadLine()) != null)
         { ......



现在您的第二个问题

您可以使用前两个答案中的任何一个



Now your second problem

U can use any of the first two answers


这篇关于使用C#从日志文件中提取特定字段并将其导出到csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 21:24