Logger.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using System.Text;
  3. using System.IO;
  4. namespace Vinno.IdentificationCardApp
  5. {
  6. public class Logger
  7. {
  8. private static readonly object Locker = new object();
  9. private static string LogDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "IDCardToolLogs");
  10. static Logger()
  11. {
  12. }
  13. private static string FilePath
  14. {
  15. get { return Path.Combine(LogDir, DateTime.Now.ToString("yyyyMMdd") + ".txt"); }
  16. }
  17. private static void Write(string msg)
  18. {
  19. lock (Locker)
  20. {
  21. if (!File.Exists(FilePath))
  22. {
  23. if (!Directory.Exists(LogDir))
  24. Directory.CreateDirectory(LogDir);
  25. }
  26. using (var writer = new StreamWriter(FilePath, true, Encoding.UTF8))
  27. {
  28. writer.WriteLine(msg);
  29. }
  30. }
  31. }
  32. public static void Debug(string msg)
  33. {
  34. msg = string.Format("[{0} {1}] : {2}", "Debug", DateTime.Now, msg);
  35. Write(msg);
  36. }
  37. public static void Info(string msg)
  38. {
  39. msg = string.Format("[{0} {1}] : {2}", "Info", DateTime.Now, msg);
  40. Write(msg);
  41. }
  42. public static void Error(string msg)
  43. {
  44. msg = string.Format("[{0} {1}] : {2}", "Error", DateTime.Now, msg);
  45. Write(msg);
  46. }
  47. public static void Error(string desc, Exception exception)
  48. {
  49. var msg = string.Format("[{0} {1}] : {2} {3}", "Error", DateTime.Now, desc, exception);
  50. Write(msg);
  51. }
  52. }
  53. }