【问题标题】:reading .log files and displaying the data读取 .log 文件并显示数据
【发布时间】:2020-05-02 12:32:52
【问题描述】:

我有一个看起来像这样的日志文件:

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

我的目标是只获取“{ ... }”中的部分并将其添加到对象数组中并像

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"}}
]

到目前为止我所拥有的:

我知道我可以使用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.";
        }
    }

但我不知道如何获取大括号内的内容并转换为对象

注意事项

  • 我正在使用 APS.NET mvc。
  • 如果可能,我更喜欢使用正则表达式
  • 数据是动态的,可以转换文件

更新:

获取我所做的 .log 文件的一部分:

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

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

但是这个正则表达式并没有得到我想要的,它只得到"Description": "abc",},我还想得到"Number": ...

我使用this link 来输入正则表达式

有人可以帮忙处理正则表达式吗?

【问题讨论】:

    标签: c# regex asp.net-mvc


    【解决方案1】:

    选项 1:使用 (?

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

    选项 2:遍历所有行,如果找到 { 则继续将以下行添加到字符串中,直到找到 }。您必须记下 { 的数量应该与 } 的数量相匹配

    【讨论】:

    • 我在 index.cshtml 文件中执行此代码,可以吗?还是应该在控制器中执行?
    • 你的正则表达式只会得到“数据”里面的内容,如果我这样做(?&lt;=\{)[^}]*(?=\}) 它也会得到 Id 和 Time 但没有得到数字。你能帮忙吗?
    • @LLM 我正在​​学习 mvc,但我不知道在控制器内部该怎么做
    【解决方案2】:

    以下示例将起作用:

    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
    }
    

    【讨论】:

    • 对不起,我忘记了不是以 2019 开头的行,它不起作用。
    • 有了这个 jsonString 我可以做类似jsonString.id?
    • 您可以通过 item.Id... item.Times 或 item.Data.id 访问查找中的值
    • 好的,你懂正则表达式吗?
    猜你喜欢
    • 2022-01-22
    • 2011-11-07
    • 2015-06-28
    • 1970-01-01
    • 2018-04-20
    • 2017-06-19
    • 2014-12-12
    • 1970-01-01
    • 2016-04-23
    相关资源
    最近更新 更多