UpdateLog.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using System.IO;
  3. namespace Microsoft.Samples.AppUpdater
  4. {
  5. internal class UpdateLog
  6. {
  7. private const string LogFileName = "AppUpdate.log";
  8. internal UpdateLog()
  9. {
  10. string path = Path.Combine(GetLogFilePath(), "AppUpdate.log");
  11. if (!File.Exists(path))
  12. {
  13. File.Create(path);
  14. File.SetAttributes(path, FileAttributes.Hidden);
  15. }
  16. }
  17. internal void AddSuccess(string versionNumber)
  18. {
  19. string path = Path.Combine(GetLogFilePath(), "AppUpdate.log");
  20. FileStream stream = File.Open(path, FileMode.Append);
  21. StreamWriter streamWriter = new StreamWriter(stream);
  22. string value = DateTime.Now.ToString() + ": Successful update to version: " + versionNumber;
  23. streamWriter.WriteLine(value);
  24. streamWriter.Close();
  25. }
  26. internal void AddError(string errorMessage)
  27. {
  28. string path = Path.Combine(GetLogFilePath(), "AppUpdate.log");
  29. FileStream stream = File.Open(path, FileMode.Append);
  30. StreamWriter streamWriter = new StreamWriter(stream);
  31. string value = DateTime.Now.ToString() + ": UPDATE FAILED with the following error message:";
  32. streamWriter.WriteLine(value);
  33. streamWriter.WriteLine(errorMessage);
  34. streamWriter.Close();
  35. }
  36. private string GetLogFilePath()
  37. {
  38. DirectoryInfo directoryInfo = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
  39. return directoryInfo.Parent.Parent.FullName + "\\";
  40. }
  41. }
  42. }