IpsLogger.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Ips.Library.Basic
  8. {
  9. public class IpsLogger
  10. {
  11. public static event EventHandler<IpsLogEventArg> Logged;
  12. public static void Info(string msg, bool writeToUI = true)
  13. {
  14. Log(LogType.Info, msg, null, writeToUI);
  15. }
  16. public static void Warn(string msg, Exception ex = null, bool writeToUI = true)
  17. {
  18. Log(LogType.Warn, msg, ex, writeToUI);
  19. }
  20. public static void Error(string msg, Exception ex = null, bool writeToUI = true)
  21. {
  22. Log(LogType.Error, msg, ex, writeToUI);
  23. }
  24. public static void Log(LogType logType, string logContent, Exception ex = null, bool writeToUI = true)
  25. {
  26. Logged?.Invoke(null, new IpsLogEventArg(logType, logContent, ex, writeToUI));
  27. }
  28. }
  29. public enum LogType
  30. {
  31. [Description("消息")]
  32. Info,
  33. [Description("警告")]
  34. Warn,
  35. [Description("错误")]
  36. Error
  37. }
  38. public class IpsLogEventArg : EventArgs
  39. {
  40. public IpsLogEventArg(LogType logType, string content, Exception ex = null, bool writeToUI = true)
  41. {
  42. LogType = logType;
  43. Content = content;
  44. this.Exception = ex;
  45. WriteToUI = writeToUI;
  46. }
  47. public LogType LogType { get; set; }
  48. public string Content { get; set; }
  49. public Exception Exception { get; set; }
  50. public bool WriteToUI { get; set; }
  51. }
  52. }