Skip to content

Commit

Permalink
Merge pull request #111 from Analogy-LogViewer/refacor
Browse files Browse the repository at this point in the history
Refacor
  • Loading branch information
LiorBanai authored Sep 26, 2020
2 parents f7d89bf + d0dce02 commit 6cfc26d
Show file tree
Hide file tree
Showing 40 changed files with 1,143 additions and 1,343 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@
</PackageReference>
</ItemGroup>

<ItemGroup>
<Folder Include="log files\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Analogy.LogViewer.Serilog\Analogy.LogViewer.Serilog.csproj" />
</ItemGroup>
Expand All @@ -28,22 +24,25 @@
<None Update="log files\Analogy.Logserver.20200913.log">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="log files\ClefExample1.clef">
<None Update="log files\CompactJsonFormat.clef">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="log files\CompactJsonFormat.gz">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="log files\ClefExample1.clef.gz">
<None Update="log files\JsonFormatPerFile.clef">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="log files\JsonFormatter.clef">
<None Update="log files\JsonFormatPerLine.clef">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="log files\SourceContextTest.clef">
<None Update="log files\CompactJsonFormatSourceContextTest.clef">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="log files\TestColumns.clef">
<None Update="log files\CompactJsonFormatTestColumns.clef">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="log files\testJson.clef">
<None Update="log files\JsonFileCompactFormat.clef">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
Expand Down
125 changes: 0 additions & 125 deletions Analogy.LogViewer.Serilog.UnitTests/Analogy.Logserver.20200913.log

This file was deleted.

60 changes: 0 additions & 60 deletions Analogy.LogViewer.Serilog.UnitTests/ClefTests.cs

This file was deleted.

126 changes: 126 additions & 0 deletions Analogy.LogViewer.Serilog.UnitTests/CompactJsonFormatTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
using Analogy.LogViewer.Serilog.DataTypes;
using Analogy.LogViewer.Serilog.IAnalogy;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Analogy.LogViewer.Serilog.UnitTests
{
[TestClass]
public class CompactJsonFormatTests
{
private string Folder { get; } = Environment.CurrentDirectory;
[TestMethod]
[DataRow("CompactJsonFormat.clef",4, "2016-10-12T04:46:58.0554314Z")]
[DataRow("CompactJsonFormatSourceContextTest.clef",2, "2020-06-18T18:03:19.2248275Z")]
[DataRow("CompactJsonFormatTestColumns.clef",4, "2020-06-26T14:21:34.7233612Z")]
[DataRow("CompactJsonFormat.gz",4, "2016-10-12T04:46:58.0554314Z")]
public async Task OfflineProviderParserTimestampTest(string fileName,int numberOfMessages,string datetimeToParse)
{
OfflineDataProvider parser = new OfflineDataProvider();
CancellationTokenSource cts = new CancellationTokenSource();
string file = Path.Combine(Folder, "log files", fileName);
MessageHandlerForTesting forTesting = new MessageHandlerForTesting();
var messages = (await parser.Process(file, cts.Token, forTesting)).ToList();
DateTimeOffset dto = DateTimeOffset.Parse(datetimeToParse);
Assert.IsTrue(messages.Count == numberOfMessages);
Assert.IsTrue(messages[0].Date == dto.DateTime);
}


[TestMethod]
[DataRow("CompactJsonFormat.clef")]
[DataRow("CompactJsonFormatSourceContextTest.clef")]
[DataRow("CompactJsonFormatTestColumns.clef")]
[DataRow("CompactJsonFormat.gz")]
public void CompactJsonFormatTestAutomaticDetection(string fileName)
{
string file = Path.Combine(Folder, "log files", fileName);
var type = OfflineDataProvider.TryDetectFormat(file);
Assert.IsTrue(type == FileFormat.CompactJsonFormatPerLine);

}

[TestMethod]
public async Task CompactJsonFormatParserTest()
{
CompactJsonFormatParser parser = new CompactJsonFormatParser();
CancellationTokenSource cts = new CancellationTokenSource();
string fileName = Path.Combine(Folder, "log files", "CompactJsonFormat.clef");
MessageHandlerForTesting forTesting = new MessageHandlerForTesting();
var messages = (await parser.Process(fileName, cts.Token, forTesting)).ToList();
Assert.IsTrue(messages.Count == 4);
Assert.IsTrue(messages[0].Text == "Hello, { Name: \"nblumhardt\", Id: 101 }");
}

// Test reading the (optional) source context
[TestMethod]
public async Task CompactJsonFormatSourceContextTest()
{
CompactJsonFormatParser parser = new CompactJsonFormatParser();
CancellationTokenSource cts = new CancellationTokenSource();
string fileName = Path.Combine(Folder, "log files", "CompactJsonFormatSourceContextTest.clef");
MessageHandlerForTesting forTesting = new MessageHandlerForTesting();

var messages = (await parser.Process(fileName, cts.Token, forTesting)).ToList();

Assert.AreEqual(2, messages.Count());

// The first event doesn't have a source context
var firstEvent = messages.ElementAt(0);
Assert.AreEqual("Hello, Serilog!", firstEvent.Text);
Assert.AreEqual(string.Empty, firstEvent.Source);
Assert.AreEqual(1, firstEvent.ThreadId);
Assert.IsNotNull(firstEvent.Module);
Assert.IsNotNull(firstEvent.FileName);
Assert.IsNotNull(firstEvent.Category);
Assert.IsNotNull(firstEvent.User);
Assert.IsNotNull(firstEvent.MethodName);
// The second event should have a source context
var secondEvent = messages.ElementAt(1);
Assert.AreEqual("Contextual Log", secondEvent.Text);
Assert.AreEqual("SerilogLogging.Program", secondEvent.Source);
Assert.AreEqual(1, secondEvent.ThreadId);
Assert.IsNotNull(secondEvent.Module);
Assert.IsNotNull(secondEvent.FileName);
Assert.IsNotNull(secondEvent.Category);
Assert.IsNotNull(secondEvent.User);
Assert.IsNotNull(secondEvent.MethodName);
}


[TestMethod]
public async Task CompactJsonFormatTestColumns()
{
CompactJsonFormatParser parser = new CompactJsonFormatParser();
CancellationTokenSource cts = new CancellationTokenSource();
string fileName = Path.Combine(Folder, "log files", "CompactJsonFormatTestColumns.clef");
MessageHandlerForTesting forTesting = new MessageHandlerForTesting();
var messages = (await parser.Process(fileName, cts.Token, forTesting)).ToList();
Assert.AreEqual(4, messages.Count());
// The first event doesn't have a source context
Assert.IsTrue(messages[0].MachineName == "Test");
Assert.IsTrue(messages[1].AdditionalInformation["CustomProperty"] == "\"Custom Value\"");

}

[TestMethod]
public async Task CompactJsonFormatTestGZFile()
{
CompactJsonFormatParser parser = new CompactJsonFormatParser();
CancellationTokenSource cts = new CancellationTokenSource();
string fileName = Path.Combine(Folder, "log files", "CompactJsonFormat.gz");
MessageHandlerForTesting forTesting = new MessageHandlerForTesting();
var messages = (await parser.Process(fileName, cts.Token, forTesting)).ToList();
Assert.AreEqual(4, messages.Count());
// The first event doesn't have a source context
Assert.IsTrue(messages[2].AdditionalInformation["Tags"] == "[\"test\", \"orange\"]");

}


}
}
85 changes: 85 additions & 0 deletions Analogy.LogViewer.Serilog.UnitTests/JsonFormatTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using Analogy.LogViewer.Serilog.DataTypes;
using Analogy.LogViewer.Serilog.IAnalogy;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Analogy.LogViewer.Serilog.UnitTests
{
[TestClass]
public class JsonFormatTests
{
private string Folder { get; } = Environment.CurrentDirectory;

[TestMethod]
public async Task CompactJsonFormatFullFileTest()
{
JsonFileParser parser = new JsonFileParser(new CompactJsonFormatMessageFields());
CancellationTokenSource cts = new CancellationTokenSource();
string fileName = Path.Combine(Folder, "log files", "JsonFileCompactFormat.clef");
MessageHandlerForTesting forTesting = new MessageHandlerForTesting();
var messages = (await parser.Process(fileName, cts.Token, forTesting)).ToList();
Assert.IsTrue(messages.Count == 2);
Assert.IsTrue(messages[0].MachineName == "MY-MACHINE");
Assert.IsTrue(messages[1].Text.StartsWith("An unknown error occurred"));
Assert.IsTrue((messages[1].Module == "My process"));

}

[TestMethod]
public async Task JsonFilePerLineTest()
{
var p = new JsonFormatterParser(new JsonFormatMessageFields());
CancellationTokenSource cts = new CancellationTokenSource();
string fileName = Path.Combine(Folder, "log files", "JsonFormatPerLine.clef");
MessageHandlerForTesting forTesting = new MessageHandlerForTesting();
var messages = (await p.Process(fileName, cts.Token, forTesting)).ToList();
Assert.IsTrue(messages.Count == 2);
//Assert.IsTrue(messages[0].Text == "Hello, { Name: \"nblumhardt\", Tags: [1, 2, 3] }, 0000007b at 06/07/2016 06:44:57","got"+ messages[0].Text);
Assert.IsTrue(messages[0].User == "{ Name: \"nblumhardt\", Tags: [1, 2, 3] }");
}

[TestMethod]
public async Task JsonFilePerFileTest()
{
JsonFileParser parser = new JsonFileParser(new JsonFormatMessageFields());
CancellationTokenSource cts = new CancellationTokenSource();
string fileName = Path.Combine(Folder, "log files", "JsonFormatPerFile.clef");
MessageHandlerForTesting forTesting = new MessageHandlerForTesting();
var messages = (await parser.Process(fileName, cts.Token, forTesting)).ToList();
Assert.IsTrue(messages.Count == 2);
//Assert.IsTrue(messages[0].Text == "Hello, { Name: \"nblumhardt\", Tags: [1, 2, 3] }, 0000007b at 06/07/2016 06:44:57","got"+ messages[0].Text);
Assert.IsTrue(messages[0].User == "{ Name: \"nblumhardt\", Tags: [1, 2, 3] }");
}

[TestMethod]
[DataRow("JsonFileCompactFormat.clef", FileFormat.CompactJsonFormatPerFile)]
[DataRow("JsonFormatPerLine.clef", FileFormat.JsonFormatPerLine)]
public void CompactJsonFormatTestAutomaticDetection(string fileName, FileFormat format)
{
string file = Path.Combine(Folder, "log files", fileName);
var type = OfflineDataProvider.TryDetectFormat(file);
Assert.IsTrue(type == format);
}

[TestMethod]
public async Task JsonFilePerLineDateTimeWithOffsetTest()
{
var p = new JsonFormatterParser(new JsonFormatMessageFields());
CancellationTokenSource cts = new CancellationTokenSource();
string fileName = Path.Combine(Folder, "log files", "JsonFormatPerLine.clef");
MessageHandlerForTesting forTesting = new MessageHandlerForTesting();
var messages = (await p.Process(fileName, cts.Token, forTesting)).ToList();
Assert.IsTrue(messages.Count == 2);

Assert.IsTrue(messages[0].User == "{ Name: \"nblumhardt\", Tags: [1, 2, 3] }");

DateTimeOffset dto = DateTimeOffset.Parse("2020-06-07T13:44:57.8532799+10:00");
//Assert.IsTrue(messages[0].Date == dto.DateTime);
//Assert.IsTrue(messages[0].Text == "Hello, { Name: \"nblumhardt\", Tags: [1, 2, 3] }, 0000007b at 06/07/2016 06:44:57", "got" + messages[0].Text);
}
}
}
26 changes: 0 additions & 26 deletions Analogy.LogViewer.Serilog.UnitTests/JsonParserTests.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[
{
"Timestamp": "2020-06-07T13:44:57.8532799+10:00",
"Level": "Information",
"MessageTemplate": "Hello, {@User}, {N:x8} at {Now}",
"Properties": {
"User": {
"Name": "nblumhardt",
"Tags": [ 1, 2, 3 ]
},
"N": 123,
"Now": "2016-06-07T13:44:57.8532799+10:00"
},
"Renderings": {
"N": [
{
"Format": "x8",
"Rendering": "0000007b"
}
]
}
},
{
"Timestamp": "2016-06-07T13:44:57.8532799+10:00",
"Level": "Information",
"MessageTemplate": "Hello, {@User}, {N:x8} at {Now}",
"Properties": {
"User": {
"Name": "Lior",
"Tags": [ 1, 2, 3 ]
},
"N": 123,
"Now": "2016-06-07T13:44:57.8532799+10:00"
},
"Renderings": {
"N": [
{
"Format": "x8",
"Rendering": "0000007b"
}
]
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{"Timestamp":"2020-06-07T13:44:57.8532799+10:00","Level":"Information","MessageTemplate":"Hello, {@User}, {N:x8} at {Now}","Properties":{"User":{"Name":"nblumhardt","Tags":[1,2,3]},"N":123,"Now":"2016-06-07T13:44:57.8532799+10:00"},"Renderings":{"N":[{"Format":"x8","Rendering":"0000007b"}]}}
{"Timestamp":"2016-06-07T13:44:57.8532799+10:00","Level":"Information","MessageTemplate":"Hello, {@User}, {N:x8} at {Now}","Properties":{"User":{"Name":"Lior","Tags":[1,2,3]},"N":123,"Now":"2016-06-07T13:44:57.8532799+10:00"},"Renderings":{"N":[{"Format":"x8","Rendering":"0000007b"}]}}

This file was deleted.

Loading

0 comments on commit 6cfc26d

Please sign in to comment.