Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

将Console输出改为Trace输出 #462

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 95 additions & 81 deletions src/CSRedisCore/CSRedisClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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();

}
};
Expand All @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -1939,14 +1951,15 @@ private SubscribeListObject SubscribeList(string[] listKeys, Action<string, stri
var isMultiKey = listKeys.Length > 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(() =>
{
Expand All @@ -1973,14 +1986,15 @@ private SubscribeListObject SubscribeList(string[] listKeys, Action<string, stri
}
catch (Exception ex)
{
bgcolor = Console.BackgroundColor;
forecolor = Console.ForegroundColor;
Console.BackgroundColor = ConsoleColor.DarkRed;
Console.ForegroundColor = ConsoleColor.White;
Console.Write($"列表订阅出错(listKey:{listKeysStr}):{ex.Message}");
Console.BackgroundColor = bgcolor;
Console.ForegroundColor = forecolor;
Console.WriteLine();
Trace.TraceWarning($"列表订阅出错(listKey:{listKeysStr}):{ex.Message}");
//bgcolor = Console.BackgroundColor;
//forecolor = Console.ForegroundColor;
//Console.BackgroundColor = ConsoleColor.DarkRed;
//Console.ForegroundColor = ConsoleColor.White;
//Console.Write($"列表订阅出错(listKey:{listKeysStr}):{ex.Message}");
//Console.BackgroundColor = bgcolor;
//Console.ForegroundColor = forecolor;
//Console.WriteLine();

Thread.CurrentThread.Join(3000);
}
Expand Down
36 changes: 20 additions & 16 deletions src/CSRedisCore/Internal/ObjectPool/ObjectPool.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -13,23 +14,26 @@ internal static void WriteLine(string text, ConsoleColor backgroundColor)
{
try //#643
{
var bgcolor = Console.BackgroundColor;
var forecolor = Console.ForegroundColor;
Console.BackgroundColor = backgroundColor;

switch (backgroundColor)
{
case ConsoleColor.Yellow:
Console.ForegroundColor = ConsoleColor.White;
break;
case ConsoleColor.DarkGreen:
Console.ForegroundColor = ConsoleColor.White;
break;
}
Console.Write(text);
Console.BackgroundColor = bgcolor;
Console.ForegroundColor = forecolor;
Console.WriteLine();
Trace.TraceInformation(text);

//var bgcolor = Console.BackgroundColor;
//var forecolor = Console.ForegroundColor;
//Console.BackgroundColor = backgroundColor;

//switch (backgroundColor)
//{
// case ConsoleColor.Yellow:
// Console.ForegroundColor = ConsoleColor.White;
// break;
// case ConsoleColor.DarkGreen:
// Console.ForegroundColor = ConsoleColor.White;
// break;
//}
//Console.Write(text);
//Console.BackgroundColor = bgcolor;
//Console.ForegroundColor = forecolor;
//Console.WriteLine();
}
catch
{
Expand Down
15 changes: 9 additions & 6 deletions src/CSRedisCore/RedisClientPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,15 @@ public void Return(Object<RedisClient> 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
{
Expand Down
4 changes: 2 additions & 2 deletions src/CSRedisCore/RedisSentinelManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down