forked from akpaevj/OneSTools.PS.TechLog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetTechLogCmdlet.cs
More file actions
219 lines (175 loc) · 7.53 KB
/
GetTechLogCmdlet.cs
File metadata and controls
219 lines (175 loc) · 7.53 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using System;
using System.Text;
using System.IO;
using System.Collections.Generic;
using System.Management.Automation;
using System.Linq;
using System.Text.RegularExpressions;
namespace OneSTools.PS.TechLog
{
[Cmdlet(VerbsCommon.Get, "TechLog")]
[OutputType(typeof(TjEvent))]
public class GetTechLogCmdlet : PSCmdlet
{
private readonly Dictionary<string, StreamReader> readers = new Dictionary<string, StreamReader>();
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true)]
public string[] Path { get; set; }
protected override void BeginProcessing()
{
var allFiles = new List<string>();
foreach(var path in Path) {
var files = SessionState.Path.GetResolvedPSPathFromPSPath(path).Select(c => c.Path);
allFiles.AddRange(files);
}
foreach (var logFile in allFiles)
{
WriteVerbose($"Creating reader for the file by path {logFile}");
var stream = new FileStream(logFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
var reader = new StreamReader(stream);
readers.Add(logFile, reader);
}
}
public void BeginProcessingInternal()
{
BeginProcessing();
}
protected override void ProcessRecord()
{
foreach (var kv in readers)
{
WriteVerbose($"Reading of {kv.Key} is started");
StartReading(kv.Key, kv.Value);
}
}
public void ProcessRecordInternal()
{
ProcessRecord();
}
private void StartReading(string path, StreamReader reader)
{
var fileDateTime = GetFileDateTime(path) + ":";
reader.BaseStream.Seek(3, SeekOrigin.Current);
var eventBuffer = new StringBuilder();
while (!reader.EndOfStream) {
var line = reader.ReadLine();
if (line?.Length > 15 && line[2] == ':' && line[5] == '.' && line[12] == '-') {
if (eventBuffer.Length > 0)
{
var tjEvent = ParseTjEventData(eventBuffer.ToString());
WriteObject(tjEvent);
eventBuffer.Clear();
}
eventBuffer.Append(fileDateTime);
}
else
eventBuffer.Append('\n');
eventBuffer.Append(line);
}
if (eventBuffer.Length > 0)
{
var tjEvent = ParseTjEventData(eventBuffer.ToString());
WriteObject(tjEvent);
}
}
string GetFileDateTime(string filePath) {
var info = new FileInfo(filePath);
var year = info.Name.Substring(0, 2);
var month = info.Name.Substring(2, 2);
var day = info.Name.Substring(4, 2);
var hour = info.Name.Substring(6, 2);
return $"20{year}-{month}-{day} {hour}";
}
private TjEvent ParseTjEventData(string eventData)
{
var eventDateTime = DateTime.Parse(eventData.Substring(0, 26));
var durationStart = 27;
var durationEnd = eventData.IndexOf(',');
var duration = long.Parse(eventData.Substring(durationStart, durationEnd - durationStart));
var eventNameStart = durationEnd + 1;
var eventNameEnd = eventData.IndexOf(',', eventNameStart + 1);
var eventName = eventData.Substring(eventNameStart, eventNameEnd - eventNameStart);
var levelStart = eventNameEnd + 1;
var levelEnd = eventData.IndexOf(',', levelStart + 1);
var level = int.Parse(eventData.Substring(levelStart, levelEnd - levelStart));
var properties = ParseEventProperties(eventData, levelEnd + 1);
var ev = new TjEvent()
{
DateTime = eventDateTime,
Duration = duration,
EventName = eventName.ToString(),
Level = level,
Properties = properties
};
return ev;
}
private Dictionary<string, string> ParseEventProperties(string eventData, int startIndex)
{
var properties = new Dictionary<string, string>();
while(true) {
if (startIndex >= eventData.Length)
break;
var propertyNameEnd = eventData.IndexOf('=', startIndex);
var propertyName = eventData.Substring(startIndex, propertyNameEnd - startIndex);
var propertyValueStart = propertyNameEnd + 1;
var propertyValueEnd = eventData.Length;
var startIndexOffset = 0;
// Sometimes there is no property value and this property is the last one in the event
if (propertyValueStart != eventData.Length)
{
switch(eventData[propertyValueStart])
{
case ',':
// there is no value for this property
startIndex = propertyValueStart + 1;
continue;
case '\'':
// this is a text value
propertyValueStart += 1;
propertyValueEnd = eventData.Replace("\'\'", "••")
.IndexOf('\'', propertyValueStart);
startIndexOffset = 1;
break;
case '"':
// this is a text value
propertyValueStart += 1;
propertyValueEnd = eventData.Replace("\"\"", "••")
.IndexOf('"', propertyValueStart);
startIndexOffset = 1;
break;
default:
propertyValueEnd = eventData.IndexOf(',', propertyValueStart);
break;
}
}
if (propertyValueEnd < propertyValueStart)
propertyValueEnd = eventData.Length;
var propertyValue = eventData.Substring(propertyValueStart, propertyValueEnd - propertyValueStart);
var propertyNameStr = propertyName.ToString();
var propertyValueStr = propertyValue.ToString();
AddPropertyValue(properties, propertyNameStr, propertyValueStr);
startIndex = propertyValueEnd + startIndexOffset + 1;
}
return properties;
}
private void AddPropertyValue(Dictionary<string, string> properties, string property, string value, int index = 0)
{
var propertyName = index == 0 ? property : $"{property}_{index}";
if (properties.ContainsKey(propertyName))
AddPropertyValue(properties, property, value, index + 1);
else
{
properties.Add(propertyName, value);
}
}
protected override void EndProcessing()
{
foreach(var reader in readers.Values)
reader?.Dispose();
readers.Clear();
}
public void EndProcessingInternal()
{
EndProcessing();
}
}
}