Skip to content

Commit

Permalink
Fix Sonar code smells (#989)
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot authored Sep 28, 2023
1 parent d4c9181 commit 9eeb664
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private bool DefaultCapture(HttpContext context)

if (!context.HasAllowedContentType(AllowContentTypes))
{
InternalLogger.Debug("NLogRequestPostedBodyMiddleware: HttpContext.Request.ContentType={0}", context?.Request?.ContentType);
InternalLogger.Debug("NLogRequestPostedBodyMiddleware: HttpContext.Request.ContentType={0}", context.Request.ContentType);
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/NLog.Web/NLogRequestPostedBodyModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private bool ShouldCaptureRequestBody(HttpContext context)

if (!context.HasAllowedContentType(AllowContentTypes))
{
InternalLogger.Debug("NLogRequestPostedBodyModule: HttpContext.Request.ContentType={0}", context?.Request?.ContentType);
InternalLogger.Debug("NLogRequestPostedBodyModule: HttpContext.Request.ContentType={0}", context.Request.ContentType);
return false;
}

Expand Down
8 changes: 4 additions & 4 deletions src/Shared/LayoutRenderers/AspNetItemValueLayoutRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
return;
}

var context = HttpContextAccessor.HttpContext;
if (context is null)
var httpContext = HttpContextAccessor.HttpContext;
if (httpContext is null)
{
return;
}
Expand All @@ -105,14 +105,14 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
#pragma warning disable CS0618 // Type or member is obsolete
if (EvaluateAsNestedProperties)
{
value = PropertyReader.GetValue(item, context?.Items, (items, key) => LookupItemValue(items, key), true);
value = PropertyReader.GetValue(item, httpContext.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);
value = LookupItemValue(httpContext.Items, item);
if (value is null)
return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
var httpContext = HttpContextAccessor.HttpContext;
#if ASP_NET_CORE
var connection = httpContext.Connection;
if (connection == null)
{
return;
}
builder.Append(connection.ClientCertificate?.ToString(Verbose));
builder.Append(connection?.ClientCertificate?.ToString(Verbose));
#else
var certificate = httpContext.Request.ClientCertificate;
if (certificate == null)
Expand All @@ -46,7 +42,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
}
// Convert to an X509Certificate2, which does have the proper overridden ToString() method.
// HttpClientCertificate class only use object.ToString() which is useless.
builder.Append(new X509Certificate2(certificate.Certificate)?.ToString(Verbose));
builder.Append(new X509Certificate2(certificate.Certificate).ToString(Verbose));
#endif
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
#if !ASP_NET_CORE
var formKeys = httpRequest?.Form?.Keys;
#else
var formKeys = httpRequest?.HasFormContentType == true ? httpRequest?.Form?.Keys : null;
var formKeys = httpRequest?.HasFormContentType == true ? httpRequest.Form?.Keys : null;
#endif
if (formKeys?.Count > 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
#endif

var contextSession = context?.TryGetSession();
var contextSession = context.TryGetSession();
if (contextSession == null)
{
return;
Expand Down
5 changes: 1 addition & 4 deletions src/Shared/LayoutRenderers/AspNetUserClaimLayoutRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
else
{
var claim = GetClaim(claimsPrincipal, ClaimType);
if (claim != null)
{
builder.Append(claim?.Value);
}
builder.Append(claim?.Value);
}
}
catch (ObjectDisposedException ex)
Expand Down

0 comments on commit 9eeb664

Please sign in to comment.