Log4NetHelper.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.IO;
  3. using log4net;
  4. using UnityEngine;
  5. namespace LogHelper
  6. {
  7. public static class Log4NetHelper
  8. {
  9. static Log4NetHelper()
  10. {
  11. string fileName = Path.Combine(Application.dataPath, "Plugins/Log/log4net.config");
  12. if (!File.Exists(fileName))
  13. {
  14. Debug.LogError("Log4Net No Config");
  15. return;
  16. }
  17. var file = new FileInfo(fileName);
  18. log4net.Config.XmlConfigurator.Configure(file);
  19. Log2 = LogManager.GetLogger(typeof(Log4NetHelper));
  20. }
  21. private static ILog Log { get; set; }
  22. private static ILog Log2 { get; set; }
  23. public static void LogInfo(object message)
  24. {
  25. Log2.Info(message);
  26. }
  27. public static void LogDebug(object message)
  28. {
  29. Log2.Debug(message);
  30. }
  31. public static void LogError(object message)
  32. {
  33. Log2.Error(message);
  34. }
  35. public static void LogError(Exception e)
  36. {
  37. Log2.Error(e);
  38. }
  39. public static void LogFatal(object message)
  40. {
  41. Log2.Fatal(message);
  42. }
  43. public static void LogFatal(Exception e)
  44. {
  45. Log2.Fatal(e);
  46. }
  47. public static void LogWarn(object message)
  48. {
  49. Log2.Warn(message);
  50. }
  51. public static void LogInfo<T>(this T t, object message) where T : class
  52. {
  53. Log = LogManager.GetLogger(t.GetType());
  54. Log.Info(message);
  55. }
  56. public static void LogDebug<T>(this T t, object message) where T : class
  57. {
  58. Log = LogManager.GetLogger(t.GetType());
  59. Log.Debug(message);
  60. }
  61. public static void LogError<T>(this T t, object message) where T : class
  62. {
  63. Log = LogManager.GetLogger(t.GetType());
  64. Log.Error(message);
  65. }
  66. public static void LogError<T>(this T t, Exception e) where T : class
  67. {
  68. Log = LogManager.GetLogger(t.GetType());
  69. Log.Error(e);
  70. }
  71. public static void LogFatal<T>(this T t, object message) where T : class
  72. {
  73. Log = LogManager.GetLogger(t.GetType());
  74. Log.Fatal(message);
  75. }
  76. public static void LogFatal<T>(this T t, Exception e) where T : class
  77. {
  78. Log = LogManager.GetLogger(t.GetType());
  79. Log.Fatal(e);
  80. }
  81. public static void LogWarn<T>(this T t, object message) where T : class
  82. {
  83. Log = LogManager.GetLogger(t.GetType());
  84. Log.Warn(message);
  85. }
  86. }
  87. }