PomeloLog.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using CommonLang.Log;
  2. using log4net;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. namespace Pomelo
  8. {
  9. public class LogConfig
  10. {
  11. /// <summary>
  12. /// 服务器id
  13. /// </summary>
  14. public string serverId;
  15. /// <summary>
  16. /// 日志配置文件
  17. /// </summary>
  18. public string configFile;
  19. /// <summary>
  20. /// 日志输出目录
  21. /// </summary>
  22. public string outputPath;
  23. /// <summary>
  24. /// 日志输出等级
  25. /// </summary>
  26. public uint level;
  27. }
  28. public class PomeloLog : LoggerFactory
  29. {
  30. private uint logLevel;
  31. public PomeloLog(LogConfig config)
  32. {
  33. this.logLevel = config.level;
  34. if (this.logLevel == 0)
  35. {
  36. this.logLevel = 0xFF;
  37. }
  38. Console.WriteLine("C# log level:" + LogLevels(this.logLevel) + ",configFile:" + config.configFile);
  39. log4net.Config.XmlConfigurator.Configure(new FileInfo(config.configFile));
  40. //foreach (log4net.Appender.IAppender appender in log4net.LogManager.GetRepository().GetAppenders())
  41. //{
  42. // var targetApder = appender as log4net.Appender.RollingFileAppender;
  43. // if (targetApder != null)
  44. // {
  45. // var currentDir = System.AppDomain.CurrentDomain.BaseDirectory;
  46. // string newFile = targetApder.File.Replace(currentDir, "");
  47. // targetApder.File = config.outputPath + "/" + config.serverId + "/" + newFile;
  48. // +"-" + config.serverId + ".log";
  49. // targetApder.File = config.outputPath + "/" + newFile + "-" + config.serverId + ".log";
  50. // targetApder.ActivateOptions();
  51. // }
  52. //}
  53. //log4net.GlobalContext.Properties["ServerId"] = config.serverId;
  54. }
  55. private string AddLevel(uint levelMask, uint level, string name)
  56. {
  57. if ((level & levelMask) == levelMask)
  58. {
  59. return name + " ";
  60. }
  61. return "";
  62. }
  63. private string LogLevels(uint level)
  64. {
  65. string ret = "[";
  66. //ret += AddLevel(LoggerLevel.FINE, level, "FINE");
  67. //ret += AddLevel(LoggerLevel.TRACE, level, "TRACE");
  68. ret += AddLevel(LoggerLevel.DEBUG, level, "DEBUG");
  69. //ret += AddLevel(LoggerLevel.LOG, level, "LOG");
  70. ret += AddLevel(LoggerLevel.INFO, level, "INFO");
  71. ret += AddLevel(LoggerLevel.WARNNING, level, "WARN");
  72. ret += AddLevel(LoggerLevel.ERROR, level, "ERROR");
  73. //ret += AddLevel(LoggerLevel.FATAL, level, "FATAL");
  74. return ret + "]";
  75. }
  76. protected override Logger CreateLogger(string name)
  77. {
  78. Log4netLogger log = new Log4netLogger(log4net.LogManager.GetLogger(name));
  79. log.SetLevel(logLevel);
  80. return log;
  81. }
  82. }
  83. public class Log4netLogger : Logger
  84. {
  85. private log4net.ILog logger;
  86. public Log4netLogger(log4net.ILog logger)
  87. {
  88. this.logger = logger;
  89. }
  90. static readonly Dictionary<uint, ConsoleColor> logColor = new Dictionary<uint, ConsoleColor>()
  91. {
  92. { LoggerLevel.DEBUG , ConsoleColor.Green},
  93. { LoggerLevel.INFO , ConsoleColor.White},
  94. { LoggerLevel.WARNNING , ConsoleColor.Yellow},
  95. { LoggerLevel.ERROR , ConsoleColor.Red}
  96. };
  97. public void print2Console(uint level, string msg, Exception err = null)
  98. {
  99. ConsoleColor cc;
  100. if (logColor.TryGetValue(level, out cc))
  101. {
  102. Console.ForegroundColor = cc;
  103. }
  104. else
  105. {
  106. Console.ForegroundColor = ConsoleColor.White;
  107. }
  108. Console.WriteLine(msg);
  109. if (err != null)
  110. {
  111. Console.WriteLine(err.StackTrace);
  112. }
  113. Console.ForegroundColor = ConsoleColor.White;
  114. }
  115. public override void LogLevel(uint level, string msg, Exception err = null)
  116. {
  117. //print2Console(level, msg, err);
  118. switch (level)
  119. {
  120. //case LoggerLevel.TRACE:
  121. // logger.Debug(msg, err);
  122. // break;
  123. case LoggerLevel.DEBUG:
  124. logger.Debug(msg, err);
  125. break;
  126. case LoggerLevel.INFO:
  127. logger.Info(msg, err);
  128. break;
  129. //case LoggerLevel.LOG:
  130. // logger.Info(msg, err);
  131. // break;
  132. //case LoggerLevel.FINE:
  133. // logger.Info(msg, err);
  134. // break;
  135. case LoggerLevel.WARNNING:
  136. print2Console(level, msg, err);
  137. logger.Warn(msg, err);
  138. break;
  139. case LoggerLevel.ERROR:
  140. print2Console(level, msg, err);
  141. logger.Error(msg, err);
  142. break;
  143. //case LoggerLevel.FATAL:
  144. // logger.Fatal(msg, err);
  145. // break;
  146. default:
  147. print2Console(level, msg, err);
  148. logger.Error(msg, err);
  149. break;
  150. }
  151. }
  152. protected override void LogLevelInternal(uint level, object msg, object[] args, Exception exception)
  153. {
  154. if (IsLevelEnable(level))
  155. {
  156. //监控日志转成json格式
  157. if ("monitor".Equals(this.logger.Logger.Name))
  158. {
  159. //dynamic data = msg;
  160. //data.@version = "1";
  161. //data.@timestamp = DateTime.Now.ToString("o");
  162. //data.fields = new
  163. //{
  164. // level = "INFO",
  165. // category = this.logger.Logger.Name,
  166. // serverType = "csharp",
  167. // serverId = log4net.GlobalContext.Properties["ServerId"]
  168. //};
  169. string json = JsonConvert.SerializeObject(msg);
  170. //json转换没有@了,只能再加进去,后续想个高效一点的方式
  171. //json = json.Replace("timestamp", "@timestamp");
  172. //json = json.Replace("version", "@version");
  173. msg = json;
  174. }
  175. base.LogLevelInternal(level, msg, args, exception);
  176. }
  177. }
  178. }
  179. }