From 6411f297232f179f6f3a20073d74c507f6a7c419 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Wed, 12 Jan 2022 18:53:22 +0200 Subject: [PATCH] log actions messages --- .../Services/Abstractions/ICliService.cs | 1 + .../Services/Linux/BashService.cs | 5 ++ .../Services/Windows/CmdService.cs | 53 ++++++++++--------- .../Services/Windows/WindowsPowerService.cs | 48 ++++++++--------- .../NetworkResurrector.Agent/appsettings.json | 2 +- 5 files changed, 56 insertions(+), 53 deletions(-) diff --git a/src/agent/NetworkResurrector.Agent.Application/Services/Abstractions/ICliService.cs b/src/agent/NetworkResurrector.Agent.Application/Services/Abstractions/ICliService.cs index 07c6f2b..244f985 100644 --- a/src/agent/NetworkResurrector.Agent.Application/Services/Abstractions/ICliService.cs +++ b/src/agent/NetworkResurrector.Agent.Application/Services/Abstractions/ICliService.cs @@ -3,5 +3,6 @@ public interface ICliService { (bool success, string message) Execute(string command); + (bool success, string message) Shutdown(string arguments); } } diff --git a/src/agent/NetworkResurrector.Agent.Application/Services/Linux/BashService.cs b/src/agent/NetworkResurrector.Agent.Application/Services/Linux/BashService.cs index 56c5dd1..f88398d 100644 --- a/src/agent/NetworkResurrector.Agent.Application/Services/Linux/BashService.cs +++ b/src/agent/NetworkResurrector.Agent.Application/Services/Linux/BashService.cs @@ -9,5 +9,10 @@ namespace NetworkResurrector.Agent.Application.Services.Linux { throw new NotImplementedException(); } + + public (bool success, string message) Shutdown(string arguments) + { + throw new NotImplementedException(); + } } } diff --git a/src/agent/NetworkResurrector.Agent.Application/Services/Windows/CmdService.cs b/src/agent/NetworkResurrector.Agent.Application/Services/Windows/CmdService.cs index 9a50a46..d1ab390 100644 --- a/src/agent/NetworkResurrector.Agent.Application/Services/Windows/CmdService.cs +++ b/src/agent/NetworkResurrector.Agent.Application/Services/Windows/CmdService.cs @@ -7,15 +7,13 @@ namespace NetworkResurrector.Agent.Application.Services.Windows { internal class CmdService : ICliService { - public CmdService() - { - } + public (bool success, string message) Execute(string command) => Run("cmd.exe", command); + public (bool success, string message) Shutdown(string arguments) => Run("shutdown.exe", arguments); - public (bool success, string message) Execute(string command) + private (bool success, string message) Run(string fileName, string arguments) { - var msg = new StringBuilder(); var commandOutput = new StringBuilder(); - var commandError = new StringBuilder(); + var commandErrors = new StringBuilder(); var process = new Process(); process.StartInfo.RedirectStandardError = true; @@ -23,27 +21,25 @@ namespace NetworkResurrector.Agent.Application.Services.Windows process.StartInfo.RedirectStandardInput = true; process.StartInfo.CreateNoWindow = true; process.StartInfo.UseShellExecute = false; - process.StartInfo.FileName = "cmd.exe"; + process.StartInfo.FileName = fileName; commandOutput.Clear(); - commandError.Clear(); + commandErrors.Clear(); - process.OutputDataReceived += new DataReceivedEventHandler( - (s, e) => - { - if (!string.IsNullOrEmpty(e.Data)) - commandOutput.Append(e.Data + "\n"); - }); - process.ErrorDataReceived += new DataReceivedEventHandler( - (s, e) => - { - if (!string.IsNullOrEmpty(e.Data)) - commandError.Append(e.Data + "\n"); - }); + process.OutputDataReceived += new DataReceivedEventHandler((s, e) => + { + if (!string.IsNullOrEmpty(e.Data)) + commandOutput.Append($"{e.Data}{Environment.NewLine}"); + }); + + process.ErrorDataReceived += new DataReceivedEventHandler((s, e) => + { + if (!string.IsNullOrEmpty(e.Data)) + commandErrors.Append($"{e.Data}{Environment.NewLine}"); + }); - msg.Append($"\nProcess started at: {DateTime.Now}\n"); process.Start(); - process.StandardInput.WriteLine(command); + process.StandardInput.WriteLine(arguments); process.StandardInput.Flush(); process.StandardInput.Close(); @@ -51,11 +47,16 @@ namespace NetworkResurrector.Agent.Application.Services.Windows process.BeginErrorReadLine(); process.WaitForExit(); - msg.Append($"Output:\n {commandOutput}\n"); - msg.Append($"Errors:\n {commandError}\n"); - msg.Append($"Process ended at: {DateTime.Now}"); - return (commandError.Length == 0, msg.ToString()); + var msg = new StringBuilder(); + + if (commandOutput.Length > 0) + msg.Append($"Output:{Environment.NewLine}{commandOutput}{Environment.NewLine}"); + + if (commandErrors.Length > 0) + msg.Append($"Errors:{Environment.NewLine}{commandErrors}{Environment.NewLine}"); + + return (commandErrors.Length == 0, msg.ToString()); } } } diff --git a/src/agent/NetworkResurrector.Agent.Application/Services/Windows/WindowsPowerService.cs b/src/agent/NetworkResurrector.Agent.Application/Services/Windows/WindowsPowerService.cs index 075be3a..6bc38d7 100644 --- a/src/agent/NetworkResurrector.Agent.Application/Services/Windows/WindowsPowerService.cs +++ b/src/agent/NetworkResurrector.Agent.Application/Services/Windows/WindowsPowerService.cs @@ -2,7 +2,6 @@ using NetworkResurrector.Agent.Application.Services.Abstractions; using NetworkResurrector.Agent.Domain.Models; using System; -using System.Diagnostics; using System.Security.Principal; namespace NetworkResurrector.Agent.Application.Services.Windows @@ -18,17 +17,6 @@ namespace NetworkResurrector.Agent.Application.Services.Windows _cliService=cliService; } - private void StartShutdownProcess(string arguments) - { - var psi = new ProcessStartInfo("shutdown.exe", arguments) - { - CreateNoWindow = true, - UseShellExecute = false - }; - - Process.Start(psi); - } - private string SetOptions(string arguments, PowerOptions options) { if (options == null) @@ -49,12 +37,12 @@ namespace NetworkResurrector.Agent.Application.Services.Windows private bool IsSystemUser() { bool isSystem; - #pragma warning disable CA1416 // Validate platform compatibility +#pragma warning disable CA1416 // Validate platform compatibility using (var identity = WindowsIdentity.GetCurrent()) { isSystem = identity.IsSystem; } - #pragma warning restore CA1416 // Validate platform compatibility +#pragma warning restore CA1416 // Validate platform compatibility return isSystem; } @@ -66,14 +54,22 @@ namespace NetworkResurrector.Agent.Application.Services.Windows throw new Exception("This action is not available for system users."); } + private void ManageExecutionResponse((bool success, string message) input) + { + var (success, message) = input; + if (success) + _logger.LogDebug(message); + else + throw new Exception(message); + } public void Shutdown(PowerOptions options) { var arguments = "-s"; arguments = SetOptions(arguments, options); _logger.LogInformation($"Shutdown arguments: {arguments}"); - - StartShutdownProcess(arguments); + var result =_cliService.Shutdown(arguments); + ManageExecutionResponse(result); } public void Restart(PowerOptions options) @@ -81,7 +77,8 @@ namespace NetworkResurrector.Agent.Application.Services.Windows var arguments = "-r"; arguments = SetOptions(arguments, options); _logger.LogInformation($"Restart arguments: {arguments}"); - StartShutdownProcess(arguments); + var result = _cliService.Shutdown(arguments); + ManageExecutionResponse(result); } public void Sleep(PowerOptions options) @@ -89,30 +86,29 @@ namespace NetworkResurrector.Agent.Application.Services.Windows var arguments = "-h"; arguments = SetOptions(arguments, options); _logger.LogInformation($"Sleep arguments: {arguments}"); - StartShutdownProcess(arguments); + var result =_cliService.Shutdown(arguments); + ManageExecutionResponse(result); } public void Logout() { ValidateLocalOrDomainUser(); - StartShutdownProcess("-l"); + var result = _cliService.Shutdown("-l"); + ManageExecutionResponse(result); } public void Lock() { ValidateLocalOrDomainUser(); var command = "rundll32.exe user32.dll,LockWorkStation"; - var (success, message) = _cliService.Execute(command); - var msg = $"Command: {command} | ExecutionLog: {message}"; - if (success) - _logger.LogDebug(msg); - else - throw new Exception(msg); + var result = _cliService.Execute(command); + ManageExecutionResponse(result); } public void Cancel() { - StartShutdownProcess("-a"); + var result = _cliService.Shutdown("-a"); + ManageExecutionResponse(result); } } } diff --git a/src/agent/NetworkResurrector.Agent/appsettings.json b/src/agent/NetworkResurrector.Agent/appsettings.json index 7f54a28..c3d9be2 100644 --- a/src/agent/NetworkResurrector.Agent/appsettings.json +++ b/src/agent/NetworkResurrector.Agent/appsettings.json @@ -6,7 +6,7 @@ }, "Logging": { "LogLevel": { - "Default": "Information", + "Default": "Debug", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" }