简体   繁体   中英

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:

    @{
        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.
  • 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:

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": ...

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.

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 }. 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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