Skip to content

Commit a3c3b7f

Browse files
authored
Merge pull request #187 from cnblogs/tweak-logging
Check if the given LogLevel is enabled
2 parents bc06974 + 09e156d commit a3c3b7f

File tree

2 files changed

+68
-14
lines changed

2 files changed

+68
-14
lines changed

src/Enyim.Caching/Memcached/MemcachedNode.cs

+44-9
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,11 @@ public IPooledSocketResult Acquire()
160160
var startTime = DateTime.Now;
161161
_internalPoolImpl.InitPool();
162162
_isInitialized = true;
163-
_logger.LogInformation("MemcachedInitPool-cost: {0}ms", (DateTime.Now - startTime).TotalMilliseconds);
163+
164+
if (_logger.IsEnabled(LogLevel.Information))
165+
{
166+
_logger.LogInformation("MemcachedInitPool-cost: {0}ms", (DateTime.Now - startTime).TotalMilliseconds);
167+
}
164168
}
165169
}
166170
finally
@@ -204,7 +208,11 @@ public async Task<IPooledSocketResult> AcquireAsync()
204208
var startTime = DateTime.Now;
205209
await _internalPoolImpl.InitPoolAsync();
206210
_isInitialized = true;
207-
_logger.LogInformation("MemcachedInitPool-cost: {0}ms", (DateTime.Now - startTime).TotalMilliseconds);
211+
212+
if (_logger.IsEnabled(LogLevel.Information))
213+
{
214+
_logger.LogInformation("MemcachedInitPool-cost: {0}ms", (DateTime.Now - startTime).TotalMilliseconds);
215+
}
208216
}
209217
}
210218
finally
@@ -502,7 +510,12 @@ public IPooledSocketResult Acquire()
502510
// okay, create the new item
503511
var startTime = DateTime.Now;
504512
socket = CreateSocket();
505-
_logger.LogInformation("MemcachedAcquire-CreateSocket: {0}ms", (DateTime.Now - startTime).TotalMilliseconds);
513+
514+
if (_logger.IsEnabled(LogLevel.Information))
515+
{
516+
_logger.LogInformation("MemcachedAcquire-CreateSocket: {0}ms", (DateTime.Now - startTime).TotalMilliseconds);
517+
}
518+
506519
result.Value = socket;
507520
result.Pass();
508521
}
@@ -627,7 +640,12 @@ public async Task<IPooledSocketResult> AcquireAsync()
627640
// okay, create the new item
628641
var startTime = DateTime.Now;
629642
socket = await CreateSocketAsync();
630-
_logger.LogInformation("MemcachedAcquire-CreateSocket: {0}ms", (DateTime.Now - startTime).TotalMilliseconds);
643+
644+
if (_logger.IsEnabled(LogLevel.Information))
645+
{
646+
_logger.LogInformation("MemcachedAcquire-CreateSocket: {0}ms", (DateTime.Now - startTime).TotalMilliseconds);
647+
}
648+
631649
result.Value = socket;
632650
result.Pass();
633651
}
@@ -753,7 +771,11 @@ private bool TryPopPooledSocket(out PooledSocket pooledSocket)
753771
{
754772
try
755773
{
756-
_logger.LogInformation("Connection idle timeout {idleTimeout} reached.", _connectionIdleTimeout);
774+
if (_logger.IsEnabled(LogLevel.Information))
775+
{
776+
_logger.LogInformation("Connection idle timeout {idleTimeout} reached.", _connectionIdleTimeout);
777+
}
778+
757779
socket.Destroy();
758780
}
759781
catch (Exception ex)
@@ -919,7 +941,10 @@ protected virtual IPooledSocketResult ExecuteOperation(IOperation op)
919941

920942
protected virtual async Task<IPooledSocketResult> ExecuteOperationAsync(IOperation op)
921943
{
922-
_logger.LogDebug($"ExecuteOperationAsync({op})");
944+
if (_logger.IsEnabled(LogLevel.Debug))
945+
{
946+
_logger.LogDebug($"ExecuteOperationAsync({op})");
947+
}
923948

924949
var result = await AcquireAsync();
925950
if (result.Success && result.HasValue)
@@ -931,7 +956,10 @@ protected virtual async Task<IPooledSocketResult> ExecuteOperationAsync(IOperati
931956
//if Get, call BinaryRequest.CreateBuffer()
932957
var b = op.GetBuffer();
933958

934-
_logger.LogDebug("pooledSocket.WriteAsync...");
959+
if (_logger.IsEnabled(LogLevel.Debug))
960+
{
961+
_logger.LogDebug("pooledSocket.WriteAsync...");
962+
}
935963

936964
var writeSocketTask = pooledSocket.WriteAsync(b);
937965
if (await Task.WhenAny(writeSocketTask, Task.Delay(_config.ConnectionTimeout)) != writeSocketTask)
@@ -942,7 +970,10 @@ protected virtual async Task<IPooledSocketResult> ExecuteOperationAsync(IOperati
942970
await writeSocketTask;
943971

944972
//if Get, call BinaryResponse
945-
_logger.LogDebug($"{op}.ReadResponseAsync...");
973+
if (_logger.IsEnabled(LogLevel.Debug))
974+
{
975+
_logger.LogDebug($"{op}.ReadResponseAsync...");
976+
}
946977

947978
var readResponseTask = op.ReadResponseAsync(pooledSocket);
948979
if (await Task.WhenAny(readResponseTask, Task.Delay(_config.ConnectionTimeout)) != readResponseTask)
@@ -958,7 +989,11 @@ protected virtual async Task<IPooledSocketResult> ExecuteOperationAsync(IOperati
958989
}
959990
else
960991
{
961-
_logger.LogInformation($"{op}.{nameof(op.ReadResponseAsync)} result: {readResult.Message}");
992+
if (_logger.IsEnabled(LogLevel.Information))
993+
{
994+
_logger.LogInformation($"{op}.{nameof(op.ReadResponseAsync)} result: {readResult.Message}");
995+
}
996+
962997
readResult.Combine(result);
963998
}
964999
return result;

src/Enyim.Caching/MemcachedClient.cs

+24-5
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,11 @@ public async Task<IGetOperationResult> GetAsync(string key)
298298
{
299299
if (!CreateGetCommand(key, out var result, out var node, out var command))
300300
{
301-
_logger.LogInformation($"Failed to CreateGetCommand for '{key}' key");
301+
if (_logger.IsEnabled(LogLevel.Information))
302+
{
303+
_logger.LogInformation($"Failed to CreateGetCommand for '{key}' key");
304+
}
305+
302306
return result;
303307
}
304308

@@ -321,7 +325,11 @@ public async Task<IGetOperationResult<T>> GetAsync<T>(string key)
321325
{
322326
if (!CreateGetCommand<T>(key, out var result, out var node, out var command))
323327
{
324-
_logger.LogInformation($"Failed to CreateGetCommand for '{key}' key");
328+
if (_logger.IsEnabled(LogLevel.Information))
329+
{
330+
_logger.LogInformation($"Failed to CreateGetCommand for '{key}' key");
331+
}
332+
325333
return result;
326334
}
327335

@@ -351,19 +359,30 @@ public async Task<T> GetValueOrCreateAsync<T>(string key, int cacheSeconds, Func
351359
var result = await GetAsync<T>(key);
352360
if (result.Success)
353361
{
354-
_logger.LogDebug($"Cache is hint. Key is '{key}'.");
362+
if (_logger.IsEnabled(LogLevel.Debug))
363+
{
364+
_logger.LogDebug($"Cache is hint. Key is '{key}'.");
365+
}
366+
355367
return result.Value;
356368
}
357369

358-
_logger.LogDebug($"Cache is missed. Key is '{key}'.");
370+
if (_logger.IsEnabled(LogLevel.Debug))
371+
{
372+
_logger.LogDebug($"Cache is missed. Key is '{key}'.");
373+
}
359374

360375
var value = await generator?.Invoke();
361376
if (value != null)
362377
{
363378
try
364379
{
365380
await AddAsync(key, value, cacheSeconds);
366-
_logger.LogDebug($"Added value into cache. Key is '{key}'. " + value);
381+
382+
if (_logger.IsEnabled(LogLevel.Debug))
383+
{
384+
_logger.LogDebug($"Added value into cache. Key is '{key}'. " + value);
385+
}
367386
}
368387
catch (Exception ex)
369388
{

0 commit comments

Comments
 (0)