diff --git a/Netch/Controllers/Guard.cs b/Netch/Controllers/Guard.cs index bd999de8fe..4320950074 100644 --- a/Netch/Controllers/Guard.cs +++ b/Netch/Controllers/Guard.cs @@ -20,6 +20,7 @@ public abstract class Guard private FileStream? _logFileStream; private StreamWriter? _logStreamWriter; + private bool _redirectToFile = true; /// /// 日志文件(重定向输出文件) @@ -50,6 +51,12 @@ public abstract class Guard /// protected bool RedirectStd { get; set; } = true; + protected bool RedirectToFile + { + get => RedirectStd && _redirectToFile; + set => _redirectToFile = value; + } + /// /// 进程实例 /// @@ -123,18 +130,12 @@ protected void StartInstanceAuto(string argument, ProcessPriorityClass priority State = State.Starting; // 初始化程序 InitInstance(argument); - Instance!.EnableRaisingEvents = true; - if (RedirectStd) - { - // 清理日志 - _logFileStream = File.Open(LogPath, FileMode.Create, FileAccess.ReadWrite, FileShare.Read); - _logStreamWriter = new StreamWriter(_logFileStream); - } - Instance.Exited += OnExited; + if (RedirectToFile) + OpenLogFile(); // 启动程序 - Instance.Start(); + Instance!.Start(); if (priority != ProcessPriorityClass.Normal) Instance.PriorityClass = priority; @@ -142,21 +143,18 @@ protected void StartInstanceAuto(string argument, ProcessPriorityClass priority { Task.Run(() => ReadOutput(Instance.StandardOutput)); Task.Run(() => ReadOutput(Instance.StandardError)); + + if (!StartedKeywords.Any()) + { + State = State.Started; + return; + } } else { return; } - // 启动日志重定向 - _flushFileStreamTimer.Elapsed += FlushFileStreamTimerEvent; - _flushFileStreamTimer.Enabled = true; - if (!StartedKeywords.Any()) - { - State = State.Started; - return; - } - // 等待启动 for (var i = 0; i < 1000; i++) { @@ -178,33 +176,68 @@ protected void StartInstanceAuto(string argument, ProcessPriorityClass priority throw new MessageException($"{Name} 控制器启动超时"); } - protected virtual void OnKeywordStarted() + #region FileStream + + private void OpenLogFile() { + _logFileStream = File.Open(LogPath, FileMode.Create, FileAccess.ReadWrite, FileShare.Read); + _logStreamWriter = new StreamWriter(_logFileStream); + + _flushFileStreamTimer.Elapsed += FlushFileStreamTimerEvent; + _flushFileStreamTimer.Enabled = true; } - protected virtual void OnKeywordTimeout() + private void WriteLog(string line) + { + if (!RedirectToFile) + return; + + _logStreamWriter!.WriteLine(line); + } + + private void CloseLogFile() + { + if (!RedirectToFile) + return; + + _flushFileStreamTimer.Enabled = false; + _logStreamWriter!.Close(); + _logFileStream!.Close(); + _logStreamWriter = _logStreamWriter = null; + } + + #endregion + + #region virtual + + protected virtual void OnReadNewLine(string line) + { + } + + protected virtual void OnKeywordStarted() { } protected virtual void OnKeywordStopped() { Utils.Utils.Open(LogPath); - throw new MessageException($"{Name} 控制器启动失败"); } - private void OnExited(object sender, EventArgs e) + protected virtual void OnKeywordTimeout() { - State = State.Stopped; } - protected virtual void ReadOutput(TextReader reader) + #endregion + + protected void ReadOutput(TextReader reader) { string? line; while ((line = reader.ReadLine()) != null) { - _logStreamWriter!.WriteLine(line); + OnReadNewLine(line); + WriteLog(line); - // 检查启动 + // State == State.Started if !StartedKeywords.Any() if (State == State.Starting) { if (StartedKeywords.Any(s => line.Contains(s))) @@ -214,10 +247,8 @@ protected virtual void ReadOutput(TextReader reader) } } - _flushFileStreamTimer.Enabled = false; - _logStreamWriter!.Close(); - _logFileStream!.Close(); - _logStreamWriter = _logStreamWriter = null; + State = State.Stopped; + CloseLogFile(); } ///