Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
133 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
test/Serilog.Enrichers.Process.Tests/Enrichers/ProcessIdEnricherTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
test/Serilog.Enrichers.Process.Tests/Enrichers/ProcessNameEnricherTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
test/Serilog.Enrichers.Process.Tests/Serilog.Enrichers.Process.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
33
test/Serilog.Enrichers.Process.Tests/Support/DelegatingSink.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
12
test/Serilog.Enrichers.Process.Tests/Support/Extensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.