Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing handling for LoggingEvent properties #7

Merged

added unit tests for base properties

  • Loading branch information
Asbjørn Hansen
Asbjørn Hansen committed Mar 9, 2017
commit 0e11585bbc3daa5728685f0ccf9f3f67831ad048
@@ -0,0 +1,129 @@
namespace log4net_loggly.UnitTests
{
using System;
using FluentAssertions;
using JetBrains.Annotations;
using log4net.Core;
using log4net.loggly;
using Moq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Ploeh.AutoFixture;
using Ploeh.AutoFixture.AutoMoq;
using Xunit;

[UsedImplicitly]
public class LogglyFormatterTest
{
public class ToJsonMethod
{
private readonly Fixture _fixture;

public ToJsonMethod()
{
_fixture = new Fixture();
_fixture.Customize(new SupportMutableValueTypesCustomization());
_fixture.Customize(new AutoMoqCustomization());
_fixture.Customize<LogglyFormatter>(c => c.With(x => x.Config, Mock.Of<ILogglyAppenderConfig>()));
}

[Fact]
public void ShouldReturnValidJson()
{
var evt = _fixture.Create<LoggingEvent>();
var instance = _fixture.Create<LogglyFormatter>();

var result = instance.ToJson(evt);
Action act = () => JObject.Parse(result);

act.ShouldNotThrow<JsonException>("because the result should be a valid json document");
}

[Fact]
public void ShouldAddAValidTimestampProperty()
{
var evt = _fixture.Create<LoggingEvent>();
var instance = _fixture.Create<LogglyFormatter>();

var result = instance.ToJson(evt);
dynamic json = JObject.Parse(result);

var timestamp = (string)json.timestamp;
DateTime voidDateTime;

timestamp.Should().NotBeNullOrEmpty("because the timestamp property should always be set");
DateTime.TryParse(timestamp, out voidDateTime).Should().BeTrue("because the timestamp should always be a valid date");
}

[Fact]
public void ShouldAddALevelProperty()
{
var evt = _fixture.Create<LoggingEvent>();
var instance = _fixture.Create<LogglyFormatter>();

var result = instance.ToJson(evt);
dynamic json = JObject.Parse(result);

var level = (string)json.level;

level.Should().StartWith("levelName", "because the level name property on the event is used");
}

[Fact]
public void ShouldAddAHostNameProperty()
{
var evt = _fixture.Create<LoggingEvent>();
var instance = _fixture.Create<LogglyFormatter>();

var result = instance.ToJson(evt);
dynamic json = JObject.Parse(result);

var hostName = (string)json.hostName;

hostName.Should().NotBeNullOrEmpty("because the machine name is used to set the hostname");
}

[Fact]
public void ShouldAddAProcessProperty()
{
var evt = _fixture.Create<LoggingEvent>();
var instance = _fixture.Create<LogglyFormatter>();

var result = instance.ToJson(evt);
dynamic json = JObject.Parse(result);

var process = (string)json.process;

process.Should().NotBeNullOrEmpty("because the value is taken from the current process which should always be set");
}

[Fact]
public void ShouldAddAThreadNameProperty()
{
var evt = _fixture.Create<LoggingEvent>();
var instance = _fixture.Create<LogglyFormatter>();

var result = instance.ToJson(evt);
dynamic json = JObject.Parse(result);

var threadName = (string)json.threadName;

threadName.Should().StartWith("ThreadName", "because this is the value of the ThreadName property on the event");
}

[Fact]
public void ShouldAddALoggerNameProperty()
{
var evt = _fixture.Create<LoggingEvent>();
var instance = _fixture.Create<LogglyFormatter>();

var result = instance.ToJson(evt);
dynamic json = JObject.Parse(result);

var loggerName = (string)json.loggerName;

loggerName.Should().StartWith("LoggerName", "because this is the value of the LoggerName property on the event");
}
}
}
}
@@ -1,10 +1,10 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.

[assembly: AssemblyTitle("log4net-loggly.UnitTests")]
[assembly: AssemblyDescription("Unit tests for the log4net-loggly project")]
[assembly: AssemblyConfiguration("")]
@@ -17,9 +17,11 @@
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.

[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM

[assembly: Guid("3956c2cb-dab1-40a6-ac25-8ac6a6f577aa")]

// Version information for an assembly consists of the following four values:
@@ -32,5 +34,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]

[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyFileVersion("1.0.0.0")]
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Moq" publicKeyToken="69f491c39445e920" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.7.1.0" newVersion="4.7.1.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
@@ -35,7 +35,62 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Castle.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
<HintPath>..\packages\Castle.Core.4.0.0\lib\net45\Castle.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="FluentAssertions, Version=4.19.2.0, Culture=neutral, PublicKeyToken=33f2691a05b67b6a, processorArchitecture=MSIL">
<HintPath>..\packages\FluentAssertions.4.19.2\lib\net45\FluentAssertions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="FluentAssertions.Core, Version=4.19.2.0, Culture=neutral, PublicKeyToken=33f2691a05b67b6a, processorArchitecture=MSIL">
<HintPath>..\packages\FluentAssertions.4.19.2\lib\net45\FluentAssertions.Core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="JetBrains.Annotations, Version=10.3.0.0, Culture=neutral, PublicKeyToken=1010a0d8d6380325, processorArchitecture=MSIL">
<HintPath>..\packages\JetBrains.Annotations.10.3.0\lib\net\JetBrains.Annotations.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="log4net, Version=1.2.13.0, Culture=neutral, PublicKeyToken=669e0ddf0bb1aa2a, processorArchitecture=MSIL">
<HintPath>..\packages\log4net.2.0.3\lib\net40-full\log4net.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Moq, Version=4.7.1.0, Culture=neutral, PublicKeyToken=69f491c39445e920, processorArchitecture=MSIL">
<HintPath>..\packages\Moq.4.7.1\lib\net45\Moq.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.8.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Ploeh.AutoFixture, Version=3.50.2.0, Culture=neutral, PublicKeyToken=b24654c590009d4f, processorArchitecture=MSIL">
<HintPath>..\packages\AutoFixture.3.50.2\lib\net40\Ploeh.AutoFixture.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Ploeh.AutoFixture.AutoMoq, Version=3.50.2.0, Culture=neutral, PublicKeyToken=b24654c590009d4f, processorArchitecture=MSIL">
<HintPath>..\packages\AutoFixture.AutoMoq.3.50.2\lib\net40\Ploeh.AutoFixture.AutoMoq.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
<Reference Include="xunit.abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\packages\xunit.abstractions.2.0.1\lib\net35\xunit.abstractions.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="xunit.assert, Version=2.2.0.3545, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\packages\xunit.assert.2.2.0\lib\netstandard1.1\xunit.assert.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="xunit.core, Version=2.2.0.3545, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\packages\xunit.extensibility.core.2.2.0\lib\netstandard1.1\xunit.core.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="xunit.execution.desktop, Version=2.2.0.3545, Culture=neutral, PublicKeyToken=8d05b1bb7a6fdb6c, processorArchitecture=MSIL">
<HintPath>..\packages\xunit.extensibility.execution.2.2.0\lib\net452\xunit.execution.desktop.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<Choose>
<When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
@@ -46,8 +101,19 @@
<Otherwise />
</Choose>
<ItemGroup>
<Compile Include="LogglyFormatterTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\log4net-loggly\log4net-loggly.csproj">
<Project>{ABE2B1B6-A83C-4604-AF87-FAAC3976530D}</Project>
<Name>log4net-loggly</Name>
</ProjectReference>
</ItemGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
<ItemGroup>
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AutoFixture" version="3.50.2" targetFramework="net461" />
<package id="AutoFixture.AutoMoq" version="3.50.2" targetFramework="net461" />
<package id="Castle.Core" version="4.0.0" targetFramework="net461" />
<package id="FluentAssertions" version="4.19.2" targetFramework="net461" />
<package id="JetBrains.Annotations" version="10.3.0" targetFramework="net461" />
<package id="log4net" version="2.0.3" targetFramework="net461" />
<package id="Moq" version="4.7.1" targetFramework="net461" />
<package id="Newtonsoft.Json" version="8.0.1" targetFramework="net461" />
<package id="xunit" version="2.2.0" targetFramework="net461" />
<package id="xunit.abstractions" version="2.0.1" targetFramework="net461" />
<package id="xunit.assert" version="2.2.0" targetFramework="net461" />
<package id="xunit.core" version="2.2.0" targetFramework="net461" />
<package id="xunit.extensibility.core" version="2.2.0" targetFramework="net461" />
<package id="xunit.extensibility.execution" version="2.2.0" targetFramework="net461" />
</packages>
@@ -22,7 +22,7 @@ public LogglyFormatter()
_currentProcess = Process.GetCurrentProcess();
}

internal ILogglyAppenderConfig Config { private get; set; }
public ILogglyAppenderConfig Config { get; set; }

public virtual void AppendAdditionalLoggingInformation(ILogglyAppenderConfig config, LoggingEvent loggingEvent)
{
@@ -1,10 +1,10 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.

[assembly: AssemblyTitle("log4net-loggly")]
[assembly: AssemblyDescription("Log4net client for Loggly Gen 2")]
[assembly: AssemblyConfiguration("")]
@@ -17,9 +17,11 @@
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.

[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM

[assembly: Guid("ad4ffe16-a464-400d-9650-bc4d1c43da8f")]

// Version information for an assembly consists of the following four values:
@@ -32,5 +34,6 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]

[assembly: AssemblyVersion("7.2.0.0")]
[assembly: AssemblyFileVersion("7.2.0.0")]
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.