Skip to content

Commit

Permalink
AspNetRequestTlsTokenBindingLayoutRenderer - Improve code-coverage (#902
Browse files Browse the repository at this point in the history
)
  • Loading branch information
snakefoot authored Dec 13, 2022
1 parent c5768c1 commit fb9c5a4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,13 @@ public class AspNetRequestTlsTokenBindingLayoutRenderer : AspNetLayoutRendererBa
/// <inheritdoc/>
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
var features = HttpContextAccessor.HttpContext.TryGetFeatureCollection();
if (features == null)
{
return;
}

var tlsTokenBinding = features.Get<ITlsTokenBindingFeature>();
var tlsTokenBinding = HttpContextAccessor.HttpContext.TryGetFeatureCollection()?.Get<ITlsTokenBindingFeature>();
if (tlsTokenBinding == null)
{
return;
}

switch(Property)
switch (Property)
{
case TlsTokenBindingProperty.Referrer:
builder.Append(ToFormattedString(tlsTokenBinding.GetReferredTokenBindingId()));
Expand All @@ -66,18 +60,16 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)

private string ToFormattedString(byte[] bytes)
{
if(bytes == null || bytes.Length == 0)
{
return null;
}

switch (Format)
if (bytes?.Length > 0)
{
case ByteArrayFormatProperty.Base64:
return Convert.ToBase64String(bytes);
switch (Format)
{
case ByteArrayFormatProperty.Base64:
return Convert.ToBase64String(bytes);

case ByteArrayFormatProperty.Hex:
return BitConverter.ToString(bytes);
case ByteArrayFormatProperty.Hex:
return BitConverter.ToString(bytes);
}
}

return null;
Expand Down
17 changes: 10 additions & 7 deletions src/Shared/LayoutRenderers/AspNetItemValueLayoutRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,25 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)

object value = null;

if (ObjectPath is null)
{
#pragma warning disable CS0618 // Type or member is obsolete
value = PropertyReader.GetValue(item, context?.Items, (items, key) => LookupItemValue(items, key), EvaluateAsNestedProperties);
#pragma warning restore CS0618 // Type or member is obsolete
if (EvaluateAsNestedProperties)
{
value = PropertyReader.GetValue(item, context?.Items, (items, key) => LookupItemValue(items, key), true);
if (value is null)
return;
}
#pragma warning restore CS0618 // Type or member is obsolete
else
{
value = LookupItemValue(context?.Items, item);
if (value is null)
return;

if (!_objectPathRenderer.TryGetPropertyValue(value, out value))
return;

if (ObjectPath != null)
{
if (!_objectPathRenderer.TryGetPropertyValue(value, out value))
return;
}
}

var formatProvider = GetFormatProvider(logEvent, Culture);
Expand Down
15 changes: 9 additions & 6 deletions src/Shared/LayoutRenderers/AspNetSessionValueLayoutRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,22 +142,25 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)

object value = null;

if (ObjectPath is null)
{
#pragma warning disable CS0618 // Type or member is obsolete
value = PropertyReader.GetValue(item, contextSession, _sessionValueLookup, EvaluateAsNestedProperties);
#pragma warning restore CS0618 // Type or member is obsolete
if (EvaluateAsNestedProperties)
{
value = PropertyReader.GetValue(item, contextSession, _sessionValueLookup, true);
if (value is null)
return;
}
#pragma warning restore CS0618 // Type or member is obsolete
else
{
value = _sessionValueLookup(contextSession, item);
if (value is null)
return;

if (!_objectPathRenderer.TryGetPropertyValue(value, out value))
return;
if (ObjectPath != null)
{
if (!_objectPathRenderer.TryGetPropertyValue(value, out value))
return;
}
}

var formatProvider = GetFormatProvider(logEvent, Culture);
Expand Down

0 comments on commit fb9c5a4

Please sign in to comment.