Skip to content

Commit

Permalink
[dotnet] Improve logging performance when it is disabled (#13464)
Browse files Browse the repository at this point in the history
* Use IsEnabledFor before allocate log message

* Rename to `IsEnabled`

* Simplify determination of logger's level
  • Loading branch information
nvborisenko authored Jan 19, 2024
1 parent 7bf6286 commit e8f02e9
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 11 deletions.
5 changes: 4 additions & 1 deletion dotnet/src/webdriver/DriverService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,10 @@ protected virtual bool IsInitialized
}
catch (Exception ex) when (ex is HttpRequestException || ex is TaskCanceledException)
{
logger.Trace(ex.ToString());
if (logger.IsEnabled(LogEventLevel.Trace))
{
logger.Trace(ex.ToString());
}
}

return isInitialized;
Expand Down
5 changes: 4 additions & 1 deletion dotnet/src/webdriver/Internal/FileUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ public static void DeleteDirectory(string directoryToDelete)

if (Directory.Exists(directoryToDelete))
{
logger.Trace($"Unable to delete directory '{directoryToDelete}'");
if (logger.IsEnabled(LogEventLevel.Trace))
{
logger.Trace($"Unable to delete directory '{directoryToDelete}'");
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions dotnet/src/webdriver/Internal/Logging/ILogContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ public interface ILogContext : IDisposable
/// <returns>An instance of <see cref="ILogger"/> for the specified type.</returns>
internal ILogger GetLogger(Type type);

/// <summary>
/// Checks whether logs emitting is enabled for a logger and a log event level.
/// </summary>
/// <param name="logger">The specified logger instance to be checked.</param>
/// <param name="level">The specified log event level to be checked.</param>
/// <returns><c>true</c> if log messages emmiting is enabled for the specified logger and log event level, otherwise <c>false</c>.</returns>
internal bool IsEnabled(ILogger logger, LogEventLevel level);

/// <summary>
/// Emits a log message using the specified logger, log level, and message.
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions dotnet/src/webdriver/Internal/Logging/ILogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,12 @@ internal interface ILogger
/// Gets the type of the logger issuer.
/// </summary>
Type Issuer { get; }

/// <summary>
/// Checks whether logs emitting is enabled for this logger and a log event level.
/// </summary>
/// <param name="level">The specified log event level to be checked.</param>
/// <returns><c>true</c> if log messages emmiting is enabled for the specified log event level, otherwise <c>false</c>.</returns>
bool IsEnabled(LogEventLevel level);
}
}
7 changes: 6 additions & 1 deletion dotnet/src/webdriver/Internal/Logging/LogContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,14 @@ public ILogger GetLogger(Type type)
return _loggers.GetOrAdd(type, _ => new Logger(type, _level));
}

public bool IsEnabled(ILogger logger, LogEventLevel level)
{
return Handlers != null && level >= _level && level >= logger.Level;
}

public void EmitMessage(ILogger logger, LogEventLevel level, string message)
{
if (Handlers != null && level >= _level && level >= GetLogger(logger.Issuer).Level)
if (IsEnabled(logger, level))
{
var logEvent = new LogEvent(logger.Issuer, DateTimeOffset.Now, level, message);

Expand Down
5 changes: 5 additions & 0 deletions dotnet/src/webdriver/Internal/Logging/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public void Error(string message)
LogMessage(LogEventLevel.Error, message);
}

public bool IsEnabled(LogEventLevel level)
{
return Log.CurrentContext.IsEnabled(this, level);
}

private void LogMessage(LogEventLevel level, string message)
{
Log.CurrentContext.EmitMessage(this, level, message);
Expand Down
22 changes: 16 additions & 6 deletions dotnet/src/webdriver/Remote/HttpCommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using OpenQA.Selenium.Internal;
using OpenQA.Selenium.Internal.Logging;
Expand Down Expand Up @@ -166,7 +164,10 @@ public virtual Response Execute(Command commandToExecute)
throw new ArgumentNullException(nameof(commandToExecute), "commandToExecute cannot be null");
}

_logger.Debug($"Executing command: {commandToExecute}");
if (_logger.IsEnabled(LogEventLevel.Debug))
{
_logger.Debug($"Executing command: {commandToExecute}");
}

HttpCommandInfo info = this.commandInfoRepository.GetCommandInfo<HttpCommandInfo>(commandToExecute.Name);
if (info == null)
Expand Down Expand Up @@ -198,7 +199,10 @@ public virtual Response Execute(Command commandToExecute)

Response toReturn = this.CreateResponse(responseInfo);

_logger.Debug($"Response: {toReturn}");
if (_logger.IsEnabled(LogEventLevel.Debug))
{
_logger.Debug($"Response: {toReturn}");
}

return toReturn;
}
Expand Down Expand Up @@ -279,11 +283,17 @@ private async Task<HttpResponseInfo> MakeHttpRequest(HttpRequestInfo requestInfo
requestMessage.Content.Headers.ContentType = contentTypeHeader;
}

_logger.Trace($">> {requestMessage}");
if (_logger.IsEnabled(LogEventLevel.Trace))
{
_logger.Trace($">> {requestMessage}");
}

using (HttpResponseMessage responseMessage = await this.client.SendAsync(requestMessage).ConfigureAwait(false))
{
_logger.Trace($"<< {responseMessage}");
if (_logger.IsEnabled(LogEventLevel.Trace))
{
_logger.Trace($"<< {responseMessage}");
}

HttpResponseInfo httpResponseInfo = new HttpResponseInfo();
httpResponseInfo.Body = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
Expand Down
5 changes: 4 additions & 1 deletion dotnet/src/webdriver/Safari/SafariDriverService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ protected override bool IsInitialized
// check.
catch (Exception ex) when (ex is HttpRequestException || ex is TaskCanceledException)
{
logger.Trace(ex.ToString());
if (logger.IsEnabled(LogEventLevel.Trace))
{
logger.Trace(ex.ToString());
}
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion dotnet/src/webdriver/SeleniumManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ public static string DriverPath(DriverOptions options)

var driverPath = (string)output["driver_path"];

_logger.Trace($"Driver path: {driverPath}");
if (_logger.IsEnabled(LogEventLevel.Trace))
{
_logger.Trace($"Driver path: {driverPath}");
}

return driverPath;
}
Expand Down

0 comments on commit e8f02e9

Please sign in to comment.