Skip to content

Commit

Permalink
Add a simple unit test project
Browse files Browse the repository at this point in the history
  • Loading branch information
Numpsy committed Oct 18, 2020
1 parent d9523de commit 569f1e3
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 3 deletions.
18 changes: 15 additions & 3 deletions serilog-enrichers-process.sln
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26114.2
# Visual Studio Version 16
VisualStudioVersion = 16.0.30608.117
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{037440DE-440B-4129-9F7A-09B42D00397E}"
EndProject
Expand All @@ -13,7 +13,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "assets", "assets", "{E9D1B5
assets\Serilog.snk = assets\Serilog.snk
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serilog.Enrichers.Process", "src\Serilog.Enrichers.Process\Serilog.Enrichers.Process.csproj", "{E950BED9-A953-4555-9159-B40ABDE7DC90}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Enrichers.Process", "src\Serilog.Enrichers.Process\Serilog.Enrichers.Process.csproj", "{E950BED9-A953-4555-9159-B40ABDE7DC90}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Enrichers.Process.Tests", "test\Serilog.Enrichers.Process.Tests\Serilog.Enrichers.Process.Tests.csproj", "{27336067-0A49-4053-9A57-76510C8D9097}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{ACD52C07-EC24-4A6D-9DE4-653A6A89F74D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -25,11 +29,19 @@ Global
{E950BED9-A953-4555-9159-B40ABDE7DC90}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E950BED9-A953-4555-9159-B40ABDE7DC90}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E950BED9-A953-4555-9159-B40ABDE7DC90}.Release|Any CPU.Build.0 = Release|Any CPU
{27336067-0A49-4053-9A57-76510C8D9097}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{27336067-0A49-4053-9A57-76510C8D9097}.Debug|Any CPU.Build.0 = Debug|Any CPU
{27336067-0A49-4053-9A57-76510C8D9097}.Release|Any CPU.ActiveCfg = Release|Any CPU
{27336067-0A49-4053-9A57-76510C8D9097}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{E950BED9-A953-4555-9159-B40ABDE7DC90} = {037440DE-440B-4129-9F7A-09B42D00397E}
{27336067-0A49-4053-9A57-76510C8D9097} = {ACD52C07-EC24-4A6D-9DE4-653A6A89F74D}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9BEC8B70-CA11-4400-AEAE-B0EAE15E933B}
EndGlobalSection
EndGlobal
@@ -0,0 +1,26 @@
using Serilog.Events;
using Serilog.Tests.Support;
using Xunit;

namespace Serilog.Enrichers.Process.Tests
{
public class ProcessIdEnricherTests
{
[Fact]
public void ProcessIdEnricherIsApplied()
{
LogEvent evt = null;
var log = new LoggerConfiguration()
.Enrich.WithProcessId()
.WriteTo.Sink(new DelegatingSink(e => evt = e))
.CreateLogger();

log.Information(@"Has a ProcessId property");

Assert.NotNull(evt);

var processId = System.Diagnostics.Process.GetCurrentProcess().Id;
Assert.Equal(processId, (int)evt.Properties["ProcessId"].LiteralValue());
}
}
}
@@ -0,0 +1,26 @@
using Serilog.Events;
using Serilog.Tests.Support;
using Xunit;

namespace Serilog.Enrichers.Process.Tests
{
public class ProcessNameEnricherTests
{
[Fact]
public void ProcessNameEnricherIsApplied()
{
LogEvent evt = null;
var log = new LoggerConfiguration()
.Enrich.WithProcessName()
.WriteTo.Sink(new DelegatingSink(e => evt = e))
.CreateLogger();

log.Information(@"Has a ProcessName property");

Assert.NotNull(evt);

var processName = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
Assert.Equal(processName, (string)evt.Properties["ProcessName"].LiteralValue());
}
}
}
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;netcoreapp2.1;net46</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Serilog.Enrichers.Process\Serilog.Enrichers.Process.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit" Version="2.4.1" />
</ItemGroup>

</Project>
33 changes: 33 additions & 0 deletions test/Serilog.Enrichers.Process.Tests/Support/DelegatingSink.cs
@@ -0,0 +1,33 @@
using System;
using Serilog.Core;
using Serilog.Events;

namespace Serilog.Tests.Support
{
public class DelegatingSink : ILogEventSink
{
readonly Action<LogEvent> _write;

public DelegatingSink(Action<LogEvent> write)
{
if (write == null) throw new ArgumentNullException(nameof(write));
_write = write;
}

public void Emit(LogEvent logEvent)
{
_write(logEvent);
}

public static LogEvent GetLogEvent(Action<ILogger> writeAction)
{
LogEvent result = null;
var l = new LoggerConfiguration()
.WriteTo.Sink(new DelegatingSink(le => result = le))
.CreateLogger();

writeAction(l);
return result;
}
}
}
12 changes: 12 additions & 0 deletions test/Serilog.Enrichers.Process.Tests/Support/Extensions.cs
@@ -0,0 +1,12 @@
using Serilog.Events;

namespace Serilog.Tests.Support
{
public static class Extensions
{
public static object LiteralValue(this LogEventPropertyValue @this)
{
return ((ScalarValue)@this).Value;
}
}
}
Empty file removed test/test-project-needed.txt
Empty file.

0 comments on commit 569f1e3

Please sign in to comment.