CLI tool for running Playbooks
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.6 KiB

1 year ago
  1. 
  2. using System;
  3. using System.IO;
  4. using System.Windows.Forms;
  5. namespace TrustedUninstaller.Shared
  6. {
  7. public class ErrorLogger
  8. {
  9. public static void WriteToErrorLog(string msg, string stkTrace, string title, string? item = null)
  10. {
  11. if (!(Directory.Exists(Directory.GetCurrentDirectory() + "\\Logs")))
  12. {
  13. Directory.CreateDirectory(Directory.GetCurrentDirectory() + "\\Logs");
  14. }
  15. try
  16. {
  17. FileStream fs = new FileStream(Directory.GetCurrentDirectory() + "\\Logs\\ErrorLog.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
  18. StreamWriter s = new StreamWriter(fs);
  19. s.Close();
  20. fs.Close();
  21. FileStream fs1 = new FileStream(Directory.GetCurrentDirectory() + "\\Logs\\ErrorLog.txt", FileMode.Append, FileAccess.Write);
  22. StreamWriter s1 = new StreamWriter(fs1);
  23. s1.WriteLine("Title: " + title);
  24. s1.WriteLine("Message: " + msg.TrimEnd('\n').TrimEnd('\r'));
  25. if (stkTrace != null) s1.WriteLine(Environment.NewLine + "StackTrace: " + stkTrace + Environment.NewLine);
  26. if (item != null) s1.WriteLine("Object: " + item);
  27. s1.WriteLine("Date/Time: " + DateTime.Now);
  28. s1.WriteLine
  29. ("============================================");
  30. s1.Close();
  31. fs1.Close();
  32. }
  33. catch (Exception e)
  34. {
  35. Console.WriteLine("ERROR WRITING INTO THE ERROR LOG: " + e.Message);
  36. }
  37. }
  38. }
  39. }