简体   繁体   English

读取 .log 文件并显示数据

[英]reading .log files and displaying the data

I have a log file that looks like:我有一个日志文件,看起来像:

2019-12-24 18:00:17.8105|DEBUG|--------------------------------
https://...
2019-12-24 18:00:17.8105|DEBUG|Key: xxxxxxxxxxxxxxxxxxxxxxxx
{
  "Id": "xxxxxxx",
  "Times": "2019-12-24",
  "Data": {
    "Id": 8,
    "Code": "5",
    "Description": "abc",
  },
 "Number": 1,
 ...
}
2019-12-24 18:00:17.8105|DEBUG|--------------------------------
https://...
2019-12-24 18:00:17.8105|DEBUG|Key: xxxxxxxxxxxxxxxxxxxxxxxx
{
  "Id": "xxxxxxx",
  "Times": "2019-12-24",
  "Data": {
    "Id": 6,
    "Code": "3",
    "Description": "def"
  },
  "Number": 3,
  ...
}
//...and it repeats several times in this file with diferent data

My objective is to get just the part inside "{ ... }" and add it inside an array of objects and be like我的目标是只获取 "{ ... }" 中的部分并将其添加到对象数组中,就像

Array: 
[
   {"Id": "xxxxxxx", "Times": "2019-12-24", "Data": { "Id": 8, "Code": "5", "Description": "abc"}}
   {"Id": "xxxxxxx", "Time": "2019-12-24", "Data": { "Id": 6, "Code": "3", "Description": "def"}}
]

What I have so far:到目前为止我所拥有的:

I know that I can use File object to read the file, so in my index.cshtml I put:我知道我可以使用File对象来读取文件,所以在我的 index.cshtml 中我放了:

    @{
        var result = "";

        Array log = null;

        var dataFile = Server.UrlPathEncode("C:/Temp/test.log");

        if (File.Exists(dataFile))
        {
            log = File.ReadAllLines(dataFile);
            if (log == null)
            {
                result = "The file is empty.";
            } 
        }
        else
        {
            result = "The file does not exist.";
        }
    }

but I don't have idea how to just get what is inside curly brackets and convert into an object但我不知道如何获取大括号内的内容并将其转换为对象

Notes :注意事项

  • I'm using APS.NET mvc.我正在使用 APS.NET mvc。
  • I prefer to use regex if possible如果可能,我更喜欢使用正则表达式
  • The data is dynamic, it can change over file数据是动态的,可以切换文件

UPDATE:更新:

to get part of .log file I did:获取 .log 文件的一部分,我做了:

foreach (var item in log)
{
   text += item;
}

Regex regex = new Regex(@"(?={)[^}]*(?=})", RegexOptions.IgnoreCase);
matches = regex.Matches(text);

but this regex don't get all I want, it just get until "Description": "abc",} and I want to get also the "Number": ...但是这个正则表达式并没有得到我想要的一切,直到"Description": "abc",} ,我也想得到"Number": ...

I used this link to text regex我使用此链接文本正则表达式

someone can help with regex?有人可以帮助使用正则表达式吗?

Option 1: Use a Regex like (?<={)[^}{]*(?=}) to get the JSON content from the log file.选项 1:使用像 (?<={)[^}{]*(?=}) 这样的正则表达式从日志文件中获取 JSON 内容。

Regex regex = new Regex(@"(?<=\{)[^}{]*(?=\})", RegexOptions.IgnoreCase);
MatchCollection matches = regex.Matches(str);

Option 2: Loop through all the lines and if you find a { keep adding the following lines to a string until you find a }.选项 2:循环遍历所有行,如果找到 {,请继续将以下行添加到字符串中,直到找到 }。 You have to make a note of number of { should match the number of }你必须记下 { 的数量应该与 } 的数量相匹配

Below sample will work:以下示例将起作用:

public class MyData
{
    public string Id { set; get; }
    public string Times { set; get; }
    public Data Data { set; get; }
    public string Number { set; get; }
}

public class Data
{
    public string Id { set; get; }
    public string Code { set; get; }
    public string Description { set; get; }
}

var lines = File.ReadAllLines(@"C:\Temp\data.txt").Where(line => !line.StartsWith("2019"));
var jsonString = "[" + String.Join("", lines) + "]";
jsonString = jsonString.Replace("}", "},");
jsonString = jsonString.Replace("},,", "},");
var myObjectDT = JsonConvert.DeserializeObject<List<MyData>>(jsonString);
foreach(var item in myObjectDT)
{
    string line = JObject.FromObject(item).ToString(Formatting.None);
    //write line to file
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM