-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathJsonCompactColumnizer.cs
More file actions
120 lines (98 loc) · 3.43 KB
/
JsonCompactColumnizer.cs
File metadata and controls
120 lines (98 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Collections.Generic;
using System.Linq;
using LogExpert;
using Newtonsoft.Json.Linq;
namespace JsonColumnizer;
/// <summary>
/// This Columnizer can parse JSON files.
/// </summary>
public class JsonCompactColumnizer : JsonColumnizer, IColumnizerPriority
{
#region Public methods
public override string GetName ()
{
return "JSON Compact Columnizer";
}
public override string GetDescription ()
{
return "A JSON columnier for Serilog.Formatting.Compact format.";
}
public override void Selected (ILogLineColumnizerCallback callback)
{
ColumnList.Clear();
// Create column header with cached column list.
foreach (var col in TagDict.Keys)
{
ColumnList.Add(new JsonColumn(TagDict[col]));
}
}
public override Priority GetPriority (string fileName, IEnumerable<ILogLine> samples)
{
var result = Priority.NotSupport;
if (fileName.EndsWith("json", StringComparison.OrdinalIgnoreCase))
{
result = Priority.WellSupport;
}
if (samples != null && samples.Any())
{
try
{
var line = samples.First();
var json = ParseJson(line);
if (json != null)
{
var columns = SplitJsonLine(samples.First(), json);
if (columns.ColumnValues.Length > 0 && Array.Exists(columns.ColumnValues, x => !string.IsNullOrEmpty(x.FullValue)))
{
result = Priority.PerfectlySupport;
}
}
}
catch (Exception)
{
// Ignore errors when determine priority.
}
}
return result;
}
#endregion
#region Private Methods
protected Dictionary<string, string> TagDict { get; set; } = new()
{
{"@t", "Timestamp"},
{"@l", "Level"},
{"@m", "Message"},
{"@x", "Exception"},
{"@i", "Event id"},
{"@r", "Renderings"},
{"@mt", "Message Template"},
};
protected override IColumnizedLogLine SplitJsonLine (ILogLine line, JObject json)
{
List<IColumn> returnColumns = [];
var cLogLine = new ColumnizedLogLine { LogLine = line };
var columns = json.Properties().Select(property => new ColumnWithName { FullValue = property.Value.ToString(), ColumnName = property.Name.ToString(), Parent = cLogLine }).ToList();
//
// Always rearrage the order of all json fields within a line to follow the sequence of columnNameList.
// This will make sure the log line displayed correct even the order of json fields changed.
//
foreach (var column in TagDict.Keys)
{
if (column.StartsWith('@'))
{
var existingColumn = columns.Find(x => x.ColumnName == column);
if (existingColumn != null)
{
returnColumns.Add(new Column() { FullValue = existingColumn.FullValue, Parent = cLogLine });
continue;
}
// Fields that is missing in current line should be shown as empty.
returnColumns.Add(new Column() { FullValue = "", Parent = cLogLine });
}
}
cLogLine.ColumnValues = [.. returnColumns];
return cLogLine;
}
#endregion
}