使用C#从日志文件中提取特定字段并将其导出到csv文件 [英] Extracting specific fields from a log file and export it to csv file using C#

查看:98
本文介绍了使用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.Substring会很容易做到.但是仔细检查发现它已经被定界了:每个字段都以CTRL-A结尾.
您可以只使用string.Split将日志行分成其组成部分:
Initially, I thought your data was fixed format, and that string.Substring would do it fairly easily. But a closer inspection shows it is already delimited: each field ends with CTRL-A.
You can just use string.Split to break the log line into it''s component parts:
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.Split(我不能说出它是什么),然后检索以所需字符串开头的字符串:

Just call string.Split on the separator character (I can''t tell what it is), and then retrieve the strings that start with the desired string:

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.


首先要解决的问题

但是我没有得到第一条记录"

Fisrt thing first

"but I am not getting the first record"

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文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆