-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add extension methods for ILogger to be able to push properties like …
…serilog does without serilog; add more unit tests
- Loading branch information
Showing
10 changed files
with
158 additions
and
11 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
src/Cortside.Common.Logging/Cortside.Common.Logging.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,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" /> | ||
</ItemGroup> | ||
</Project> |
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,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Cortside.Common.Logging { | ||
public static class LoggerExtensions { | ||
public static IDisposable PushProperty(this ILogger logger, string name, object value) { | ||
return logger.BeginScope(new Dictionary<string, object> { { name, value } }); | ||
} | ||
|
||
public static IDisposable PushProperties(this ILogger logger, Dictionary<string, object> properties) { | ||
return logger.BeginScope(properties); | ||
} | ||
} | ||
} |
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
67 changes: 67 additions & 0 deletions
67
src/Cortside.Common.Testing.Tests/Logging/LogEvent/LogEventLoggerTest.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,67 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Cortside.Common.Logging; | ||
using Cortside.Common.Testing.Logging.LogEvent; | ||
using Microsoft.Extensions.Logging; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace Cortside.Common.Testing.Tests.Logging.Xunit { | ||
public class LogEventLoggerTest { | ||
private readonly ILoggerFactory loggerFactory; | ||
|
||
public LogEventLoggerTest(ITestOutputHelper output) { | ||
// Create a logger factory with a debug provider | ||
loggerFactory = LoggerFactory.Create(builder => { | ||
builder | ||
.SetMinimumLevel(LogLevel.Trace) | ||
.AddFilter("Microsoft", LogLevel.Warning) | ||
.AddFilter("System", LogLevel.Warning) | ||
.AddFilter("Cortside.Common", LogLevel.Trace) | ||
.AddLogEvent(); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public void TestLogger() { | ||
// Create a logger with the category name of the current class | ||
var logger = loggerFactory.CreateLogger<LogEventLoggerTest>(); | ||
|
||
Assert.NotNull(logger); | ||
|
||
// Log some messages with different log levels and message templates | ||
logger.LogTrace("This is a trace message."); | ||
logger.LogDebug("This is a debug message."); | ||
logger.LogInformation("Hello {Name}!", "World"); | ||
logger.LogWarning("This is a warning message."); | ||
logger.LogError("This is an error message."); | ||
logger.LogCritical("This is a critical message."); | ||
|
||
// Use structured logging to capture complex data | ||
var person = new Person { Name = "Alice", Age = 25 }; | ||
logger.LogInformation("Created a new person: {@Person}", person); | ||
|
||
// Use exception logging to capture the details of an exception | ||
try { | ||
throw new Exception("Something went wrong."); | ||
} catch (Exception ex) { | ||
logger.LogError(ex, "An exception occurred."); | ||
} | ||
|
||
// Use the logger to capture a log event | ||
Assert.Equal(8, LogEventLogger.LogEvents.Count); | ||
|
||
using (logger.PushProperties(new Dictionary<string, object>() { | ||
["UserId"] = "xxx", | ||
["ExtraProperty"] = "yyy", | ||
})) { | ||
logger.LogDebug("logged messaged that should have 2 properties with it"); | ||
} | ||
|
||
// 10, adding 1 for the actual log and 1 for the being scope | ||
Assert.Equal(10, LogEventLogger.LogEvents.Count); | ||
Assert.Equal("UserId=xxxExtraProperty=yyy", LogEventLogger.LogEvents.First(x => x.LogLevel == LogLevel.None).Message); | ||
} | ||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
src/Cortside.Common.Testing/Logging/LogEvent/LogEventLoggerFactoryExtensions.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,20 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Cortside.Common.Testing.Logging.LogEvent { | ||
/// <summary> | ||
/// Extension methods for the <see cref="ILoggerFactory"/> class. | ||
/// </summary> | ||
public static class LogEventLoggerFactoryExtensions { | ||
/// <summary> | ||
/// Adds a debug logger named 'Debug' to the factory. | ||
/// </summary> | ||
/// <param name="builder">The extension method argument.</param> | ||
public static ILoggingBuilder AddLogEvent(this ILoggingBuilder builder) { | ||
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<ILoggerProvider>(new LogEventLoggerProvider())); | ||
|
||
return builder; | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/Cortside.Common.Testing/Logging/LogEvent/LogEventLoggerProvider.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,22 @@ | ||
using Cortside.Common.Testing.Logging.Xunit; | ||
using Microsoft.Extensions.Logging; | ||
using Xunit.Abstractions; | ||
|
||
namespace Cortside.Common.Testing.Logging.LogEvent { | ||
/// <summary> | ||
/// The provider for the <see cref="XunitLogger"/>. | ||
/// </summary> | ||
[ProviderAlias("LogEvent")] | ||
public class LogEventLoggerProvider : ILoggerProvider { | ||
private readonly ITestOutputHelper output; | ||
|
||
/// <inheritdoc /> | ||
public ILogger CreateLogger(string name) { | ||
return new LogEventLogger(name); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void Dispose() { | ||
} | ||
} | ||
} |
7 changes: 1 addition & 6 deletions
7
src/Cortside.Common.Testing/Logging/Xunit/XunitLoggerFactoryExtensions.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
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