- 优化 Console.Write 平台兼容问题;#643

This commit is contained in:
2881099 2021-01-12 18:24:49 +08:00
parent 7379050bfd
commit fd770e06b1
5 changed files with 44 additions and 57 deletions

View File

@ -49,7 +49,7 @@ namespace FreeSql.Extensions.LazyLoading
if (!dll.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) && if (!dll.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) &&
!dll.EndsWith(".exe", StringComparison.OrdinalIgnoreCase)) continue; !dll.EndsWith(".exe", StringComparison.OrdinalIgnoreCase)) continue;
Console.WriteLine(dll); //Console.WriteLine(dll);
var dllName = string.Empty; var dllName = string.Empty;
var idx = dll.LastIndexOf('/'); var idx = dll.LastIndexOf('/');
if (idx != -1) dllName = dll.Substring(idx + 1); if (idx != -1) dllName = dll.Substring(idx + 1);

View File

@ -4316,7 +4316,7 @@
</summary> </summary>
<param name="policy">策略</param> <param name="policy">策略</param>
</member> </member>
<member name="M:FreeSql.Internal.ObjectPool.ObjectPool`1.getFree(System.Boolean)"> <member name="M:FreeSql.Internal.ObjectPool.ObjectPool`1.GetFree(System.Boolean)">
<summary> <summary>
获取可用资源,或创建资源 获取可用资源,或创建资源
</summary> </summary>

View File

@ -33,7 +33,6 @@ namespace FreeSql.Internal.ObjectPool
public void OnGet(Object<T> obj) public void OnGet(Object<T> obj)
{ {
//Console.WriteLine("Get: " + obj);
OnGetObject?.Invoke(obj); OnGetObject?.Invoke(obj);
} }
@ -41,7 +40,6 @@ namespace FreeSql.Internal.ObjectPool
#else #else
public Task OnGetAsync(Object<T> obj) public Task OnGetAsync(Object<T> obj)
{ {
//Console.WriteLine("GetAsync: " + obj);
OnGetObject?.Invoke(obj); OnGetObject?.Invoke(obj);
return Task.FromResult(true); return Task.FromResult(true);
} }
@ -54,7 +52,6 @@ namespace FreeSql.Internal.ObjectPool
public void OnReturn(Object<T> obj) public void OnReturn(Object<T> obj)
{ {
//Console.WriteLine("Return: " + obj);
} }
public bool OnCheckAvailable(Object<T> obj) public bool OnCheckAvailable(Object<T> obj)

View File

@ -9,6 +9,40 @@ using System.Threading.Tasks;
namespace FreeSql.Internal.ObjectPool namespace FreeSql.Internal.ObjectPool
{ {
internal class TestTrace
{
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();
}
catch
{
try
{
System.Diagnostics.Debug.WriteLine(text);
}
catch { }
}
}
}
/// <summary> /// <summary>
/// 对象池管理类 /// 对象池管理类
@ -74,16 +108,7 @@ namespace FreeSql.Internal.ObjectPool
{ {
if (UnavailableException != null) if (UnavailableException != null)
{ TestTrace.WriteLine($"【{Policy.Name}】恢复检查时间:{DateTime.Now.AddSeconds(interval)}", ConsoleColor.DarkYellow);
var bgcolor = Console.BackgroundColor;
var forecolor = Console.ForegroundColor;
Console.BackgroundColor = ConsoleColor.DarkYellow;
Console.ForegroundColor = ConsoleColor.White;
Console.Write($"【{Policy.Name}】恢复检查时间:{DateTime.Now.AddSeconds(interval)}");
Console.BackgroundColor = bgcolor;
Console.ForegroundColor = forecolor;
Console.WriteLine();
}
while (UnavailableException != null) while (UnavailableException != null)
{ {
@ -97,7 +122,7 @@ namespace FreeSql.Internal.ObjectPool
try try
{ {
var conn = getFree(false); var conn = GetFree(false);
if (conn == null) throw new Exception($"CheckAvailable 无法获得资源,{this.Statistics}"); if (conn == null) throw new Exception($"CheckAvailable 无法获得资源,{this.Statistics}");
try try
@ -116,14 +141,7 @@ namespace FreeSql.Internal.ObjectPool
} }
catch (Exception ex) catch (Exception ex)
{ {
var bgcolor = Console.BackgroundColor; TestTrace.WriteLine($"【{Policy.Name}】仍然不可用,下一次恢复检查时间:{DateTime.Now.AddSeconds(interval)},错误:({ex.Message})", ConsoleColor.DarkYellow);
var forecolor = Console.ForegroundColor;
Console.BackgroundColor = ConsoleColor.DarkYellow;
Console.ForegroundColor = ConsoleColor.White;
Console.Write($"【{Policy.Name}】仍然不可用,下一次恢复检查时间:{DateTime.Now.AddSeconds(interval)},错误:({ex.Message})");
Console.BackgroundColor = bgcolor;
Console.ForegroundColor = forecolor;
Console.WriteLine();
} }
} }
@ -160,14 +178,7 @@ namespace FreeSql.Internal.ObjectPool
Policy.OnAvailable(); Policy.OnAvailable();
var bgcolor = Console.BackgroundColor; TestTrace.WriteLine($"【{Policy.Name}】已恢复工作", ConsoleColor.DarkGreen);
var forecolor = Console.ForegroundColor;
Console.BackgroundColor = ConsoleColor.DarkGreen;
Console.ForegroundColor = ConsoleColor.White;
Console.Write($"【{Policy.Name}】已恢复工作");
Console.BackgroundColor = bgcolor;
Console.ForegroundColor = forecolor;
Console.WriteLine();
} }
} }
@ -177,7 +188,7 @@ namespace FreeSql.Internal.ObjectPool
try try
{ {
var conn = getFree(false); var conn = GetFree(false);
if (conn == null) throw new Exception($"LiveCheckAvailable 无法获得资源,{this.Statistics}"); if (conn == null) throw new Exception($"LiveCheckAvailable 无法获得资源,{this.Statistics}");
try try
@ -260,7 +271,7 @@ namespace FreeSql.Internal.ObjectPool
/// 获取可用资源,或创建资源 /// 获取可用资源,或创建资源
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
private Object<T> getFree(bool checkAvailable) private Object<T> GetFree(bool checkAvailable)
{ {
if (running == false) if (running == false)
@ -300,7 +311,7 @@ namespace FreeSql.Internal.ObjectPool
public Object<T> Get(TimeSpan? timeout = null) public Object<T> Get(TimeSpan? timeout = null)
{ {
var obj = getFree(true); var obj = GetFree(true);
if (obj == null) if (obj == null)
{ {
@ -357,7 +368,7 @@ namespace FreeSql.Internal.ObjectPool
async public Task<Object<T>> GetAsync() async public Task<Object<T>> GetAsync()
{ {
var obj = getFree(true); var obj = GetFree(true);
if (obj == null) if (obj == null)
{ {

View File

@ -1,21 +0,0 @@
case DataType.Firebird:
type = Type.GetType("FreeSql.Firebird.FirebirdProvider`1,FreeSql.Provider.Firebird")?.MakeGenericType(typeof(TMark));
if (type == null) throwNotFind("FreeSql.Provider.Firebird.dll", "FreeSql.Firebird.FirebirdProvider<>");
break;
static Lazy<IFreeSql> firebirdLazy = new Lazy<IFreeSql>(() => new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.Firebird, @"database=localhost:D:\fbdata\EXAMPLES.fdb;user=sysdba;password=123456;max pool size=5")
//.UseConnectionFactory(FreeSql.DataType.Firebird, () => new FirebirdSql.Data.FirebirdClient.FbConnection(@"database=localhost:D:\fbdata\EXAMPLES.fdb;user=sysdba;password=123456;max pool size=5"))
.UseAutoSyncStructure(true)
.UseLazyLoading(true)
.UseNameConvert(FreeSql.Internal.NameConvertType.ToUpper)
//.UseNoneCommandParameter(true)
.UseMonitorCommand(
cmd => Trace.WriteLine(cmd.CommandText), //监听SQL命令对象在执行前
(cmd, traceLog) => Console.WriteLine(traceLog))
.Build());
public static IFreeSql firebird => firebirdLazy.Value;