Marusyk / grok.net

.NET implementation of the grok 📝
MIT License
287 stars 55 forks source link

Get Dictionary instead of GrokResult with AsDictionary method (#72) #75

Closed GioMaz closed 1 year ago

GioMaz commented 1 year ago
Grok grok = new Grok("%{MONTHDAY:month}-%{MONTHDAY:day}-%{MONTHDAY:year} %{TIME:timestamp};%{WORD:id};%{LOGLEVEL:loglevel};%{WORD:func};%{GREEDYDATA:msg}");

string logs = @"06-21-19 21:00:13:589241;15;INFO;main;DECODED: 775233900043 DECODED BY: 18500738 DISTANCE: 1.5165
                06-22-19 22:00:13:589265;156;WARN;main;DECODED: 775233900043 EMPTY DISTANCE: --------";

var grokResult = grok.AsDictionary(logs);

foreach (var item in grokResult)
{
    Console.WriteLine($"{item.Key}:");
    foreach (var value in item.Value)
    {
        Console.WriteLine("\t" + value);
    }
}

this will now print:

month:
        06
        06
day:
        21
        22
year:
        19
        19
timestamp:
        21:00:13:589241
        22:00:13:589265
id:
        15
        156
loglevel:
        INFO
        WARN
func:
        main
        main
msg:
        DECODED: 775233900043 DECODED BY: 18500738 DISTANCE: 1.5165
        DECODED: 775233900043 EMPTY DISTANCE: --------