generated from dailydevops/dotnet-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Initial functionality * fix(deps): Added missing PackageVersion * fix(deps): More packages * ci: Enabled SonarQube * chore(tests): Added unit tests
- Loading branch information
Showing
13 changed files
with
263 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,7 @@ jobs: | |
name: Build & Tests | ||
uses: dailydevops/pipelines/.github/workflows/[email protected] | ||
with: | ||
disablePublish: true | ||
enableSonarQube: true | ||
dotnet-logging: ${{ inputs.dotnet-logging }} | ||
dotnet-version: | | ||
8.x | ||
|
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
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
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
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 |
---|---|---|
@@ -1,2 +1,9 @@ | ||
# template-dotnet | ||
.NET template for repositories | ||
# NetEvolve.Logging.Abstractions | ||
|
||
This library provides some extensions to the `Microsoft.Extensions.Logging` library. It is intended to be used in conjunction further libraries that provide more specific logging implementations. | ||
|
||
## Provided Types & Extensions | ||
|
||
- `LoggedMessage` - A simple class that represents a message that has been logged. It contains the message, the log level, and the exception that was thrown (if any). | ||
- `NullExternalScopeProvider` - An implementation of `IExternalScopeProvider` that does nothing. | ||
- `NullScope` - An implementation of `IDisposable` that does nothing. |
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,16 @@ | ||
// <auto-generated /> | ||
|
||
#if NETSTANDARD2_0 | ||
namespace System.Runtime.CompilerServices | ||
{ | ||
using global::System.Diagnostics; | ||
using global::System.Diagnostics.CodeAnalysis; | ||
|
||
/// <summary> | ||
/// Reserved to be used by the compiler for tracking metadata. | ||
/// This class should not be used by developers in source code. | ||
/// </summary> | ||
[ExcludeFromCodeCoverage, DebuggerNonUserCode] | ||
internal static class IsExternalInit { } | ||
} | ||
#endif |
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,23 @@ | ||
namespace NetEvolve.Logging.Abstractions; | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.Extensions.Logging; | ||
|
||
/// <summary> | ||
/// Represents a logged message, including the timestamp, log level, event ID, message, exception, and scopes. | ||
/// </summary> | ||
/// <param name="Timestamp">Timestamp of the logged message.</param> | ||
/// <param name="LogLevel">LogLevel of the logged message.</param> | ||
/// <param name="EventId">EventId of the logged message.</param> | ||
/// <param name="Message">Logged message.</param> | ||
/// <param name="Exception">Logged exception. (optional)</param> | ||
/// <param name="Scopes">Logged scopes.</param> | ||
public readonly record struct LoggedMessage( | ||
DateTimeOffset Timestamp, | ||
LogLevel LogLevel, | ||
EventId EventId, | ||
string Message, | ||
Exception? Exception = null, | ||
IReadOnlyCollection<object?>? Scopes = null | ||
); |
26 changes: 26 additions & 0 deletions
26
src/NetEvolve.Logging.Abstractions/NetEvolve.Logging.Abstractions.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,26 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>$(ProjectTargetFrameworks)</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<Title>$(MSBuildProjectName)</Title> | ||
<Description> | ||
Contains several abstractions for logging, based on the nuget package `Microsoft.Extensions.Logging.Abstractions`. | ||
|
||
- `LoggedMessage` represents a message that was logged. | ||
- `NullExternalScopeProvider` is a `IExternalScopeProvider` that does nothing. | ||
- `NullScope` is a `IDisposable` that does nothing. | ||
</Description> | ||
|
||
<PackageTags>$(PackageTags);logging;abstractions</PackageTags> | ||
<PackageProjectUrl>https://github.com/dailydevops/logging.abstractions.git</PackageProjectUrl> | ||
<RepositoryUrl>https://github.com/dailydevops/logging.abstractions.git</RepositoryUrl> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" /> | ||
</ItemGroup> | ||
|
||
</Project> |
26 changes: 26 additions & 0 deletions
26
src/NetEvolve.Logging.Abstractions/NullExternalScopeProvider.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 @@ | ||
namespace NetEvolve.Logging.Abstractions; | ||
|
||
using System; | ||
using Microsoft.Extensions.Logging; | ||
|
||
/// <summary> | ||
/// Scope provider that does nothing, used when no scope provider is available. | ||
/// </summary> | ||
public sealed class NullExternalScopeProvider : IExternalScopeProvider | ||
{ | ||
private static readonly Lazy<IExternalScopeProvider> _instance = | ||
new(() => new NullExternalScopeProvider()); | ||
|
||
private NullExternalScopeProvider() { } | ||
|
||
/// <summary> | ||
/// Returns a cached instance of <see cref="NullExternalScopeProvider"/>. | ||
/// </summary> | ||
public static IExternalScopeProvider Instance => _instance.Value; | ||
|
||
/// <inheritdoc cref="IExternalScopeProvider.ForEachScope{TState}(Action{object?, TState}, TState)" /> | ||
public void ForEachScope<TState>(Action<object?, TState> callback, TState state) { } | ||
|
||
/// <inheritdoc cref="IExternalScopeProvider.Push(object?)" /> | ||
public IDisposable Push(object? state) => NullScope.Instance; | ||
} |
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 @@ | ||
namespace NetEvolve.Logging.Abstractions; | ||
|
||
using System; | ||
|
||
/// <summary> | ||
/// Scope that does nothing. | ||
/// </summary> | ||
public sealed class NullScope : IDisposable | ||
{ | ||
private static readonly Lazy<IDisposable> _instance = new(() => new NullScope()); | ||
|
||
/// <summary> | ||
/// Returns a cached instance of <see cref="NullScope"/>. | ||
/// </summary> | ||
public static IDisposable Instance => _instance.Value; | ||
|
||
private NullScope() { } | ||
|
||
/// <inheritdoc cref="IDisposable.Dispose" /> | ||
public void Dispose() { } | ||
} |
29 changes: 29 additions & 0 deletions
29
tests/NetEvolve.Logging.Abstractions.Tests.Unit/LoggedMessageTests.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,29 @@ | ||
namespace NetEvolve.Logging.Abstractions.Tests.Unit; | ||
|
||
using System; | ||
using Microsoft.Extensions.Logging; | ||
using Xunit; | ||
|
||
public class LoggedMessageTests | ||
{ | ||
[Fact] | ||
public void Constructor_WithoutScopes_Expected() | ||
{ | ||
// Arrange | ||
var timestamp = DateTimeOffset.Now; | ||
var logLevel = LogLevel.Information; | ||
var message = "Hello World!"; | ||
var eventId = new EventId(1, nameof(Constructor_WithoutScopes_Expected)); | ||
|
||
// Act | ||
var loggedMessage = new LoggedMessage(timestamp, logLevel, eventId, message); | ||
|
||
// Assert | ||
Assert.Equal(timestamp, loggedMessage.Timestamp); | ||
Assert.Equal(logLevel, loggedMessage.LogLevel); | ||
Assert.Equal(eventId, loggedMessage.EventId); | ||
Assert.Equal(message, loggedMessage.Message); | ||
Assert.Null(loggedMessage.Exception); | ||
Assert.Null(loggedMessage.Scopes); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...etEvolve.Logging.Abstractions.Tests.Unit/NetEvolve.Logging.Abstractions.Tests.Unit.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,24 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>$(TestTargetFrameworks)</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" /> | ||
<PackageReference Include="coverlet.msbuild" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" /> | ||
<PackageReference Include="NetEvolve.Extensions.XUnit" /> | ||
<PackageReference Include="xunit" /> | ||
<PackageReference Include="xunit.runner.visualstudio" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\NetEvolve.Logging.Abstractions\NetEvolve.Logging.Abstractions.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<AssemblyAttribute Include="NetEvolve.Extensions.XUnit.UnitTestAttribute" /> | ||
</ItemGroup> | ||
|
||
</Project> |
51 changes: 51 additions & 0 deletions
51
tests/NetEvolve.Logging.Abstractions.Tests.Unit/NullExternalScopeProviderTests.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,51 @@ | ||
namespace NetEvolve.Logging.Abstractions.Tests.Unit; | ||
|
||
using System.Collections.Generic; | ||
using Xunit; | ||
|
||
public class NullExternalScopeProviderTests | ||
{ | ||
[Fact] | ||
public void Instance_ReturnsSameInstance_Expected() | ||
{ | ||
// Arrange | ||
var instance1 = NullExternalScopeProvider.Instance; | ||
var instance2 = NullExternalScopeProvider.Instance; | ||
|
||
// Assert | ||
Assert.Same(instance1, instance2); | ||
} | ||
|
||
[Fact] | ||
public void ForEachScope_DoNothing_NoExceptionIsThrown() | ||
{ | ||
// Arrange | ||
var provider = NullExternalScopeProvider.Instance; | ||
var state = new Dictionary<string, object?> { { "Hello World!", null } }; | ||
|
||
// Act | ||
var ex = Record.Exception(() => provider.ForEachScope((_, _) => { }, state)); | ||
|
||
// Assert | ||
Assert.Null(ex); | ||
} | ||
|
||
[Fact] | ||
public void Push_ReturnsNullScope_Expected() | ||
{ | ||
// Arrange | ||
var provider = NullExternalScopeProvider.Instance; | ||
|
||
// Act | ||
var ex = Record.Exception(() => | ||
{ | ||
using var scope = provider.Push(null); | ||
|
||
Assert.NotNull(scope); | ||
_ = Assert.IsType<NullScope>(scope); | ||
}); | ||
|
||
// Assert | ||
Assert.Null(ex); | ||
} | ||
} |