Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added optional propertyName argument to WithRequestHeader #25

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ or in `appsettings.json` file:
"Serilog": {
"MinimumLevel": "Debug",
"Using": [ "Serilog.Enrichers.ClientInfo" ],
"Enrich": [
"WithClientIp",
"Enrich": [
"WithClientIp",
"WithCorrelationId",
{
"Name": "WithRequestHeader",
{
"Name": "WithRequestHeader",
"Args": { "headerName": "User-Agent"}
}
],
Expand All @@ -57,7 +57,7 @@ or
"Serilog": {
"MinimumLevel": "Debug",
"Using": [ "Serilog.Enrichers.ClientInfo" ],
"Enrich": [
"Enrich": [
{
"Name": "WithClientIp",
"Args": {
Expand All @@ -83,7 +83,7 @@ or
"Serilog": {
"MinimumLevel": "Debug",
"Using": [ "Serilog.Enrichers.ClientInfo" ],
"Enrich": [
"Enrich": [
{
"Name": "WithCorrelationId",
"Args": {
Expand All @@ -109,7 +109,7 @@ or
"Serilog": {
"MinimumLevel": "Debug",
"Using": [ "Serilog.Enrichers.ClientInfo" ],
"Enrich": [
"Enrich": [
{
"Name": "WithRequestHeader",
"Args": {
Expand All @@ -121,6 +121,13 @@ or
"Args": {
"headerName": "Connection"
}
},
{
"Name": "WithRequestHeader",
"Args": {
"headerName": "Content-Length",
"propertyName": "RequestLength"
}
}
],
}
Expand Down Expand Up @@ -160,6 +167,7 @@ namespace MyWebApp
.Enrich.WithClientIp()
.Enrich.WithCorrelationId()
.Enrich.WithRequestHeader("header-name")
.Enrich.WithRequestHeader("another-header-name", "some-property-name")
.CreateLogger();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@
private readonly string _headerKey;
private readonly IHttpContextAccessor _contextAccessor;

public ClientHeaderEnricher(string headerKey)
: this(headerKey, new HttpContextAccessor())
public ClientHeaderEnricher(string headerKey, string propertyName)

Check warning on line 22 in src/Serilog.Enrichers.ClientInfo/Enrichers/ClientHeaderEnricher.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'ClientHeaderEnricher.ClientHeaderEnricher(string, string)'

Check warning on line 22 in src/Serilog.Enrichers.ClientInfo/Enrichers/ClientHeaderEnricher.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'ClientHeaderEnricher.ClientHeaderEnricher(string, string)'
: this(headerKey, propertyName, new HttpContextAccessor())
{
}

internal ClientHeaderEnricher(string headerKey, IHttpContextAccessor contextAccessor)
internal ClientHeaderEnricher(string headerKey, string propertyName, IHttpContextAccessor contextAccessor)
{
_headerKey = headerKey;
_propertyName = headerKey.Replace("-", "");
_propertyName = string.IsNullOrWhiteSpace(propertyName)
? headerKey.Replace("-", "")
: propertyName;
_clientHeaderItemKey = $"Serilog_{headerKey}";
_contextAccessor = contextAccessor;
}
Expand All @@ -37,7 +39,7 @@
_contextAccessor = contextAccessor;
}

public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)

Check warning on line 42 in src/Serilog.Enrichers.ClientInfo/Enrichers/ClientHeaderEnricher.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'ClientHeaderEnricher.Enrich(LogEvent, ILogEventPropertyFactory)'
{
var httpContext = _contextAccessor.HttpContext;
if (httpContext == null)
Expand All @@ -57,4 +59,4 @@

logEvent.AddPropertyIfAbsent(logProperty);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ public static LoggerConfiguration WithCorrelationId(
/// Registers the HTTP request header enricher to enrich logs with the header value.
/// </summary>
/// <param name="enrichmentConfiguration">The enrichment configuration.</param>
/// <param name="propertyName">The property name of log</param>
/// <param name="headerName">The header name to log its value</param>
/// <exception cref="ArgumentNullException">enrichmentConfiguration</exception>
/// <exception cref="ArgumentNullException">headerName</exception>
/// <returns>The logger configuration so that multiple calls can be chained.</returns>
public static LoggerConfiguration WithRequestHeader(this LoggerEnrichmentConfiguration enrichmentConfiguration, string headerName)
public static LoggerConfiguration WithRequestHeader(this LoggerEnrichmentConfiguration enrichmentConfiguration,
string headerName, string propertyName = null)
{
if (enrichmentConfiguration == null)
{
Expand All @@ -89,6 +91,6 @@ public static LoggerConfiguration WithRequestHeader(this LoggerEnrichmentConfigu
throw new ArgumentNullException(nameof(headerName));
}

return enrichmentConfiguration.With(new ClientHeaderEnricher(headerName));
return enrichmentConfiguration.With(new ClientHeaderEnricher(propertyName, headerName));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,33 @@ public ClientHeaderEnricherTests()
_contextAccessor = Substitute.For<IHttpContextAccessor>();
_contextAccessor.HttpContext.Returns(httpContext);
}

[Fact]
public void EnrichLogWithClientHeader_WhenHttpRequestContainHeader_ShouldCreateNamedHeaderValueProperty()
{
// Arrange
var headerKey = "RequestId";
var propertyName = "HttpRequestId";
var headerValue = Guid.NewGuid().ToString();
_contextAccessor.HttpContext.Request.Headers.Add(headerKey, headerValue);

var clientHeaderEnricher = new ClientHeaderEnricher(headerKey, propertyName, _contextAccessor);

LogEvent evt = null;
var log = new LoggerConfiguration()
.Enrich.With(clientHeaderEnricher)
.WriteTo.Sink(new DelegatingSink(e => evt = e))
.CreateLogger();

// Act
log.Information(@"First testing log enricher.");
log.Information(@"Second testing log enricher.");

// Assert
Assert.NotNull(evt);
Assert.True(evt.Properties.ContainsKey(propertyName));
Assert.Equal(headerValue, evt.Properties[propertyName].LiteralValue().ToString());
}

[Fact]
public void EnrichLogWithClientHeader_WhenHttpRequestContainHeader_ShouldCreateHeaderValueProperty()
Expand All @@ -25,7 +52,7 @@ public void EnrichLogWithClientHeader_WhenHttpRequestContainHeader_ShouldCreateH
var headerValue = Guid.NewGuid().ToString();
_contextAccessor.HttpContext.Request.Headers.Add(headerKey, headerValue);

var clientHeaderEnricher = new ClientHeaderEnricher(headerKey, _contextAccessor);
var clientHeaderEnricher = new ClientHeaderEnricher(headerKey, propertyName:string.Empty, _contextAccessor);

LogEvent evt = null;
var log = new LoggerConfiguration()
Expand Down Expand Up @@ -54,8 +81,8 @@ public void EnrichLogWithMulitpleClientHeaderEnricher_WhenHttpRequestContainHead
_contextAccessor.HttpContext.Request.Headers.Add(headerKey1, headerValue1);
_contextAccessor.HttpContext.Request.Headers.Add(headerKey2, headerValue2);

var clientHeaderEnricher1 = new ClientHeaderEnricher(headerKey1, _contextAccessor);
var clientHeaderEnricher2 = new ClientHeaderEnricher(headerKey2, _contextAccessor);
var clientHeaderEnricher1 = new ClientHeaderEnricher(headerKey1, propertyName:string.Empty, _contextAccessor);
var clientHeaderEnricher2 = new ClientHeaderEnricher(headerKey2, propertyName:string.Empty, _contextAccessor);

LogEvent evt = null;
var log = new LoggerConfiguration()
Expand All @@ -81,7 +108,7 @@ public void EnrichLogWithClientHeader_WhenHttpRequestNotContainHeader_ShouldCrea
{
// Arrange
var headerKey = "RequestId";
var clientHeaderEnricher = new ClientHeaderEnricher(headerKey, _contextAccessor);
var clientHeaderEnricher = new ClientHeaderEnricher(headerKey, propertyName:string.Empty, _contextAccessor);

LogEvent evt = null;
var log = new LoggerConfiguration()
Expand Down Expand Up @@ -114,4 +141,4 @@ public void WithRequestHeader_ThenLoggerIsCalled_ShouldNotThrowException()
// Assert
Assert.Null(exception);
}
}
}
Loading