From c1c3aed82cb79da0b3c5af32b1a3462141d1395f Mon Sep 17 00:00:00 2001 From: rklec <71322635+rklec@users.noreply.github.com> Date: Fri, 30 Aug 2024 15:00:50 +0200 Subject: [PATCH 1/2] Add syntax highlighting to README.md --- README.md | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 1c2bf89..a985976 100644 --- a/README.md +++ b/README.md @@ -10,19 +10,23 @@ For more background on Polly see the [main Polly repo](https://github.com/App-vN Define an `Action> logAction` to log exceptions or results. The `Context` input parameter allows filtering based on Polly's in-built [execution metadata](https://github.com/App-vNext/Polly/wiki/Keys-And-Context-Data) or context you pass in to the execution. +```csharp Action> logAction = (logger, context, outcome) => { logger.LogError("The call resulted in outcome: {happened}", outcome.Exception?.Message ?? outcome.Result.StatusCode.ToString()); } +``` Define a `Func loggerProvider` to select an `ILogger` based on `Context`. This can use the extension method `Context.GetLogger()` defined on `Polly.Context`, if execution dispatch uses the `Context.WithLogger(ILogger)` overload, as demonstrated in the ConsoleApp example. Configure the policy: - var loggingPolicy = Policy - .Handle() - .OrResult((int)response.StatusCode >= 500 || response.StatusCode == HttpStatusCode.RequestTimeout) - .AsyncLog(ctx => ctx.GetLogger(), logAction); +```csharp +var loggingPolicy = Policy + .Handle() + .OrResult((int)response.StatusCode >= 500 || response.StatusCode == HttpStatusCode.RequestTimeout) + .AsyncLog(ctx => ctx.GetLogger(), logAction); +``` The policy would typically be combined with other policies in a PolicyWrap - see below. @@ -32,9 +36,11 @@ Define a `logAction` and `loggerProvider` as above. Configure the policy, for example: - var loggingPolicy = Policy - .Handle() - .Log(ctx => ctx.GetLogger(), logAction); +```csharp +var loggingPolicy = Policy + .Handle() + .Log(ctx => ctx.GetLogger(), logAction); +``` ### Using LoggingPolicy in PolicyWrap From 12f277b24e81a80abb2a580dc8fa7ffff504b9f7 Mon Sep 17 00:00:00 2001 From: rklec <71322635+rklec@users.noreply.github.com> Date: Fri, 30 Aug 2024 15:01:13 +0200 Subject: [PATCH 2/2] Fix missing indentation --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a985976..fe8817c 100644 --- a/README.md +++ b/README.md @@ -11,10 +11,10 @@ For more background on Polly see the [main Polly repo](https://github.com/App-vN Define an `Action> logAction` to log exceptions or results. The `Context` input parameter allows filtering based on Polly's in-built [execution metadata](https://github.com/App-vNext/Polly/wiki/Keys-And-Context-Data) or context you pass in to the execution. ```csharp - Action> logAction = (logger, context, outcome) => - { - logger.LogError("The call resulted in outcome: {happened}", outcome.Exception?.Message ?? outcome.Result.StatusCode.ToString()); - } +Action> logAction = (logger, context, outcome) => +{ + logger.LogError("The call resulted in outcome: {happened}", outcome.Exception?.Message ?? outcome.Result.StatusCode.ToString()); +} ``` Define a `Func loggerProvider` to select an `ILogger` based on `Context`. This can use the extension method `Context.GetLogger()` defined on `Polly.Context`, if execution dispatch uses the `Context.WithLogger(ILogger)` overload, as demonstrated in the ConsoleApp example.