From 1c4426810c71eead57084be8a18ade8d314dd8c4 Mon Sep 17 00:00:00 2001 From: patrick <patrick.xu@broconcentric.com> Date: 星期二, 10 十二月 2019 14:24:31 +0800 Subject: [PATCH] 1. 重构项目 --- src/Bro.Device.Common/Base/DeviceBase.cs | 45 +++++++++++++++++++++++++++++++++++++++------ 1 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/Bro.Device.Common/Base/DeviceBase.cs b/src/Bro.Device.Common/Base/DeviceBase.cs index 11aa9cf..0d3c8dd 100644 --- a/src/Bro.Device.Common/Base/DeviceBase.cs +++ b/src/Bro.Device.Common/Base/DeviceBase.cs @@ -14,6 +14,7 @@ //using BroCComn.PubSub; using static Bro.Common.Helper.EnumHelper; using System.ComponentModel; +using System.IO; namespace Bro.Common.Base { @@ -28,7 +29,6 @@ [JsonIgnore] public Action<EnumHelper.DeviceState> OnDeviceStateChanged { get; set; } #endregion - int RetryTime = 3; /// <summary> @@ -168,8 +168,6 @@ [JsonIgnore] public Dictionary<DeviceInputMethodAttribute, MethodInfo> InputMethods { get; set; } = new Dictionary<DeviceInputMethodAttribute, MethodInfo>(); - - //public event DeviceStateChangedDelegate OnDeviceStateChanged; @@ -393,7 +391,7 @@ { string currentStateStr = CurrentStateToBe.GetEnumDescription(); string stateToBeStr = stateToBe.GetEnumDescription(); - throw new ProcessException($"{InitialConfig.Name}璁惧鐨勫綋鍓嶇姸鎬佷负{currentStateStr}锛屾棤娉曞垏鎹㈣嚦{stateToBeStr}", null); + throw new ProcessException($"{InitialConfig.Name}璁惧鐨勫綋鍓嶇姸鎬佷负{currentStateStr}锛屾棤娉曞垏鎹㈣嚦{stateToBeStr}"); } CurrentState = EnumHelper.DeviceState.TBD; @@ -417,7 +415,7 @@ } else { - throw new ProcessException($"{InitialConfig.Name}璁惧鎿嶄綔瓒呮椂", null); + throw new ProcessException($"{InitialConfig.Name}璁惧鎿嶄綔瓒呮椂"); } } @@ -489,7 +487,7 @@ //this.CurrentState = EnumHelper.DeviceState.DSExcept; //this.CurrentStateToBe = EnumHelper.DeviceState.DSExcept; - throw new ProcessException($"璁惧{InitialConfig.Name}鐨剓CurrentStateToBe.GetEnumDescription()}鎿嶄綔閲嶅3娆″け璐�", ex); + throw new ProcessException($"璁惧{InitialConfig.Name}鐨剓CurrentStateToBe.GetEnumDescription()}鎿嶄綔閲嶅3娆″け璐�", ExceptionLevel.Warning, ex); } } } @@ -515,5 +513,40 @@ CurrentUserId = obj; } #endregion + + #region 鏃ュ織澶勭悊 + object logObject = new object(); + public virtual async void LogAsync(DateTime dt, string prefix, string msg) + { + await Task.Run(() => + { + if (InitialConfig.IsEnableLog) + { + OnLog?.Invoke(dt, this, prefix + "\t" + msg); + + if (string.IsNullOrWhiteSpace(InitialConfig.LogPath) || !Path.IsPathRooted(InitialConfig.LogPath)) + { + return; + } + + if (!Directory.Exists(InitialConfig.LogPath)) + { + Directory.CreateDirectory(InitialConfig.LogPath); + } + + string filePath = Path.Combine(InitialConfig.LogPath, $"Log_{Name}_{DateTime.Now.ToString("yyyyMMdd")}.txt"); + lock (logObject) + { + using (StreamWriter writer = new StreamWriter(filePath, true, Encoding.UTF8)) + { + writer.WriteLine($"{DateTime.Now.ToString("HH:mm:ss.fff")}\t{prefix}\t{msg}"); + writer.Flush(); + writer.Close(); + } + } + } + }); + } + #endregion } } -- Gitblit v1.8.0