Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions IISParser.PowerShell/CmdletGetIISParsedLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,19 +136,21 @@ protected override Task ProcessRecordAsync() {
return Task.CompletedTask;
}

private void WriteEvent(IISLogEvent evt) {
if (Expand) {
var psObj = PSObject.AsPSObject(evt);
foreach (var kv in evt.Fields) {
if (psObj.Properties[kv.Key] == null) {
psObj.Properties.Add(new PSNoteProperty(kv.Key, kv.Value));
}
}
WriteObject(psObj);
} else {
WriteObject(evt);
}
}
private void WriteEvent(IISLogEvent evt) {
if (Expand) {
var psObj = new PSObject();
foreach (var prop in PSObject.AsPSObject(evt).Properties) {
if (!prop.Name.Equals("Fields", StringComparison.OrdinalIgnoreCase)) {
psObj.Properties.Add(new PSNoteProperty(prop.Name, prop.Value));
}
}
foreach (var kv in evt.Fields)
psObj.Properties.Add(new PSNoteProperty(kv.Key, kv.Value));
WriteObject(psObj);
} else {
WriteObject(evt);
}
}

/// <inheritdoc />
protected override Task EndProcessingAsync() {
Expand Down
9 changes: 9 additions & 0 deletions IISParser.Tests/ParserEngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ public void ParseLog_ReturnsEvents() {
Assert.Equal("192.168.0.1", evt.Fields["X-Forwarded-For"]);
}

[Fact]
public void ParseLog_RemovesKnownFieldsFromDictionary() {
var path = Path.Combine(AppContext.BaseDirectory, "TestData", "sample.log");
var engine = new ParserEngine(path);
var evt = engine.ParseLog().Single();
Assert.False(evt.Fields.ContainsKey("cs-uri-stem"));
Assert.False(evt.Fields.ContainsKey("date"));
}

[Fact]
public void ParseLog_HandlesValuesAboveIntMax() {
var path = Path.Combine(AppContext.BaseDirectory, "TestData", "large_values.log");
Expand Down
31 changes: 29 additions & 2 deletions IISParser/ParserEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,31 @@ public class ParserEngine : IDisposable {
private readonly Dictionary<string, string?> _dataStruct = new(StringComparer.OrdinalIgnoreCase);
private readonly int _mbSize;

private static readonly HashSet<string> KnownFields = new(StringComparer.OrdinalIgnoreCase) {
"date",
"time",
"s-sitename",
"s-computername",
"s-ip",
"cs-method",
"cs-uri-stem",
"cs-uri-query",
"s-port",
"cs-username",
"c-ip",
"cs-version",
"cs(User-Agent)",
"cs(Cookie)",
"cs(Referer)",
"cs-host",
"sc-status",
"sc-substatus",
"sc-win32-status",
"sc-bytes",
"cs-bytes",
"time-taken"
};

/// <summary>
/// Gets the path to the log file being processed.
/// </summary>
Expand Down Expand Up @@ -85,8 +110,10 @@ private void ProcessLine(string line, List<IISLogEvent> events) {

private IISLogEvent NewEventObj() {
var evt = new IISLogEvent();
foreach (var kv in _dataStruct)
evt.Fields[kv.Key] = kv.Value;
foreach (var kv in _dataStruct) {
if (!KnownFields.Contains(kv.Key))
evt.Fields[kv.Key] = kv.Value;
}
evt.DateTimeEvent = GetEventDateTime();
evt.sSitename = GetValue("s-sitename");
evt.sComputername = GetValue("s-computername");
Expand Down
1 change: 1 addition & 0 deletions Module/Tests/Get-IISParsedLog.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Describe 'Get-IISParsedLog' {
$logPath = (Resolve-Path "$PSScriptRoot/../../IISParser.Tests/TestData/sample.log").Path
$result = Get-IISParsedLog -FilePath $logPath -Expand
$result.'X-Forwarded-For' | Should -Be '192.168.0.1'
($result.PSObject.Properties.Match('Fields').Count) | Should -Be 0
}

It 'streams large log with Skip, First and Last' {
Expand Down
Loading