Skip to content

Commit

Permalink
Added ICacheLogger.ClearEntries (#284)
Browse files Browse the repository at this point in the history
* Added ICacheLogger.ClearEntries

Closes #280
  • Loading branch information
roryprimrose authored Nov 6, 2024
1 parent 783dd35 commit f1e7589
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
run: Add-Content -Path ${env:GITHUB_ENV} "`nprojectName=$(${env:GITHUB_REPOSITORY}.substring(${env:GITHUB_REPOSITORY}.IndexOf('/') + 1))" -Encoding utf8

- name: Checkout
uses: actions/checkout@v4.1.7
uses: actions/checkout@v4.2.2
with:
fetch-depth: 0

Expand Down
12 changes: 6 additions & 6 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
</PropertyGroup>
<ItemGroup Label="Package Versions used by this repository">
<!-- Direct dependencies -->
<PackageVersion Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageVersion Include="System.Text.Json" Version="8.0.4" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="8.0.1" />
<PackageVersion Include="System.Text.Json" Version="8.0.5" />
<PackageVersion Include="Xunit.Abstractions" Version="2.0.3" />
<PackageVersion Include="NSubstitute" Version="5.1.0" />
<PackageVersion Include="NSubstitute" Version="5.3.0" />
<PackageVersion Include="ModelBuilder" Version="8.6.0" />
<PackageVersion Include="FluentAssertions" Version="6.12.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageVersion Include="xunit" Version="2.9.0" />
<PackageVersion Include="FluentAssertions" Version="6.12.1" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageVersion Include="xunit" Version="2.9.2" />
<PackageVersion Include="xunit.runner.visualstudio" Version="2.8.2" PrivateAssets="All" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
<PackageVersion Include="coverlet.msbuild" Version="6.0.2" PrivateAssets="All" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
<!-- Overrides -->
Expand Down
38 changes: 38 additions & 0 deletions Neovolve.Logging.Xunit.UnitTests/CacheLoggerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@ public void CanDisposeWithoutFactory()
action.Should().NotThrow();
}

[Fact]
public void ClearEntriesResetsStoredData()
{
using var sut = new CacheLogger();

sut.LogInformation("first");
sut.LogInformation("second");
sut.LogInformation("third");
sut.ClearEntries();

sut.Count.Should().Be(0);
sut.Entries.Should().BeEmpty();
sut.Last.Should().BeNull();
}

[Fact]
public void DisposeCallsDisposeOnFactory()
{
Expand Down Expand Up @@ -118,6 +133,28 @@ public void EntriesRecordsLogStateInformation()
data.Should().ContainKey("{OriginalFormat}").WhoseValue.Should().Be(format);
}

[Fact]
public void EntriesReturnsRecordsEntriesAfterBeingCleared()
{
using var sut = new CacheLogger();

sut.LogInformation("first");
sut.LogInformation("second");
sut.LogInformation("third");
sut.ClearEntries();
sut.LogInformation("first");
sut.LogInformation("second");
sut.LogInformation("third");

var entries = sut.Entries.ToList();

entries.Should().HaveCount(3);
entries[0].Message.Should().Be("first");
entries[1].Message.Should().Be("second");
entries[2].Message.Should().Be("third");
sut.Last!.Message.Should().Be("third");
}

[Fact]
public void EntriesReturnsRecordsEntriesInOrder()
{
Expand All @@ -133,6 +170,7 @@ public void EntriesReturnsRecordsEntriesInOrder()
entries[0].Message.Should().Be("first");
entries[1].Message.Should().Be("second");
entries[2].Message.Should().Be("third");
sut.Last!.Message.Should().Be("third");
}

[Theory]
Expand Down
1 change: 1 addition & 0 deletions Neovolve.Logging.Xunit.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpRenamePlacementToArrangementMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpUseContinuousIndentInsideBracesMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002EMemberReordering_002EMigrations_002ECSharpFileLayoutPatternRemoveIsAttributeUpgrade/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EAddAccessorOwnerDeclarationBracesMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EAlwaysTreatStructAsNotReorderableMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002ECSharpPlaceAttributeOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
Expand Down
6 changes: 6 additions & 0 deletions Neovolve.Logging.Xunit/CacheLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ internal CacheLogger(ILogger logger, ILoggerFactory factory)
return cacheScope;
}

/// <inheritdoc />
public void ClearEntries()
{
_logEntries.Clear();
}

/// <inheritdoc />
public void Dispose()
{
Expand Down
5 changes: 5 additions & 0 deletions Neovolve.Logging.Xunit/ICacheLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
/// </summary>
public interface ICacheLogger : ILogger, IDisposable
{
/// <summary>
/// Clears all recorded log entries.
/// </summary>
void ClearEntries();

/// <summary>
/// Gets the number of cache entries recorded.
/// </summary>
Expand Down

0 comments on commit f1e7589

Please sign in to comment.