Skip to content

Commit

Permalink
Micro optimization by using is null (#1070)
Browse files Browse the repository at this point in the history
  • Loading branch information
snakefoot authored Nov 24, 2024
1 parent e9da793 commit fdb38de
Show file tree
Hide file tree
Showing 39 changed files with 83 additions and 186 deletions.
2 changes: 1 addition & 1 deletion src/NLog.Web.AspNetCore/AspNetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ private static NLogLoggerProvider CreateNLogLoggerProvider(IServiceProvider serv

var configuration = SetupNLogConfigSettings(serviceProvider, hostConfiguration, provider.LogFactory);

if (configuration != null && (!ReferenceEquals(configuration, hostConfiguration) || options == null))
if (configuration != null && (!ReferenceEquals(configuration, hostConfiguration) || options is null))
{
provider.Configure(configuration.GetSection("Logging:NLog"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
var tlsHandshake = HttpContextAccessor.HttpContext.TryGetFeature<ITlsHandshakeFeature>();
if (tlsHandshake is null)
{
return;
}

switch (Property)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
var tlsTokenBinding = HttpContextAccessor.HttpContext.TryGetFeature<ITlsTokenBindingFeature>();
if (tlsTokenBinding is null)
{
return;
}

switch (Property)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,10 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
var httpRequestTrailers = HttpContextAccessor.HttpContext.TryGetFeature<IHttpRequestTrailersFeature>();
if (httpRequestTrailers is null)
{
return;
}

if(!httpRequestTrailers.Available)
{
if (!httpRequestTrailers.Available)
return;
}

var trailers = httpRequestTrailers.Trailers;
if (trailers?.Count > 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,15 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
var item = Item;
if (item is null)
{
return;
}

var application = HttpContextAccessor.HttpContext.Application;
if (application is null)
{
return;
}

var value = application[item];
if (value is null)
{
return;
}

if (!(ObjectPath is null))
{
Expand Down
16 changes: 8 additions & 8 deletions src/NLog.Web/NLogRequestLoggingModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,16 @@ void IHttpModule.Dispose()

private bool IsSlowHttpRequest(HttpContext httpContext)
{
if (httpContext != null)
if (httpContext is null)
return false;

var timestamp = httpContext.Timestamp;
if (timestamp > DateTime.MinValue)
{
var timestamp = httpContext.Timestamp;
if (timestamp > DateTime.MinValue)
var currentDuration = DateTime.UtcNow - timestamp.ToUniversalTime();
if (currentDuration > _durationThresholdMs)
{
var currentDuration = DateTime.UtcNow - timestamp.ToUniversalTime();
if (currentDuration > _durationThresholdMs)
{
return true;
}
return true;
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/NLog.Web/Targets/AspNetTrace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@ public AspNetTraceTarget()
protected override void Write(LogEventInfo logEvent)
{
HttpContext context = HttpContext.Current;

if (context == null)
{
if (context is null)
return;
}

if (logEvent.Level >= LogLevel.Warn)
{
Expand Down
17 changes: 4 additions & 13 deletions src/Shared/Internal/HttpContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,34 +107,25 @@ internal static T TryGetFeature<T>(this HttpContext context) where T : class
internal static string GetString(this ISession session, string key)
{
if (!session.TryGetValue(key, out var data))
{
return null;
}

if (data == null)
{
if (data is null)
return null;
}

if (data.Length == 0)
{
return string.Empty;
}

return Encoding.UTF8.GetString(data);
}

public static int? GetInt32(this ISession session, string key)
{
if (!session.TryGetValue(key, out var data))
{
return null;
}

if (data == null || data.Length < 4)
{
if (data is null || data.Length < 4)
return null;
}

return data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
}
#endif
Expand All @@ -143,7 +134,7 @@ internal static string GetString(this ISession session, string key)
internal static HttpSessionStateBase TryGetSession(this HttpContextBase context)
{
var session = context?.Session;
if (session == null)
if (session is null)
InternalLogger.Debug("HttpContext Session Lookup returned null");
return session;
}
Expand Down
8 changes: 3 additions & 5 deletions src/Shared/Internal/HttpHeaderCollectionValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal static class HttpHeaderCollectionValues
#if ASP_NET_CORE
internal static IEnumerable<KeyValuePair<string, string>> GetHeaderValues(IHeaderDictionary headers, List<string> itemNames, ISet<string> excludeNames)
{
bool checkForExclude = (itemNames == null || itemNames.Count == 0) && excludeNames?.Count > 0;
bool checkForExclude = (itemNames is null || itemNames.Count == 0) && excludeNames?.Count > 0;
var headerNames = itemNames?.Count > 0 ? itemNames : headers.Keys;
foreach (var headerName in headerNames)
{
Expand All @@ -32,18 +32,16 @@ internal static IEnumerable<KeyValuePair<string, string>> GetHeaderValues(IHeade
#else
internal static IEnumerable<KeyValuePair<string, string>> GetHeaderValues(NameValueCollection headers, List<string> itemNames, HashSet<string> excludeNames)
{
bool checkForExclude = (itemNames == null || itemNames.Count == 0) && excludeNames?.Count > 0;
bool checkForExclude = (itemNames is null || itemNames.Count == 0) && excludeNames?.Count > 0;
var headerNames = itemNames?.Count > 0 ? itemNames : headers.Keys.Cast<string>();
foreach (var headerName in headerNames)
{
if (checkForExclude && excludeNames.Contains(headerName))
continue;

var headerValue = headers[headerName];
if (headerValue == null)
{
if (headerValue is null)
continue;
}

yield return new KeyValuePair<string, string>(headerName, headerValue);
}
Expand Down
4 changes: 0 additions & 4 deletions src/Shared/LayoutRenderers/AspNetItemValueLayoutRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,11 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
var item = Item;
if (string.IsNullOrEmpty(item))
{
return;
}

var httpContext = HttpContextAccessor.HttpContext;
if (httpContext is null)
{
return;
}

object value = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public class AspNetRequestContentTypeLayoutRenderer : AspNetLayoutRendererBase
/// <inheritdoc/>
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
var request = HttpContextAccessor.HttpContext.TryGetRequest();
var contentType = request?.ContentType;
var httpRequest = HttpContextAccessor.HttpContext.TryGetRequest();
var contentType = httpRequest?.ContentType;
builder.Append(contentType);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class AspNetRequestDurationLayoutRenderer : AspNetLayoutRendererBase
/// <inheritdoc/>
protected override void InitializeLayoutRenderer()
{
if (DurationMsFormat == null && string.IsNullOrEmpty(Format) && ReferenceEquals(Culture, CultureInfo.InvariantCulture))
if (DurationMsFormat is null && string.IsNullOrEmpty(Format) && ReferenceEquals(Culture, CultureInfo.InvariantCulture))
{
System.Threading.Interlocked.CompareExchange(ref DurationMsFormat, Enumerable.Range(0, 1000).Select(i => i.ToString(CultureInfo.InvariantCulture)).ToArray(), null);
}
Expand All @@ -64,10 +64,8 @@ protected override void InitializeLayoutRenderer()
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
var context = HttpContextAccessor.HttpContext;
if (context == null)
{
if (context is null)
return;
}

#if ASP_NET_CORE && !NETCOREAPP3_0_OR_GREATER
_scopeTiming?.Render(logEvent, builder);
Expand Down
6 changes: 3 additions & 3 deletions src/Shared/LayoutRenderers/AspNetRequestHostLayoutRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public class AspNetRequestHostLayoutRenderer : AspNetLayoutRendererBase
/// <inheritdoc/>
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
var request = HttpContextAccessor.HttpContext.TryGetRequest();
var httpRequest = HttpContextAccessor.HttpContext.TryGetRequest();
#if ASP_NET_CORE
var host = request?.Host.ToString();
var host = httpRequest?.Host.ToString();
#else
var host = request?.UserHostName?.ToString();
var host = httpRequest?.UserHostName?.ToString();
#endif
builder.Append(host);
}
Expand Down
10 changes: 4 additions & 6 deletions src/Shared/LayoutRenderers/AspNetRequestIpLayoutRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,16 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
var httpContext = HttpContextAccessor.HttpContext;

var request = httpContext.TryGetRequest();
if (request == null)
{
var httpRequest = httpContext.TryGetRequest();
if (httpRequest is null)
return;
}

var ip = CheckForwardedForHeader && ForwardedForHeader != null ? TryLookupForwardHeader(request, logEvent) : string.Empty;
var ip = CheckForwardedForHeader && ForwardedForHeader != null ? TryLookupForwardHeader(httpRequest, logEvent) : string.Empty;

if (string.IsNullOrEmpty(ip))
{
#if !ASP_NET_CORE
ip = request.ServerVariables?["REMOTE_ADDR"];
ip = httpRequest.ServerVariables?["REMOTE_ADDR"];
#else
ip = httpContext.Connection?.RemoteIpAddress?.ToString();
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,15 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)

#if ASP_NET_CORE
var connection = httpContext.TryGetConnection();
if (connection == null)
{
if (connection is null)
return;
}

builder.Append(connection.LocalIpAddress?.ToString());
#else
var request = httpContext.TryGetRequest();
if (request == null)
{
if (request is null)
return;
}

builder.Append(request.ServerVariables?["LOCAL_ADDR"]);
#endif
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,15 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
var httpContext = HttpContextAccessor.HttpContext;
#if ASP_NET_CORE
var connection = httpContext.TryGetConnection();
if (connection == null)
{
if (connection is null)
return;
}

builder.Append(connection.LocalPort);
#else
var request = httpContext.TryGetRequest();
if (request == null)
{
if (request is null)
return;
}

builder.Append(request.ServerVariables?["LOCAL_PORT"]);
#endif
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
}

var items = HttpContextAccessor.HttpContext?.Items;
if (items == null || items.Count == 0)
{
if (items is null || items.Count == 0)
return;
}

#if !ASP_NET_CORE
if (items.Contains(NLogPostedRequestBodyKey))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,15 @@ public AspNetQueryStringLayoutRenderer()
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
var httpRequest = HttpContextAccessor.HttpContext.TryGetRequest();
if (httpRequest == null)
{
if (httpRequest is null)
return;
}

#if !ASP_NET_CORE
var queryStrings = httpRequest.QueryString;
#else
var queryStrings = httpRequest.Query;
#endif
if (queryStrings == null || queryStrings.Count == 0)
if (queryStrings is null || queryStrings.Count == 0)
return;

var queryStringKeys = Items?.Count > 0 ? Items :
Expand All @@ -82,7 +80,7 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
#else
queryStrings.Keys;
#endif
bool checkForExclude = (Items == null || Items.Count == 0) && Exclude?.Count > 0;
bool checkForExclude = (Items is null || Items.Count == 0) && Exclude?.Count > 0;
var pairs = GetQueryStringValues(queryStrings, queryStringKeys, checkForExclude, Exclude);
SerializePairs(pairs, builder, logEvent);
}
Expand Down
4 changes: 1 addition & 3 deletions src/Shared/LayoutRenderers/AspNetRequestReferrerRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ public class AspNetRequestReferrerRenderer : AspNetLayoutRendererBase
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
var httpRequest = HttpContextAccessor.HttpContext.TryGetRequest();
if (httpRequest == null)
{
if (httpRequest is null)
return;
}

var referrer = string.Empty;
#if !ASP_NET_CORE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,15 @@ protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)

#if ASP_NET_CORE
var connection = httpContext.TryGetConnection();
if (connection == null)
{
if (connection is null)
return;
}

builder.Append(connection.RemotePort);
#else
var request = httpContext.TryGetRequest();
if (request == null)
{
if (request is null)
return;
}

builder.Append(request.ServerVariables?["REMOTE_PORT"]);
#endif
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ public class AspNetRequestRouteParametersRenderer : AspNetLayoutMultiValueRender
protected override void DoAppend(StringBuilder builder, LogEventInfo logEvent)
{
var context = HttpContextAccessor.HttpContext;
if (context == null)
{
if (context is null)
return;
}

var pairs = GetPairs(context, Items);
if (pairs != null)
Expand Down
Loading

0 comments on commit fdb38de

Please sign in to comment.