From 3fdb1fdf8e055eb4f0e0944d80f4406dc53165c7 Mon Sep 17 00:00:00 2001 From: xieyidong Date: Wed, 28 Sep 2022 15:42:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86Console=E8=BE=93=E5=87=BA=E6=94=B9?= =?UTF-8?q?=E4=B8=BATracer=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/CSRedisCore/CSRedisClient.cs | 176 ++++++++++-------- .../Internal/ObjectPool/ObjectPool.cs | 36 ++-- src/CSRedisCore/RedisClientPool.cs | 15 +- src/CSRedisCore/RedisSentinelManager.cs | 4 +- 4 files changed, 126 insertions(+), 105 deletions(-) diff --git a/src/CSRedisCore/CSRedisClient.cs b/src/CSRedisCore/CSRedisClient.cs index 7a8aa3d..02d897d 100644 --- a/src/CSRedisCore/CSRedisClient.cs +++ b/src/CSRedisCore/CSRedisClient.cs @@ -439,15 +439,15 @@ bool BackgroundGetSentinelMasterValue() pool._policy.SetHost(SentinelMasterValue); if (pool.CheckAvailable()) { - - var bgcolor = Console.BackgroundColor; - var forecolor = Console.ForegroundColor; - Console.BackgroundColor = ConsoleColor.DarkGreen; - Console.ForegroundColor = ConsoleColor.White; - Console.Write($"Redis Sentinel Pool 已切换至 {SentinelMasterValue}"); - Console.BackgroundColor = bgcolor; - Console.ForegroundColor = forecolor; - Console.WriteLine(); + Trace.TraceInformation($"Redis Sentinel Pool 已切换至 {SentinelMasterValue}"); + //var bgcolor = Console.BackgroundColor; + //var forecolor = Console.ForegroundColor; + //Console.BackgroundColor = ConsoleColor.DarkGreen; + //Console.ForegroundColor = ConsoleColor.White; + //Console.Write($"Redis Sentinel Pool 已切换至 {SentinelMasterValue}"); + //Console.BackgroundColor = bgcolor; + //Console.ForegroundColor = forecolor; + //Console.WriteLine(); BackgroundGetSentinelMasterValueIng = false; return; @@ -1520,14 +1520,15 @@ private void Subscribe(object state) } catch (Exception ex) { - var bgcolor = Console.BackgroundColor; - var forecolor = Console.ForegroundColor; - Console.BackgroundColor = ConsoleColor.DarkRed; - Console.ForegroundColor = ConsoleColor.White; - Console.Write($"订阅方法执行出错【{pool.Key}】(channels:{string.Join(",", Channels)})/(chans:{string.Join(",", subscr.chans)}):{ex.Message}\r\n{ex.StackTrace}"); - Console.BackgroundColor = bgcolor; - Console.ForegroundColor = forecolor; - Console.WriteLine(); + Trace.WriteLine($"订阅方法执行出错【{pool.Key}】(channels:{string.Join(",", Channels)})/(chans:{string.Join(",", subscr.chans)}):{ex.Message}\r\n{ex.StackTrace}"); + //var bgcolor = Console.BackgroundColor; + //var forecolor = Console.ForegroundColor; + //Console.BackgroundColor = ConsoleColor.DarkRed; + //Console.ForegroundColor = ConsoleColor.White; + //Console.Write($"订阅方法执行出错【{pool.Key}】(channels:{string.Join(",", Channels)})/(chans:{string.Join(",", subscr.chans)}):{ex.Message}\r\n{ex.StackTrace}"); + //Console.BackgroundColor = bgcolor; + //Console.ForegroundColor = forecolor; + //Console.WriteLine(); } }; subscr.conn.Value.SubscriptionReceived += SubscriptionReceived; @@ -1570,14 +1571,17 @@ private void Subscribe(object state) { subscr.conn.Value.Ping(); - var bgcolor = Console.BackgroundColor; - var forecolor = Console.ForegroundColor; - Console.BackgroundColor = ConsoleColor.DarkGreen; - Console.ForegroundColor = ConsoleColor.White; - Console.Write($"正在订阅【{pool.Key}】(channels:{string.Join(",", Channels)})/(chans:{string.Join(",", subscr.chans)})"); - Console.BackgroundColor = bgcolor; - Console.ForegroundColor = forecolor; - Console.WriteLine(); + + Trace.WriteLine($"正在订阅【{pool.Key}】(channels:{string.Join(",", Channels)})/(chans:{string.Join(",", subscr.chans)})"); + + //var bgcolor = Console.BackgroundColor; + //var forecolor = Console.ForegroundColor; + //Console.BackgroundColor = ConsoleColor.DarkGreen; + //Console.ForegroundColor = ConsoleColor.White; + //Console.Write($"正在订阅【{pool.Key}】(channels:{string.Join(",", Channels)})/(chans:{string.Join(",", subscr.chans)})"); + //Console.BackgroundColor = bgcolor; + //Console.ForegroundColor = forecolor; + //Console.WriteLine(); isSubscribeing = true; isKeepliveReSubscribe = false; @@ -1600,14 +1604,17 @@ private void Subscribe(object state) { if (IsUnsubscribed) break; - var bgcolor = Console.BackgroundColor; - var forecolor = Console.ForegroundColor; - Console.BackgroundColor = ConsoleColor.DarkYellow; - Console.ForegroundColor = ConsoleColor.White; - Console.Write($"订阅出错【{pool.Key}】(channels:{string.Join(",", Channels)})/(chans:{string.Join(",", subscr.chans)}):{ex.Message},3秒后重连。。。"); - Console.BackgroundColor = bgcolor; - Console.ForegroundColor = forecolor; - Console.WriteLine(); + + Trace.TraceWarning($"订阅出错【{pool.Key}】(channels:{string.Join(",", Channels)})/(chans:{string.Join(",", subscr.chans)}):{ex.Message},3秒后重连。。。"); + + //var bgcolor = Console.BackgroundColor; + //var forecolor = Console.ForegroundColor; + //Console.BackgroundColor = ConsoleColor.DarkYellow; + //Console.ForegroundColor = ConsoleColor.White; + //Console.Write($"订阅出错【{pool.Key}】(channels:{string.Join(",", Channels)})/(chans:{string.Join(",", subscr.chans)}):{ex.Message},3秒后重连。。。"); + //Console.BackgroundColor = bgcolor; + //Console.ForegroundColor = forecolor; + //Console.WriteLine(); Thread.CurrentThread.Join(1000 * 3); subscr.conn.ResetValue(); @@ -1752,14 +1759,16 @@ private void PSubscribe(object state) } catch (Exception ex) { - var bgcolor = Console.BackgroundColor; - var forecolor = Console.ForegroundColor; - Console.BackgroundColor = ConsoleColor.DarkRed; - Console.ForegroundColor = ConsoleColor.White; - Console.Write($"模糊订阅出错【{pool.Key}】(channels:{string.Join(",", Channels)}):{ex.Message}\r\n{ex.StackTrace}"); - Console.BackgroundColor = bgcolor; - Console.ForegroundColor = forecolor; - Console.WriteLine(); + + Trace.TraceWarning($"模糊订阅出错【{pool.Key}】(channels:{string.Join(",", Channels)}):{ex.Message}\r\n{ex.StackTrace}"); + //var bgcolor = Console.BackgroundColor; + //var forecolor = Console.ForegroundColor; + //Console.BackgroundColor = ConsoleColor.DarkRed; + //Console.ForegroundColor = ConsoleColor.White; + //Console.Write($"模糊订阅出错【{pool.Key}】(channels:{string.Join(",", Channels)}):{ex.Message}\r\n{ex.StackTrace}"); + //Console.BackgroundColor = bgcolor; + //Console.ForegroundColor = forecolor; + //Console.WriteLine(); } }; @@ -1771,14 +1780,15 @@ private void PSubscribe(object state) { conn.Value.Ping(); - var bgcolor = Console.BackgroundColor; - var forecolor = Console.ForegroundColor; - Console.BackgroundColor = ConsoleColor.DarkGreen; - Console.ForegroundColor = ConsoleColor.White; - Console.Write($"正在模糊订阅【{pool.Key}】(channels:{string.Join(",", Channels)})"); - Console.BackgroundColor = bgcolor; - Console.ForegroundColor = forecolor; - Console.WriteLine(); + Trace.TraceInformation($"正在模糊订阅【{pool.Key}】(channels:{string.Join(",", Channels)})"); + //var bgcolor = Console.BackgroundColor; + //var forecolor = Console.ForegroundColor; + //Console.BackgroundColor = ConsoleColor.DarkGreen; + //Console.ForegroundColor = ConsoleColor.White; + //Console.Write($"正在模糊订阅【{pool.Key}】(channels:{string.Join(",", Channels)})"); + //Console.BackgroundColor = bgcolor; + //Console.ForegroundColor = forecolor; + //Console.WriteLine(); //conn.Value.Socket?.SetSocketOption(System.Net.Sockets.SocketOptionLevel.Socket, System.Net.Sockets.SocketOptionName.KeepAlive, 60000); conn.Value.ReceiveTimeout = 0; @@ -1795,14 +1805,15 @@ private void PSubscribe(object state) { if (IsPUnsubscribed) break; - var bgcolor = Console.BackgroundColor; - var forecolor = Console.ForegroundColor; - Console.BackgroundColor = ConsoleColor.DarkYellow; - Console.ForegroundColor = ConsoleColor.White; - Console.Write($"模糊订阅出错【{pool.Key}】(channels:{string.Join(",", Channels)}):{ex.Message},3秒后重连。。。"); - Console.BackgroundColor = bgcolor; - Console.ForegroundColor = forecolor; - Console.WriteLine(); + Trace.TraceWarning($"模糊订阅出错【{pool.Key}】(channels:{string.Join(",", Channels)}):{ex.Message},3秒后重连。。。"); + //var bgcolor = Console.BackgroundColor; + //var forecolor = Console.ForegroundColor; + //Console.BackgroundColor = ConsoleColor.DarkYellow; + //Console.ForegroundColor = ConsoleColor.White; + //Console.Write($"模糊订阅出错【{pool.Key}】(channels:{string.Join(",", Channels)}):{ex.Message},3秒后重连。。。"); + //Console.BackgroundColor = bgcolor; + //Console.ForegroundColor = forecolor; + //Console.WriteLine(); Thread.CurrentThread.Join(1000 * 3); conn.ResetValue(); @@ -1880,14 +1891,15 @@ public SubscribeListBroadcastObject SubscribeListBroadcast(string listKey, strin } catch (Exception ex) { - var bgcolor = Console.BackgroundColor; - var forecolor = Console.ForegroundColor; - Console.BackgroundColor = ConsoleColor.DarkRed; - Console.ForegroundColor = ConsoleColor.White; - Console.Write($"列表订阅出错(listKey:{listKey}):{ex.Message}"); - Console.BackgroundColor = bgcolor; - Console.ForegroundColor = forecolor; - Console.WriteLine(); + Trace.TraceWarning($"列表订阅出错(listKey:{listKey}):{ex.Message}"); + //var bgcolor = Console.BackgroundColor; + //var forecolor = Console.ForegroundColor; + //Console.BackgroundColor = ConsoleColor.DarkRed; + //Console.ForegroundColor = ConsoleColor.White; + //Console.Write($"列表订阅出错(listKey:{listKey}):{ex.Message}"); + //Console.BackgroundColor = bgcolor; + //Console.ForegroundColor = forecolor; + //Console.WriteLine(); } }, true)); @@ -1939,14 +1951,15 @@ private SubscribeListObject SubscribeList(string[] listKeys, Action 1; var subobj = new SubscribeListObject(); - var bgcolor = Console.BackgroundColor; - var forecolor = Console.ForegroundColor; - Console.BackgroundColor = ConsoleColor.DarkGreen; - Console.ForegroundColor = ConsoleColor.White; - Console.Write($"正在订阅列表(listKey:{listKeysStr})"); - Console.BackgroundColor = bgcolor; - Console.ForegroundColor = forecolor; - Console.WriteLine(); + Trace.TraceInformation($"正在订阅列表(listKey:{listKeysStr})"); + //var bgcolor = Console.BackgroundColor; + //var forecolor = Console.ForegroundColor; + //Console.BackgroundColor = ConsoleColor.DarkGreen; + //Console.ForegroundColor = ConsoleColor.White; + //Console.Write($"正在订阅列表(listKey:{listKeysStr})"); + //Console.BackgroundColor = bgcolor; + //Console.ForegroundColor = forecolor; + //Console.WriteLine(); new Thread(() => { @@ -1973,14 +1986,15 @@ private SubscribeListObject SubscribeList(string[] listKeys, Action obj, Exception exception, bool isRecreate if (!obj.Value.IsConnected) obj.Value.Connect(_policy._connectTimeout); obj.Value.Ping(); - var fcolor = Console.ForegroundColor; - Console.WriteLine($""); - Console.ForegroundColor = ConsoleColor.DarkYellow; - Console.WriteLine($"csreids 错误【{Policy.Name}】:{exception.Message} {exception.StackTrace}"); - Console.ForegroundColor = fcolor; - Console.WriteLine($""); + + Trace.TraceWarning($"\ncsreids 错误【{Policy.Name}】:{exception.Message} {exception.StackTrace}\n"); + + //var fcolor = Console.ForegroundColor; + //Console.WriteLine($""); + //Console.ForegroundColor = ConsoleColor.DarkYellow; + //Console.WriteLine($"csreids 错误【{Policy.Name}】:{exception.Message} {exception.StackTrace}"); + //Console.ForegroundColor = fcolor; + //Console.WriteLine($""); } catch { diff --git a/src/CSRedisCore/RedisSentinelManager.cs b/src/CSRedisCore/RedisSentinelManager.cs index 4a86331..d970de9 100644 --- a/src/CSRedisCore/RedisSentinelManager.cs +++ b/src/CSRedisCore/RedisSentinelManager.cs @@ -195,7 +195,7 @@ string SetMaster(string name, int timeout) catch (Exception ex) { Trace.WriteLine(ex.Message); - Console.WriteLine(ex.Message); + //Console.WriteLine(ex.Message); continue; } @@ -252,7 +252,7 @@ string SetSlave(string name, int timeout) catch (Exception ex) { Trace.WriteLine(ex.Message); - Console.WriteLine(ex.Message); + //Console.WriteLine(ex.Message); continue; }