Skip to content

Latest commit

 

History

History
119 lines (87 loc) · 4.98 KB

README.md

File metadata and controls

119 lines (87 loc) · 4.98 KB

Audit.NET.Polly

Resilience wrapper Data Provider for Audit.NET library

Allows to define Polly resilience strategies to any Data Provider within Audit.NET.

Install

NuGet Package

To install the package, run the following command on the Package Manager Console:

PM> Install-Package Audit.NET.Polly

NuGet Status NuGet Count

Usage

Please see the Audit.NET Readme

Configuration

To set the Polly data provider globally, call the UsePolly() method on the fluent configuration API:

Audit.Core.Configuration.Setup()
    .UsePolly(polly => polly
        .DataProvider(...)
        .WithResilience(r => r...)

For instance, to establish a retry policy for a RavenDB data provider, ensuring that insert/replace operations are retried no more than twice in the event of a RavenException:

var ravenDbProvider = new RavenDbDataProvider(r => r
    .WithSettings(s => s
        .Urls("http://127.0.0.1:8080")
        .Database(_ => "Audit")));

Audit.Core.Configuration.Setup()
    .UsePolly(p => p
        .DataProvider(ravenDbProvider)
        .WithResilience(resilience => resilience
            .AddRetry(new()
            {
                ShouldHandle = new PredicateBuilder().Handle<RavenException>(),
                Delay = TimeSpan.FromSeconds(1),
                MaxRetryAttempts = 2
            })));

Extension for Fallback

To facilitate the fallback to a different Data Provider you can use the FallbackToDataProvider() extension method in the FallbackAction.

For instance, to establish a fallback policy for a RavenDB data provider, ensuring that in the event of a RavenException, the Audit Events will be written to a file using a File data provider as a fallback:

using Audit.Polly;

var ravenDbProvider = new RavenDbDataProvider(...);
var fallbakDbProvider = new FileDataProvider(...);

Audit.Core.Configuration.Setup()
    .UsePolly(p => p
        .DataProvider(ravenDbProvider)
        .WithResilience(resilience => resilience
            .AddFallback(new()
            {
                ShouldHandle = new PredicateBuilder().Handle<RavenException>(),
                FallbackAction = args => args.FallbackToDataProvider(fallbakDbProvider)
            })));

Extension for hedging

To facilitate the hedging strategy you can use the FallbackToDataProvider() extension method in the ActionGenerator.

For instance, to establish a hedging policy for a data provider so in case of exceptions, the Audit Events will be written to a different data provider using a hedging strategy:

using Audit.Polly;

var primaryDataProvider = new SqlDataProvider(...);
var secondaryDataProvider = new FileDataProvider(...);

Audit.Core.Configuration.Setup()
    .UsePolly(p => p
        .DataProvider(primaryDataProvider)
        .WithResilience(r => r
            .AddHedging(new()
            {
                ShouldHandle = new PredicateBuilder().Handle<SqlException>(),
                MaxHedgedAttempts = 2,
                Delay = TimeSpan.Zero,
                ActionGenerator = args => args.FallbackToDataProvider(secondaryDataProvider)
            }));

Settings

  • DataProvider: The primary data provider to use.
  • WithResilience: The resilience strategy to apply to the primary data provider. It can be a single policy or a collection of policies.

Resilience Strategies

The following are some of the available resilience policies to apply to the primary data provider.

  • AddRetry: Adds a retry policy to the primary data provider.
  • AddFallback: Adds a fallback policy to the primary data provider.
  • AddCircuitBreaker: Adds a circuit breaker policy to the primary data provider.
  • AddTimeout: Adds a timeout policy to the primary data provider.
  • AddHedging: Adds a hedging policy to the primary data provider.

Please refer to Polly documentation for a complete list.