PvWay.LoggerService.UTest.nc6
3.0.3
See the version list below for details.
dotnet add package PvWay.LoggerService.UTest.nc6 --version 3.0.3
NuGet\Install-Package PvWay.LoggerService.UTest.nc6 -Version 3.0.3
<PackageReference Include="PvWay.LoggerService.UTest.nc6" Version="3.0.3" />
paket add PvWay.LoggerService.UTest.nc6 --version 3.0.3
#r "nuget: PvWay.LoggerService.UTest.nc6, 3.0.3"
// Install PvWay.LoggerService.UTest.nc6 as a Cake Addin #addin nuget:?package=PvWay.LoggerService.UTest.nc6&version=3.0.3 // Install PvWay.LoggerService.UTest.nc6 as a Cake Tool #tool nuget:?package=PvWay.LoggerService.UTest.nc6&version=3.0.3
pvWay UTestLoggerService for dotNet Core 6
Description
This nuget implements the ILoggerService by passing a UnitTest logWriter enabling you to perform Asserts on log content
Severity enum
public enum SeverityEnu
{
Ok, // "O"
Trace, // "T"
Debug, // "D"
Info, // "I"
Warning, // "W"
Error, // "E"
Fatal // "F"
}
Methods
All methods include both a synchronous and an asynchronous signature.
The class also implement the ILogger interface (microsoft.logging)
See here after the main methods:
void Log(
string message,
SeverityEnu severity = SeverityEnu.Debug,
[CallerMemberName] string memberName = "",
[CallerFilePath] string filePath = "",
[CallerLineNumber] int lineNumber = -1);
Task LogAsync(
string message,
SeverityEnu severity = SeverityEnu.Debug,
[CallerMemberName] string memberName = "",
[CallerFilePath] string filePath = "",
[CallerLineNumber] int lineNumber = -1);
void Log(
IEnumerable<string> messages,
SeverityEnu severity,
[CallerMemberName] string memberName = "",
[CallerFilePath] string filePath = "",
[CallerLineNumber] int lineNumber = -1);
Task LogAsync(
IEnumerable<string> messages,
SeverityEnu severity,
[CallerMemberName] string memberName = "",
[CallerFilePath] string filePath = "",
[CallerLineNumber] int lineNumber = -1);
void Log(
Exception e,
SeverityEnu severity = SeverityEnu.Fatal,
[CallerMemberName] string memberName = "",
[CallerFilePath] string filePath = "",
[CallerLineNumber] int lineNumber = -1);
Task LogAsync(
Exception e,
SeverityEnu severity = SeverityEnu.Fatal,
[CallerMemberName] string memberName = "",
[CallerFilePath] string filePath = "",
[CallerLineNumber] int lineNumber = -1);
void Log(
IMethodResult result,
string? topic,
[CallerMemberName] string memberName = "",
[CallerFilePath] string filePath = "",
[CallerLineNumber] int lineNumber = -1);
Task LogAsync(
IMethodResult result,
string? topic,
[CallerMemberName] string memberName = "",
[CallerFilePath] string filePath = "",
[CallerLineNumber] int lineNumber = -1);
Injection
The AddPvWayUTestLoggerService method extends the IServiceCollection
The default lifetime is Scoped and the default minimum log level is Trace... i.e. logging everything
The method returns a IUTestLogWriter object.
That object UTestLogWriter object will allow you to perform Asserts on log content during your unit tests.
/// <summary>
/// Injects a transient IUTestLoggerService
/// </summary>
/// <param name="services"></param>
/// <returns>IUTestLogWriter</returns>
public static IUTestLogWriter AddPvWayUTestLoggerService(
this IServiceCollection services)
{
services.TryAddSingleton<ILoggerServiceConfig>(_ =>
new LoggerServiceConfig(SeverityEnu.Trace));
var logWriter = new UTestLogWriter();
services.TryAddTransient<IUTestLogWriter>(_ => logWriter);
services.TryAddTransient<ILoggerService, UTestLoggerService>();
services.TryAddTransient<IUTestLoggerService, UTestLoggerService>();
return logWriter;
}
IUTestLogWriter interface
public interface IUTestLogWriter : ILogWriter, IDisposable, IAsyncDisposable
{
IEnumerable<ILoggerServiceRow> LogRows { get; }
bool HasLog(string term);
ILoggerServiceRow? FindFirstMatchingRow(string term);
ILoggerServiceRow? FindLastMatchingRow(string term);
}
Static factories
The PvWayConsoleLogger static class also exposes two public Create methods enabling to factor the service from your own code
public static IUTestLogWriter CreateUTestLogWriter()
{
return new UTestLogWriter();
}
public static IUTestLoggerService CreateService(
IUTestLogWriter utLw)
{
return new UTestLoggerService(
new LoggerServiceConfig(SeverityEnu.Trace),
utLw);
}
public static IUTestLoggerService CreateService(
out IUTestLogWriter utLw)
{
utLw = CreateUTestLogWriter();
return new UTestLoggerService(
new LoggerServiceConfig(SeverityEnu.Trace),
utLw);
}
public static IUTestLoggerService<T> CreateService<T>(
IUTestLogWriter utLw)
{
return new UTestLoggerService<T>(
new LoggerServiceConfig(SeverityEnu.Trace),
utLw);
}
public static IUTestLoggerService<T> CreateService<T>(
out IUTestLogWriter utLw)
{
utLw = CreateUTestLogWriter();
return new UTestLoggerService<T>(
new LoggerServiceConfig(SeverityEnu.Trace),
utLw);
}
Usage
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using PvWay.LoggerService.Abstractions.nc8;
using PvWay.LoggerService.UTest.nc8;
Console.WriteLine("Hello, UTestLoggerService");
Console.WriteLine();
var services = new ServiceCollection();
var lw = services.AddPvWayUTestLoggerService();
var sp = services.BuildServiceProvider();
var ls = sp.GetRequiredService<ILoggerService>();
ls.Log("This is a trace test log message", SeverityEnu.Trace);
ls.Log("This is a debug test log message");
ls.Log("This is an info test log message", SeverityEnu.Info);
ls.Log("This is a warning test log message", SeverityEnu.Warning);
ls.Log("This is an error test log message", SeverityEnu.Error);
ls.Log("This is a fatal test log message", SeverityEnu.Fatal);
ls.Log(LogLevel.Trace, "MsLog trace");
foreach (var row in lw.LogRows)
{
Console.WriteLine(row.Message);
}
Happy coding
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
-
net6.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
- PvWay.LoggerService.nc6 (>= 3.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Uses LoggerService 1.0.4