Skip to content

Commit

Permalink
Fix Exception handling (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinchilson authored Mar 3, 2021
1 parent f186822 commit b58e384
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion examples/netFx/NLogGraylogHttp.Example.NetFx/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<nlog throwExceptions="true" xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<nlog throwExceptions="false" internalLogToConsoleError="true" xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<extensions>
<add assembly="NLog.Targets.GraylogHttp" />
</extensions>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8" ?>
<nlog throwExceptions="true" xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<nlog throwExceptions="false" internalLogToConsoleError="true" xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<extensions>
<add assembly="NLog.Targets.GraylogHttp"/>
</extensions>
Expand Down
34 changes: 29 additions & 5 deletions src/NLog.Targets.GraylogHttp/GraylogHttpTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using NLog.Config;
using Polly;
using Polly.CircuitBreaker;
using Polly.Wrap;

namespace NLog.Targets.GraylogHttp
{
Expand All @@ -15,7 +16,7 @@ public class GraylogHttpTarget : TargetWithContext
{
private HttpClient _httpClient;
private Uri _requestAddress;
private CircuitBreakerPolicy _policy;
private PolicyWrap _policy;

public GraylogHttpTarget()
{
Expand Down Expand Up @@ -68,13 +69,27 @@ protected override void InitializeTarget()

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

_policy = Policy
.Handle<Exception>()
var waitAndRetryPolicy = Policy
.Handle<Exception>(e => !(e is BrokenCircuitException))
.WaitAndRetry(
1,
attempt => TimeSpan.FromMilliseconds(200),
(exception, calculatedWaitDuration) =>
{
InternalLogger.Error(exception, "GraylogHttp(Name={0}): Graylog server error, the log messages were not sent to the server.", Name);
});

var circuitBreakerPolicy = Policy
.Handle<Exception>(e => !(e is BrokenCircuitException))
.AdvancedCircuitBreaker(
(double)FailureThreshold / 100,
TimeSpan.FromSeconds(SamplingDurationSeconds),
MinimumThroughput,
TimeSpan.FromSeconds(FailureCooldownSeconds));
TimeSpan.FromSeconds(FailureCooldownSeconds),
(exception, span) => InternalLogger.Error(exception, "GraylogHttp(Name={0}): Circuit breaker Open", Name),
() => InternalLogger.Info("GraylogHttp(Name={0}): Circuit breaker Reset", Name));

_policy = Policy.Wrap(waitAndRetryPolicy, circuitBreakerPolicy);

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

try
{
_policy.Execute(() =>
_policy.ExecuteAndCapture(() =>
{
var content = new StringContent(messageBuilder.Render(logEvent.TimeStamp), Encoding.UTF8, "application/json");
return _httpClient.PostAsync(_requestAddress, content).Result.EnsureSuccessStatusCode();
Expand All @@ -149,6 +164,15 @@ protected override void Write(LogEventInfo logEvent)
"GraylogHttp(Name={0}): The Graylog server seems to be inaccessible, the log messages were not sent to the server.",
Name);
}
#pragma warning disable CA1031 // Do not catch general exception types
catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
{
InternalLogger.Error(
ex,
"GraylogHttp(Name={0}): Graylog server error, the log messages were not sent to the server.",
Name);
}
}

/// <summary>
Expand Down

0 comments on commit b58e384

Please sign in to comment.