-
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.
Add the support of the log message templates. (#6)
* Add the support of the log message templates.
- Loading branch information
1 parent
44a83bc
commit 72d5c4f
Showing
11 changed files
with
623 additions
and
128 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
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//----------------------------------------------------------------------- | ||
// <copyright file="ILoggerMockSetupSequenceLog.cs" company="P.O.S Informatique"> | ||
// Copyright (c) P.O.S Informatique. All rights reserved. | ||
// </copyright> | ||
//----------------------------------------------------------------------- | ||
|
||
namespace PosInformatique.Logging.Assertions | ||
{ | ||
using Microsoft.Extensions.Logging; | ||
|
||
/// <summary> | ||
/// Allows to setup the sequence of <see cref="ILogger"/> method calls for the <see cref="ILogger.Log{TState}(LogLevel, EventId, TState, Exception?, Func{TState, Exception?, string})"/>. | ||
/// </summary> | ||
public interface ILoggerMockSetupSequenceLog : ILoggerMockSetupSequence | ||
{ | ||
/// <summary> | ||
/// Allows to assert the template message arguments. | ||
/// </summary> | ||
/// <param name="expectedCount">Number of template message arguments expected.</param> | ||
/// <param name="expectedArguments">A delegate which allows to check the template message arguments. All the arguments can be asserted using the <see cref="LogMessageTemplateArguments"/> | ||
/// parameter of the delegate.</param> | ||
/// <returns>An instance of <see cref="ILoggerMockSetupSequence"/> which allows to continue the setup of the method calls for the <see cref="ILogger"/>.</returns> | ||
ILoggerMockSetupSequence WithArguments(int expectedCount, Action<LogMessageTemplateArguments> expectedArguments); | ||
} | ||
} |
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,92 @@ | ||
//----------------------------------------------------------------------- | ||
// <copyright file="LogMessageTemplateArguments.cs" company="P.O.S Informatique"> | ||
// Copyright (c) P.O.S Informatique. All rights reserved. | ||
// </copyright> | ||
//----------------------------------------------------------------------- | ||
|
||
namespace PosInformatique.Logging.Assertions | ||
{ | ||
using System.Collections; | ||
using Microsoft.Extensions.Logging; | ||
|
||
/// <summary> | ||
/// Contains the message template arguments of the <see cref="ILogger.Log{TState}(LogLevel, EventId, TState, Exception?, Func{TState, Exception?, string})"/> | ||
/// which are accessible by the delegate specified on the <see cref="ILoggerMockSetupSequenceLog.WithArguments(int, Action{LogMessageTemplateArguments})"/> | ||
/// to assert the message template arguments. | ||
/// </summary> | ||
public sealed class LogMessageTemplateArguments : IEnumerable<object?> | ||
{ | ||
private readonly IReadOnlyList<KeyValuePair<string, object?>> arguments; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="LogMessageTemplateArguments"/> class. | ||
/// </summary> | ||
/// <param name="arguments">Arguments to assert.</param> | ||
internal LogMessageTemplateArguments(IEnumerable<KeyValuePair<string, object?>> arguments) | ||
{ | ||
this.arguments = arguments.ToArray(); | ||
} | ||
|
||
/// <summary> | ||
/// Gets the number of message template arguments. | ||
/// </summary> | ||
public int Count => this.arguments.Count; | ||
|
||
/// <summary> | ||
/// Gets the message template argument value by his key. | ||
/// </summary> | ||
/// <param name="key">Template argument name to retrieve the value.</param> | ||
/// <returns>The message template argument value by his key.</returns> | ||
/// <exception cref="KeyNotFoundException">If the <paramref name="key"/> argument name has not been found.</exception> | ||
public object? this[string key] | ||
{ | ||
get | ||
{ | ||
var valueFound = this.arguments.SingleOrDefault(kv => kv.Key == key); | ||
|
||
if (valueFound.Equals(default(KeyValuePair<string, object?>))) | ||
{ | ||
throw new KeyNotFoundException($"The given message template argument '{key}' was not present."); | ||
} | ||
|
||
return valueFound.Value; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets the message template argument value by his index position. | ||
/// </summary> | ||
/// <param name="index">Template argument index position to retrieve the value.</param> | ||
/// <returns>The message template argument value at the specified <paramref name="index"/> position.</returns> | ||
/// <exception cref="ArgumentOutOfRangeException">If the <paramref name="index"/> is out of the range of the template message arguments list.</exception> | ||
public object? this[int index] | ||
{ | ||
get | ||
{ | ||
if (index < 0) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(index)); | ||
} | ||
|
||
if (index >= this.arguments.Count) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(index)); | ||
} | ||
|
||
return this.arguments[index].Value; | ||
} | ||
} | ||
|
||
/// <inheritdoc /> | ||
public IEnumerator<object?> GetEnumerator() | ||
{ | ||
return this.arguments.Select(kv => kv.Value).GetEnumerator(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return this.GetEnumerator(); | ||
} | ||
} | ||
} |
Oops, something went wrong.