Skip to content

Commit

Permalink
Add serilog enricher unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Jandini committed Nov 4, 2024
1 parent 2b99359 commit e4695ad
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
33 changes: 33 additions & 0 deletions src/Serilog.Enrichers.ClassName.Tests/ClassNameEnricherTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Serilog.Sinks.TestCorrelator;

namespace Serilog.Enrichers.ClassName.Tests
{
public class ClassNameEnricherTests
{
[Fact]
public void ClassNameProperty_MustExistAndMatch()
{
// Arrange
const string expectedClassName = "ClassNameEnricher";
var logger = new LoggerConfiguration()
.Enrich.With(new ClassNameEnricher())
.WriteTo.TestCorrelator()
.CreateLogger()
.ForContext("SourceContext", typeof(ClassNameEnricher).FullName);

using (TestCorrelator.CreateContext())
{
// Act
logger.Information("Test log");

// Assert
var logEvent = TestCorrelator.GetLogEventsFromCurrentContext().Single();
Assert.NotNull(logEvent);

// Verify if the UserId property is added
Assert.True(logEvent.Properties.ContainsKey("ClassName"));
Assert.Equal(expectedClassName, logEvent.Properties["ClassName"].ToString().Trim('"'));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="Serilog" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.TestCorrelator" Version="4.0.0" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

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

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

</Project>
8 changes: 7 additions & 1 deletion src/Serilog.Enrichers.ClassName.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33516.290
MinimumVisualStudioVersion = 10.0.40219.1
Project("{D12BECA3-8DFB-48E5-9430-3C060D774BD6}") = "Serilog.Enrichers.ClassName", "Serilog.Enrichers.ClassName\Serilog.Enrichers.ClassName.csproj", "{C7AF6148-7DAC-41C4-8CC6-A2B68404BDE8}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Enrichers.ClassName", "Serilog.Enrichers.ClassName\Serilog.Enrichers.ClassName.csproj", "{C7AF6148-7DAC-41C4-8CC6-A2B68404BDE8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Serilog.Enrichers.ClassName.Tests", "Serilog.Enrichers.ClassName.Tests\Serilog.Enrichers.ClassName.Tests.csproj", "{1964BEA8-3030-4E02-AD97-87D65E16157A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -15,6 +17,10 @@ Global
{C7AF6148-7DAC-41C4-8CC6-A2B68404BDE8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C7AF6148-7DAC-41C4-8CC6-A2B68404BDE8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C7AF6148-7DAC-41C4-8CC6-A2B68404BDE8}.Release|Any CPU.Build.0 = Release|Any CPU
{1964BEA8-3030-4E02-AD97-87D65E16157A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1964BEA8-3030-4E02-AD97-87D65E16157A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1964BEA8-3030-4E02-AD97-87D65E16157A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1964BEA8-3030-4E02-AD97-87D65E16157A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down

0 comments on commit e4695ad

Please sign in to comment.