Skip to content

Commit

Permalink
Simplify the logging (#1359)
Browse files Browse the repository at this point in the history
  • Loading branch information
martintmk authored Jun 27, 2023
1 parent 67d72b0 commit f803198
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 101 deletions.
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@
</ItemGroup>
<ItemGroup Condition="!$([MSBuild]::IsTargetFrameworkCompatible($(TargetFramework), 'netcoreapp3.1'))">
<PackageVersion Include="Microsoft.Extensions.Options" Version="2.2.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="2.2.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.0" />
</ItemGroup>
</Project>
93 changes: 0 additions & 93 deletions src/Polly.Extensions/Telemetry/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@
namespace Polly.Extensions.Telemetry;

#pragma warning disable S107 // Methods should not have too many parameters
#pragma warning disable S109 // Magic numbers should not be used

#if NET6_0_OR_GREATER
internal static partial class Log
#else
internal static class Log
#endif
{
private const string StrategyExecutedMessage = "Resilience strategy executed. " +
"Builder Name: '{BuilderName}', " +
Expand Down Expand Up @@ -42,7 +37,6 @@ internal static class Log
"Strategy Key: '{StrategyKey}', " +
"Result Type: '{ResultType}'";

#if NET6_0_OR_GREATER
[LoggerMessage(0, LogLevel.Warning, ResilienceEventMessage, EventName = "ResilienceEvent")]
public static partial void ResilienceEvent(
this ILogger logger,
Expand All @@ -53,46 +47,14 @@ public static partial void ResilienceEvent(
string? strategyKey,
object? result,
Exception? exception);
#else
private static readonly Action<ILogger, string, string?, string?, string, string?, object?, Exception?> ResilienceEventAction =
LoggerMessage.Define<string, string?, string?, string, string?, object?>(LogLevel.Warning, new EventId(0, "ResilienceEvent"), ResilienceEventMessage);

public static void ResilienceEvent(
this ILogger logger,
string eventName,
string? builderName,
string? strategyName,
string strategyType,
string? strategyKey,
object? result,
Exception? exception)
{
ResilienceEventAction(logger, eventName, builderName, strategyName, strategyType, strategyKey, result, exception);
}
#endif

#if NET6_0_OR_GREATER
[LoggerMessage(1, LogLevel.Debug, StrategyExecutingMessage, EventName = "StrategyExecuting")]
public static partial void ExecutingStrategy(
this ILogger logger,
string? builderName,
string? strategyKey,
string resultType);
#else
private static readonly Action<ILogger, string?, string?, string, Exception?> ExecutingStrategyAction =
LoggerMessage.Define<string?, string?, string>(LogLevel.Debug, new EventId(1, "StrategyExecuting"), StrategyExecutingMessage);

public static void ExecutingStrategy(
this ILogger logger,
string? builderName,
string? strategyKey,
string resultType)
{
ExecutingStrategyAction(logger, builderName, strategyKey, resultType, null);
}
#endif

#if NET6_0_OR_GREATER
[LoggerMessage(EventId = 2, Message = StrategyExecutedMessage, EventName = "StrategyExecuted")]
public static partial void StrategyExecuted(
this ILogger logger,
Expand All @@ -104,36 +66,7 @@ public static partial void StrategyExecuted(
string executionHealth,
double executionTime,
Exception? exception);
#else
private static readonly Action<ILogger, string?, string?, string, object?, string, double, Exception?> StrategyExecutedActionDebug =
LoggerMessage.Define<string?, string?, string, object?, string, double>(LogLevel.Debug, new EventId(2, "StrategyExecuted"), StrategyExecutedMessage);

private static readonly Action<ILogger, string?, string?, string, object?, string, double, Exception?> StrategyExecutedActionWarning =
LoggerMessage.Define<string?, string?, string, object?, string, double>(LogLevel.Warning, new EventId(2, "StrategyExecuted"), StrategyExecutedMessage);

public static void StrategyExecuted(
this ILogger logger,
LogLevel logLevel,
string? builderName,
string? strategyKey,
string resultType,
object? result,
string executionHealth,
double executionTime,
Exception? exception)
{
if (logLevel == LogLevel.Warning)
{
StrategyExecutedActionWarning(logger, builderName, strategyKey, resultType, result, executionHealth, executionTime, exception);
}
else
{
StrategyExecutedActionDebug(logger, builderName, strategyKey, resultType, result, executionHealth, executionTime, exception);
}
}
#endif

#if NET6_0_OR_GREATER
[LoggerMessage(EventId = 3, Message = ExecutionAttemptMessage, EventName = "ExecutionAttempt", SkipEnabledCheck = true)]
public static partial void ExecutionAttempt(
this ILogger logger,
Expand All @@ -147,30 +80,4 @@ public static partial void ExecutionAttempt(
int attempt,
double executionTimeMs,
Exception? exception);
#else
public static void ExecutionAttempt(
this ILogger logger,
LogLevel level,
string? builderName,
string? strategyName,
string strategyType,
string? strategyKey,
object? result,
bool handled,
int attempt,
double executionTimeMs,
Exception? exception)
{
#pragma warning disable CA1848 // Use the LoggerMessage delegates
if (exception is null)
{
logger.Log(level, new EventId(3, "ExecutionAttempt"), ExecutionAttemptMessage, builderName, strategyName, strategyType, strategyKey, result, handled, attempt, executionTimeMs);
}
else
{
logger.Log(level, new EventId(3, "ExecutionAttempt"), exception, ExecutionAttemptMessage, builderName, strategyName, strategyType, strategyKey, result, handled, attempt, executionTimeMs);
}
#pragma warning restore CA1848 // Use the LoggerMessage delegates
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,7 @@ public void WriteExecutionAttempt_LoggingWithOutcome_Ok(bool noOutcome, bool han

if (noOutcome)
{
#if NET6_0_OR_GREATER
string resultString = string.Empty;
#else
string resultString = "(null)";
#endif

messages[0].Message.Should().Be($"Execution attempt. Builder Name: 'my-builder', Strategy Name: 'my-strategy', Strategy Type: 'my-strategy-type', Strategy Key: 'my-strategy-key', Result: '{resultString}', Handled: '{handled}', Attempt: '4', Execution Time: '123'");
}
else
Expand All @@ -168,7 +163,6 @@ public void WriteExecutionAttempt_LoggingWithOutcome_Ok(bool noOutcome, bool han
messages[0].LogLevel.Should().Be(LogLevel.Debug);
}

#if NET6_0_OR_GREATER
// verify reported state
var coll = messages[0].State.Should().BeAssignableTo<IReadOnlyList<KeyValuePair<string, object>>>().Subject;
coll.Count.Should().Be(9);
Expand All @@ -184,7 +178,6 @@ public void WriteExecutionAttempt_LoggingWithOutcome_Ok(bool noOutcome, bool han
}

coll.Invoking(c => c[coll.Count + 1]).Should().Throw<IndexOutOfRangeException>();
#endif
}

[Fact]
Expand Down

0 comments on commit f803198

Please sign in to comment.