Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Serilog.HttpClient/RequestLoggingOptions.cs
  • Loading branch information
alirezavafi committed Jul 25, 2024
2 parents b073500 + dd7c26f commit 1658ecf
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
37 changes: 37 additions & 0 deletions src/Serilog.HttpClient.Tests/LoggingDelegatingHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,43 @@ public void Test_Log_Request()
Assert.Equal("*", response[nameof(HttpClientResponseContext.Headers)].Value.ToDictionary().First().Value.ToScalar());
}
}

[Fact]
public void Test_Log_Filter_Request()
{
MockResponse(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent("this is the response body"),
Headers =
{
ETag = EntityTagHeaderValue.Any
}
});

var client = CreateHttpClient(new RequestLoggingOptions
{
ResponseBodyLogMode = LogMode.LogAll,
LogFilter = (req, resp, elapsedMs, ex) => req.RequestUri.ToString() != "https://example.com/path?query=1"
});

using (TestCorrelator.CreateContext())
{
client.SendAsync(new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = new Uri("https://example.com/path?query=1"),
Content = new StringContent("this is the request body"),
Headers =
{
Referrer = new Uri("https://example.com/referrer")
}
});

var logEvents = TestCorrelator.GetLogEventsFromCurrentContext();
Assert.Empty(logEvents);
}
}

[Fact]
public void Test_Log_Request_With_Customized_Log_Entry()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand Down
2 changes: 1 addition & 1 deletion src/Serilog.HttpClient/LoggingDelegatingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private async Task LogRequest(HttpRequestMessage req, HttpResponseMessage resp,
{
var level = _options.GetLevel(req, resp, elapsedMs, ex);
if (!_logger.IsEnabled(level)) return;

if (!_options.LogFilter(req, resp, elapsedMs, ex)) return;
var requestBodyText = string.Empty;
var responseBodyText = string.Empty;

Expand Down
13 changes: 12 additions & 1 deletion src/Serilog.HttpClient/RequestLoggingOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public class RequestLoggingOptions
/// </value>
public Func<HttpRequestMessage, HttpResponseMessage, double, Exception, LogEventLevel> GetLevel { get; set; }

/// <summary>
/// A function returning weather request should be logged based on the <see cref="HttpRequestMessage"/>/<see cref="HttpResponseMessage"/> information,
/// the number of elapsed milliseconds required for handling the request, and an <see cref="Exception" /> if one was thrown.
/// </summary>
public Func<HttpRequestMessage, HttpResponseMessage, double, Exception, bool> LogFilter { get; set; }

/// <summary>
/// The logger through which request completion events will be logged. The default is to use the
/// static <see cref="Log"/> class.
Expand Down Expand Up @@ -110,6 +116,12 @@ public RequestLoggingOptions()
GetLevel = DefaultGetLevel;
MessageTemplate = DefaultRequestCompletionMessageTemplate;
GetMessageTemplateProperties = DefaultGetMessageTemplateProperties;
LogFilter = DefaultLogFilter;
}

private static bool DefaultLogFilter(HttpRequestMessage req, HttpResponseMessage resp, double elapsedMs, Exception ex)
{
return true;
}

private static LogEventLevel DefaultGetLevel(HttpRequestMessage req, HttpResponseMessage resp, double elapsedMs, Exception ex)
Expand All @@ -135,6 +147,5 @@ static IEnumerable<LogEventProperty> DefaultGetMessageTemplateProperties(HttpCli

return Array.Empty<LogEventProperty>();
}

}
}

0 comments on commit 1658ecf

Please sign in to comment.