From 9c265b017d2ab60906909f0127de5f8c3851b026 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=BA=E8=83=BD=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Tue, 9 Jul 2024 22:51:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8D=95=E8=8E=B7=E6=9B=B4=E6=96=B0=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E6=97=B6=E7=9A=84=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/BaseDeviceController.cs | 6 +++-- NewLife.Remoting/Clients/ClientBase.cs | 24 ++++++++++++------- NewLife.Remoting/Clients/WsChannel.cs | 2 +- NewLife.Remoting/Clients/WsChannelCore.cs | 2 +- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/NewLife.Remoting.Extensions/Controllers/BaseDeviceController.cs b/NewLife.Remoting.Extensions/Controllers/BaseDeviceController.cs index 6a20dbf..57d30e4 100644 --- a/NewLife.Remoting.Extensions/Controllers/BaseDeviceController.cs +++ b/NewLife.Remoting.Extensions/Controllers/BaseDeviceController.cs @@ -26,6 +26,7 @@ public class BaseDeviceController : BaseController private readonly IDeviceService _deviceService; private readonly TokenService _tokenService; private readonly ITracer? _tracer; + private readonly ILog? _log; #region 构造 /// 实例化设备控制器 @@ -35,6 +36,7 @@ public BaseDeviceController(IServiceProvider serviceProvider) : base(serviceProv _deviceService = serviceProvider.GetRequiredService(); _tokenService = serviceProvider.GetRequiredService(); _tracer = serviceProvider.GetService(); + _log = serviceProvider.GetService(); } /// 验证身份 @@ -274,8 +276,8 @@ private async Task ConsumeMessage(WebSocket socket, String code, IProducerConsum catch (OperationCanceledException) { } catch (Exception ex) { - XTrace.WriteLine("WebSocket异常 client={0} ip={1}", code, ip); - XTrace.WriteException(ex); + _log?.Error("WebSocket异常 client={0} ip={1}", code, ip); + _log?.Error(ex.ToString()); WriteLog("WebSocket断开", false, $"State={socket.State} CloseStatus={socket.CloseStatus} {ex}"); } finally diff --git a/NewLife.Remoting/Clients/ClientBase.cs b/NewLife.Remoting/Clients/ClientBase.cs index 9185a40..066b05a 100644 --- a/NewLife.Remoting/Clients/ClientBase.cs +++ b/NewLife.Remoting/Clients/ClientBase.cs @@ -523,7 +523,7 @@ protected virtual void FillLoginRequest(ILoginRequest request) catch (Exception ex) { span?.SetError(ex, null); - XTrace.WriteException(ex); + Log?.Error(ex.ToString()); return null; } @@ -710,17 +710,19 @@ private async Task CheckUpgrade(Object? data) WriteLog("检查更新"); // 清理旧版备份文件 - var ug = new Upgrade { Log = XTrace.Log }; + var ug = new Upgrade { Log = Log }; ug.DeleteBackup("."); // 调用接口查询思否存在更新信息 var info = await UpgradeAsync(channel, cancellationToken); - if (info != null && info.Version != _lastVersion) - { - // _lastVersion避免频繁更新同一个版本 - WriteLog("发现更新:{0}", info.ToJson(true)); - this.WriteInfoEvent("Upgrade", $"准备从[{_lastVersion}]更新到[{info.Version}],开始下载 {info.Source}"); + if (info == null || info.Version == _lastVersion) return info; + + // _lastVersion避免频繁更新同一个版本 + WriteLog("发现更新:{0}", info.ToJson(true)); + this.WriteInfoEvent("Upgrade", $"准备从[{_lastVersion}]更新到[{info.Version}],开始下载 {info.Source}"); + try + { // 下载文件包 ug.Url = BuildUrl(info.Source!); await ug.Download(cancellationToken); @@ -760,6 +762,12 @@ private async Task CheckUpgrade(Object? data) } } } + catch (Exception ex) + { + span?.SetError(ex, null); + //Log?.Error(ex.ToString()); + throw; + } return info; } @@ -892,7 +900,7 @@ protected virtual async Task OnPing(Object state) // 有效期判断前把UTC转为本地时间 var now = GetNow(); var expire = model.Expire.ToLocalTime(); - XTrace.WriteLine("[{0}] Got Command: {1}", source, model.ToJson()); + WriteLog("[{0}] Got Command: {1}", source, model.ToJson()); if (model.Expire.Year < 2000 || model.Expire > now) { // 延迟执行 diff --git a/NewLife.Remoting/Clients/WsChannel.cs b/NewLife.Remoting/Clients/WsChannel.cs index e30c020..b489a94 100644 --- a/NewLife.Remoting/Clients/WsChannel.cs +++ b/NewLife.Remoting/Clients/WsChannel.cs @@ -100,7 +100,7 @@ private async Task DoPull(WebSocketClient socket, CancellationTokenSource source catch (OperationCanceledException) { } catch (Exception ex) { - XTrace.WriteLine("WebSocket异常 {0}", ex.Message); + _client.Log?.Error("WebSocket异常 {0}", ex.Message); } finally { diff --git a/NewLife.Remoting/Clients/WsChannelCore.cs b/NewLife.Remoting/Clients/WsChannelCore.cs index a25299a..167e672 100644 --- a/NewLife.Remoting/Clients/WsChannelCore.cs +++ b/NewLife.Remoting/Clients/WsChannelCore.cs @@ -99,7 +99,7 @@ private async Task DoPull(WebSocket socket, CancellationTokenSource source) catch (OperationCanceledException) { } catch (WebSocketException ex) { - XTrace.WriteLine("WebSocket异常 {0}", ex.Message); + _client.Log?.Error("WebSocket异常 {0}", ex.Message); } finally {