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
{
@@ -393,7 +394,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 +418,7 @@
                    }
                    else
                    {
                        throw new ProcessException($"{InitialConfig.Name}设备操作超时", null);
                        throw new ProcessException($"{InitialConfig.Name}设备操作超时");
                    }
                }
@@ -489,7 +490,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 +516,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
    }
}