diff --git a/Remotely_Agent/Services/Logger.cs b/Remotely_Agent/Services/Logger.cs index 597b9144..68927b62 100644 --- a/Remotely_Agent/Services/Logger.cs +++ b/Remotely_Agent/Services/Logger.cs @@ -14,48 +14,22 @@ namespace Remotely_Agent.Services { public static void Write(string message) { - var path = Path.Combine(Path.GetTempPath(), "Remotely_Logs.txt"); - if (!File.Exists(path)) + try { - File.Create(path).Close(); - if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + var path = Path.Combine(Path.GetTempPath(), "Remotely_Logs.txt"); + if (!File.Exists(path)) { - Process.Start("sudo", $"chmod 666 {path}").WaitForExit(); + File.Create(path).Close(); + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + Process.Start("sudo", $"chmod 666 {path}").WaitForExit(); + } } - } - var jsoninfo = new - { - Type = "Info", - Timestamp = DateTime.Now.ToString(), - Message = message - }; - if (File.Exists(path)) - { - var fi = new FileInfo(path); - while (fi.Length > 1000000) + var jsoninfo = new { - var content = File.ReadAllLines(path); - File.WriteAllLines(path, content.Skip(10)); - fi = new FileInfo(path); - } - } - File.AppendAllText(path, JsonConvert.SerializeObject(jsoninfo) + Environment.NewLine); - } - - public static void Write(Exception ex) - { - var exception = ex; - var path = Path.Combine(Path.GetTempPath(), "Remotely_Logs.txt"); - - while (exception != null) - { - var jsonError = new - { - Type = "Error", + Type = "Info", Timestamp = DateTime.Now.ToString(), - Message = exception?.Message, - Source = exception?.Source, - StackTrace = exception?.StackTrace, + Message = message }; if (File.Exists(path)) { @@ -67,9 +41,43 @@ namespace Remotely_Agent.Services fi = new FileInfo(path); } } - File.AppendAllText(path, JsonConvert.SerializeObject(jsonError) + Environment.NewLine); - exception = exception.InnerException; + File.AppendAllText(path, JsonConvert.SerializeObject(jsoninfo) + Environment.NewLine); } + catch { } + } + + public static void Write(Exception ex) + { + try + { + var exception = ex; + var path = Path.Combine(Path.GetTempPath(), "Remotely_Logs.txt"); + + while (exception != null) + { + var jsonError = new + { + Type = "Error", + Timestamp = DateTime.Now.ToString(), + Message = exception?.Message, + Source = exception?.Source, + StackTrace = exception?.StackTrace, + }; + if (File.Exists(path)) + { + var fi = new FileInfo(path); + while (fi.Length > 1000000) + { + var content = File.ReadAllLines(path); + File.WriteAllLines(path, content.Skip(10)); + fi = new FileInfo(path); + } + } + File.AppendAllText(path, JsonConvert.SerializeObject(jsonError) + Environment.NewLine); + exception = exception.InnerException; + } + } + catch { } } } } diff --git a/Remotely_ScreenCast.Core/Utilities/Logger.cs b/Remotely_ScreenCast.Core/Utilities/Logger.cs index 054cf215..a2863d85 100644 --- a/Remotely_ScreenCast.Core/Utilities/Logger.cs +++ b/Remotely_ScreenCast.Core/Utilities/Logger.cs @@ -14,67 +14,22 @@ namespace Remotely_ScreenCast.Core.Utilities { public static void Write(string message) { - var path = Path.Combine(Path.GetTempPath(), "Remotely_Logs.txt"); - if (!File.Exists(path)) - { - File.Create(path).Close(); - if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - { - Process.Start("sudo", $"chmod 666 {path}").WaitForExit(); - } - } - var jsoninfo = new - { - Type = "Info", - Timestamp = DateTime.Now.ToString(), - Message = message - }; - if (File.Exists(path)) - { - var fi = new FileInfo(path); - while (fi.Length > 1000000) - { - var content = File.ReadAllLines(path); - File.WriteAllLines(path, content.Skip(10)); - fi = new FileInfo(path); - } - } try { - File.AppendAllText(path, JsonConvert.SerializeObject(jsoninfo) + Environment.NewLine); - } - catch - { - Task.Delay(1000).ContinueWith((Task task) => + var path = Path.Combine(Path.GetTempPath(), "Remotely_Logs.txt"); + if (!File.Exists(path)) { - Write(message); - }); - } - } - - public static void Write(Exception ex) - { - var exception = ex; - var path = Path.Combine(Path.GetTempPath(), "Remotely_Logs.txt"); - - if (!File.Exists(path)) - { - File.Create(path).Close(); - if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - { - Process.Start("sudo", $"chmod 666 {path}").WaitForExit(); + File.Create(path).Close(); + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + Process.Start("sudo", $"chmod 666 {path}").WaitForExit(); + } } - } - - while (exception != null) - { - var jsonError = new + var jsoninfo = new { - Type = "Error", + Type = "Info", Timestamp = DateTime.Now.ToString(), - Message = exception?.Message, - Source = exception?.Source, - StackTrace = exception?.StackTrace, + Message = message }; if (File.Exists(path)) { @@ -86,9 +41,62 @@ namespace Remotely_ScreenCast.Core.Utilities fi = new FileInfo(path); } } - File.AppendAllText(path, JsonConvert.SerializeObject(jsonError) + Environment.NewLine); - exception = exception.InnerException; + try + { + File.AppendAllText(path, JsonConvert.SerializeObject(jsoninfo) + Environment.NewLine); + } + catch + { + Task.Delay(1000).ContinueWith((Task task) => + { + Write(message); + }); + } } + catch { } + } + + public static void Write(Exception ex) + { + try + { + var exception = ex; + var path = Path.Combine(Path.GetTempPath(), "Remotely_Logs.txt"); + + if (!File.Exists(path)) + { + File.Create(path).Close(); + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + Process.Start("sudo", $"chmod 666 {path}").WaitForExit(); + } + } + + while (exception != null) + { + var jsonError = new + { + Type = "Error", + Timestamp = DateTime.Now.ToString(), + Message = exception?.Message, + Source = exception?.Source, + StackTrace = exception?.StackTrace, + }; + if (File.Exists(path)) + { + var fi = new FileInfo(path); + while (fi.Length > 1000000) + { + var content = File.ReadAllLines(path); + File.WriteAllLines(path, content.Skip(10)); + fi = new FileInfo(path); + } + } + File.AppendAllText(path, JsonConvert.SerializeObject(jsonError) + Environment.NewLine); + exception = exception.InnerException; + } + } + catch { } } } }