From 09e156d7e31b2769f56559783f2da084a9d49fe2 Mon Sep 17 00:00:00 2001 From: dudu Date: Thu, 12 Jan 2023 08:08:18 +0800 Subject: [PATCH] Check if the given LogLevel is enabled --- src/Enyim.Caching/Memcached/MemcachedNode.cs | 53 ++++++++++++++++---- src/Enyim.Caching/MemcachedClient.cs | 29 +++++++++-- 2 files changed, 68 insertions(+), 14 deletions(-) diff --git a/src/Enyim.Caching/Memcached/MemcachedNode.cs b/src/Enyim.Caching/Memcached/MemcachedNode.cs index 256373b6..3989b932 100755 --- a/src/Enyim.Caching/Memcached/MemcachedNode.cs +++ b/src/Enyim.Caching/Memcached/MemcachedNode.cs @@ -160,7 +160,11 @@ public IPooledSocketResult Acquire() var startTime = DateTime.Now; _internalPoolImpl.InitPool(); _isInitialized = true; - _logger.LogInformation("MemcachedInitPool-cost: {0}ms", (DateTime.Now - startTime).TotalMilliseconds); + + if (_logger.IsEnabled(LogLevel.Information)) + { + _logger.LogInformation("MemcachedInitPool-cost: {0}ms", (DateTime.Now - startTime).TotalMilliseconds); + } } } finally @@ -204,7 +208,11 @@ public async Task AcquireAsync() var startTime = DateTime.Now; await _internalPoolImpl.InitPoolAsync(); _isInitialized = true; - _logger.LogInformation("MemcachedInitPool-cost: {0}ms", (DateTime.Now - startTime).TotalMilliseconds); + + if (_logger.IsEnabled(LogLevel.Information)) + { + _logger.LogInformation("MemcachedInitPool-cost: {0}ms", (DateTime.Now - startTime).TotalMilliseconds); + } } } finally @@ -502,7 +510,12 @@ public IPooledSocketResult Acquire() // okay, create the new item var startTime = DateTime.Now; socket = CreateSocket(); - _logger.LogInformation("MemcachedAcquire-CreateSocket: {0}ms", (DateTime.Now - startTime).TotalMilliseconds); + + if (_logger.IsEnabled(LogLevel.Information)) + { + _logger.LogInformation("MemcachedAcquire-CreateSocket: {0}ms", (DateTime.Now - startTime).TotalMilliseconds); + } + result.Value = socket; result.Pass(); } @@ -627,7 +640,12 @@ public async Task AcquireAsync() // okay, create the new item var startTime = DateTime.Now; socket = await CreateSocketAsync(); - _logger.LogInformation("MemcachedAcquire-CreateSocket: {0}ms", (DateTime.Now - startTime).TotalMilliseconds); + + if (_logger.IsEnabled(LogLevel.Information)) + { + _logger.LogInformation("MemcachedAcquire-CreateSocket: {0}ms", (DateTime.Now - startTime).TotalMilliseconds); + } + result.Value = socket; result.Pass(); } @@ -753,7 +771,11 @@ private bool TryPopPooledSocket(out PooledSocket pooledSocket) { try { - _logger.LogInformation("Connection idle timeout {idleTimeout} reached.", _connectionIdleTimeout); + if (_logger.IsEnabled(LogLevel.Information)) + { + _logger.LogInformation("Connection idle timeout {idleTimeout} reached.", _connectionIdleTimeout); + } + socket.Destroy(); } catch (Exception ex) @@ -919,7 +941,10 @@ protected virtual IPooledSocketResult ExecuteOperation(IOperation op) protected virtual async Task ExecuteOperationAsync(IOperation op) { - _logger.LogDebug($"ExecuteOperationAsync({op})"); + if (_logger.IsEnabled(LogLevel.Debug)) + { + _logger.LogDebug($"ExecuteOperationAsync({op})"); + } var result = await AcquireAsync(); if (result.Success && result.HasValue) @@ -931,7 +956,10 @@ protected virtual async Task ExecuteOperationAsync(IOperati //if Get, call BinaryRequest.CreateBuffer() var b = op.GetBuffer(); - _logger.LogDebug("pooledSocket.WriteAsync..."); + if (_logger.IsEnabled(LogLevel.Debug)) + { + _logger.LogDebug("pooledSocket.WriteAsync..."); + } var writeSocketTask = pooledSocket.WriteAsync(b); if (await Task.WhenAny(writeSocketTask, Task.Delay(_config.ConnectionTimeout)) != writeSocketTask) @@ -942,7 +970,10 @@ protected virtual async Task ExecuteOperationAsync(IOperati await writeSocketTask; //if Get, call BinaryResponse - _logger.LogDebug($"{op}.ReadResponseAsync..."); + if (_logger.IsEnabled(LogLevel.Debug)) + { + _logger.LogDebug($"{op}.ReadResponseAsync..."); + } var readResponseTask = op.ReadResponseAsync(pooledSocket); if (await Task.WhenAny(readResponseTask, Task.Delay(_config.ConnectionTimeout)) != readResponseTask) @@ -958,7 +989,11 @@ protected virtual async Task ExecuteOperationAsync(IOperati } else { - _logger.LogInformation($"{op}.{nameof(op.ReadResponseAsync)} result: {readResult.Message}"); + if (_logger.IsEnabled(LogLevel.Information)) + { + _logger.LogInformation($"{op}.{nameof(op.ReadResponseAsync)} result: {readResult.Message}"); + } + readResult.Combine(result); } return result; diff --git a/src/Enyim.Caching/MemcachedClient.cs b/src/Enyim.Caching/MemcachedClient.cs index b7bff193..77cade7a 100755 --- a/src/Enyim.Caching/MemcachedClient.cs +++ b/src/Enyim.Caching/MemcachedClient.cs @@ -298,7 +298,11 @@ public async Task GetAsync(string key) { if (!CreateGetCommand(key, out var result, out var node, out var command)) { - _logger.LogInformation($"Failed to CreateGetCommand for '{key}' key"); + if (_logger.IsEnabled(LogLevel.Information)) + { + _logger.LogInformation($"Failed to CreateGetCommand for '{key}' key"); + } + return result; } @@ -321,7 +325,11 @@ public async Task> GetAsync(string key) { if (!CreateGetCommand(key, out var result, out var node, out var command)) { - _logger.LogInformation($"Failed to CreateGetCommand for '{key}' key"); + if (_logger.IsEnabled(LogLevel.Information)) + { + _logger.LogInformation($"Failed to CreateGetCommand for '{key}' key"); + } + return result; } @@ -351,11 +359,18 @@ public async Task GetValueOrCreateAsync(string key, int cacheSeconds, Func var result = await GetAsync(key); if (result.Success) { - _logger.LogDebug($"Cache is hint. Key is '{key}'."); + if (_logger.IsEnabled(LogLevel.Debug)) + { + _logger.LogDebug($"Cache is hint. Key is '{key}'."); + } + return result.Value; } - _logger.LogDebug($"Cache is missed. Key is '{key}'."); + if (_logger.IsEnabled(LogLevel.Debug)) + { + _logger.LogDebug($"Cache is missed. Key is '{key}'."); + } var value = await generator?.Invoke(); if (value != null) @@ -363,7 +378,11 @@ public async Task GetValueOrCreateAsync(string key, int cacheSeconds, Func try { await AddAsync(key, value, cacheSeconds); - _logger.LogDebug($"Added value into cache. Key is '{key}'. " + value); + + if (_logger.IsEnabled(LogLevel.Debug)) + { + _logger.LogDebug($"Added value into cache. Key is '{key}'. " + value); + } } catch (Exception ex) {