save logs in a list for a later status command
This commit is contained in:
parent
af700c798e
commit
9771a55394
1 changed files with 16 additions and 2 deletions
|
@ -5,6 +5,7 @@ namespace netsd.Utils;
|
|||
|
||||
public class StartService
|
||||
{
|
||||
private readonly List<Log> _logs;
|
||||
private readonly ServiceParser _parseServiceFile;
|
||||
private readonly Process _process;
|
||||
private readonly string _servicePath;
|
||||
|
@ -14,6 +15,7 @@ public class StartService
|
|||
_parseServiceFile = parseServiceFile;
|
||||
_servicePath = servicePath;
|
||||
_process = new Process();
|
||||
_logs = new List<Log>();
|
||||
|
||||
_process.StartInfo.FileName = _parseServiceFile.ExecStart?.Split(' ').First();
|
||||
_process.StartInfo.Arguments = string.Join(" ", _parseServiceFile.ExecStart?.Split(' ').Skip(1)!);
|
||||
|
@ -23,9 +25,9 @@ public class StartService
|
|||
_process.EnableRaisingEvents = true;
|
||||
|
||||
_process.OutputDataReceived += (sender, args) =>
|
||||
Init.Logger.Information($"({Path.GetFileName(_servicePath)}) {args.Data}");
|
||||
_logs.Add(new Log { Type = LogType.StdOut, Message = args.Data! });
|
||||
_process.ErrorDataReceived += (sender, args) =>
|
||||
Init.Logger.Error($"({Path.GetFileName(_servicePath)}) {args.Data}");
|
||||
_logs.Add(new Log { Type = LogType.StdErr, Message = args.Data! });
|
||||
_process.Exited += (sender, args) =>
|
||||
{
|
||||
if (_process.ExitCode != 0)
|
||||
|
@ -49,3 +51,15 @@ public class StartService
|
|||
await _process.WaitForExitAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public struct Log
|
||||
{
|
||||
public LogType Type;
|
||||
public string Message;
|
||||
}
|
||||
|
||||
public enum LogType
|
||||
{
|
||||
StdOut = 1,
|
||||
StdErr = 2,
|
||||
}
|
||||
|
|
Reference in a new issue