Skip to content

Commit b58e384

Browse files
Fix Exception handling (#198)
1 parent f186822 commit b58e384

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

examples/netFx/NLogGraylogHttp.Example.NetFx/App.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<startup>
77
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
88
</startup>
9-
<nlog throwExceptions="true" xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
9+
<nlog throwExceptions="false" internalLogToConsoleError="true" xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
1010
<extensions>
1111
<add assembly="NLog.Targets.GraylogHttp" />
1212
</extensions>

examples/netcore/NLogGraylogHttp.Example.NetCore/NLog.Config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8" ?>
2-
<nlog throwExceptions="true" xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
2+
<nlog throwExceptions="false" internalLogToConsoleError="true" xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
33
<extensions>
44
<add assembly="NLog.Targets.GraylogHttp"/>
55
</extensions>

src/NLog.Targets.GraylogHttp/GraylogHttpTarget.cs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using NLog.Config;
88
using Polly;
99
using Polly.CircuitBreaker;
10+
using Polly.Wrap;
1011

1112
namespace NLog.Targets.GraylogHttp
1213
{
@@ -15,7 +16,7 @@ public class GraylogHttpTarget : TargetWithContext
1516
{
1617
private HttpClient _httpClient;
1718
private Uri _requestAddress;
18-
private CircuitBreakerPolicy _policy;
19+
private PolicyWrap _policy;
1920

2021
public GraylogHttpTarget()
2122
{
@@ -68,13 +69,27 @@ protected override void InitializeTarget()
6869

6970
_httpClient.DefaultRequestHeaders.ExpectContinue = false; // Expect (100) Continue breaks the graylog server
7071

71-
_policy = Policy
72-
.Handle<Exception>()
72+
var waitAndRetryPolicy = Policy
73+
.Handle<Exception>(e => !(e is BrokenCircuitException))
74+
.WaitAndRetry(
75+
1,
76+
attempt => TimeSpan.FromMilliseconds(200),
77+
(exception, calculatedWaitDuration) =>
78+
{
79+
InternalLogger.Error(exception, "GraylogHttp(Name={0}): Graylog server error, the log messages were not sent to the server.", Name);
80+
});
81+
82+
var circuitBreakerPolicy = Policy
83+
.Handle<Exception>(e => !(e is BrokenCircuitException))
7384
.AdvancedCircuitBreaker(
7485
(double)FailureThreshold / 100,
7586
TimeSpan.FromSeconds(SamplingDurationSeconds),
7687
MinimumThroughput,
77-
TimeSpan.FromSeconds(FailureCooldownSeconds));
88+
TimeSpan.FromSeconds(FailureCooldownSeconds),
89+
(exception, span) => InternalLogger.Error(exception, "GraylogHttp(Name={0}): Circuit breaker Open", Name),
90+
() => InternalLogger.Info("GraylogHttp(Name={0}): Circuit breaker Reset", Name));
91+
92+
_policy = Policy.Wrap(waitAndRetryPolicy, circuitBreakerPolicy);
7893

7994
// Prefix the custom properties with underscore upfront, so we don't have to do it for each log-event
8095
foreach (TargetPropertyWithContext p in ContextProperties)
@@ -137,7 +152,7 @@ protected override void Write(LogEventInfo logEvent)
137152

138153
try
139154
{
140-
_policy.Execute(() =>
155+
_policy.ExecuteAndCapture(() =>
141156
{
142157
var content = new StringContent(messageBuilder.Render(logEvent.TimeStamp), Encoding.UTF8, "application/json");
143158
return _httpClient.PostAsync(_requestAddress, content).Result.EnsureSuccessStatusCode();
@@ -149,6 +164,15 @@ protected override void Write(LogEventInfo logEvent)
149164
"GraylogHttp(Name={0}): The Graylog server seems to be inaccessible, the log messages were not sent to the server.",
150165
Name);
151166
}
167+
#pragma warning disable CA1031 // Do not catch general exception types
168+
catch (Exception ex)
169+
#pragma warning restore CA1031 // Do not catch general exception types
170+
{
171+
InternalLogger.Error(
172+
ex,
173+
"GraylogHttp(Name={0}): Graylog server error, the log messages were not sent to the server.",
174+
Name);
175+
}
152176
}
153177

154178
/// <summary>

0 commit comments

Comments
 (0)