123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- using CommonLang.Log;
- using log4net;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.IO;
- namespace Pomelo
- {
- public class LogConfig
- {
- /// <summary>
- /// 服务器id
- /// </summary>
- public string serverId;
- /// <summary>
- /// 日志配置文件
- /// </summary>
- public string configFile;
- /// <summary>
- /// 日志输出目录
- /// </summary>
- public string outputPath;
- /// <summary>
- /// 日志输出等级
- /// </summary>
- public uint level;
- }
- public class PomeloLog : LoggerFactory
- {
- private uint logLevel;
- public PomeloLog(LogConfig config)
- {
- this.logLevel = config.level;
- if (this.logLevel == 0)
- {
- this.logLevel = 0xFF;
- }
- Console.WriteLine("C# log level:" + LogLevels(this.logLevel) + ",configFile:" + config.configFile);
- log4net.Config.XmlConfigurator.Configure(new FileInfo(config.configFile));
- //foreach (log4net.Appender.IAppender appender in log4net.LogManager.GetRepository().GetAppenders())
- //{
- // var targetApder = appender as log4net.Appender.RollingFileAppender;
- // if (targetApder != null)
- // {
- // var currentDir = System.AppDomain.CurrentDomain.BaseDirectory;
- // string newFile = targetApder.File.Replace(currentDir, "");
- // targetApder.File = config.outputPath + "/" + config.serverId + "/" + newFile;
- // +"-" + config.serverId + ".log";
- // targetApder.File = config.outputPath + "/" + newFile + "-" + config.serverId + ".log";
- // targetApder.ActivateOptions();
- // }
- //}
- //log4net.GlobalContext.Properties["ServerId"] = config.serverId;
- }
- private string AddLevel(uint levelMask, uint level, string name)
- {
- if ((level & levelMask) == levelMask)
- {
- return name + " ";
- }
- return "";
- }
- private string LogLevels(uint level)
- {
- string ret = "[";
- //ret += AddLevel(LoggerLevel.FINE, level, "FINE");
- //ret += AddLevel(LoggerLevel.TRACE, level, "TRACE");
- ret += AddLevel(LoggerLevel.DEBUG, level, "DEBUG");
- //ret += AddLevel(LoggerLevel.LOG, level, "LOG");
- ret += AddLevel(LoggerLevel.INFO, level, "INFO");
- ret += AddLevel(LoggerLevel.WARNNING, level, "WARN");
- ret += AddLevel(LoggerLevel.ERROR, level, "ERROR");
- //ret += AddLevel(LoggerLevel.FATAL, level, "FATAL");
- return ret + "]";
- }
- protected override Logger CreateLogger(string name)
- {
- Log4netLogger log = new Log4netLogger(log4net.LogManager.GetLogger(name));
- log.SetLevel(logLevel);
- return log;
- }
- }
- public class Log4netLogger : Logger
- {
- private log4net.ILog logger;
- public Log4netLogger(log4net.ILog logger)
- {
- this.logger = logger;
- }
- static readonly Dictionary<uint, ConsoleColor> logColor = new Dictionary<uint, ConsoleColor>()
- {
- { LoggerLevel.DEBUG , ConsoleColor.Green},
- { LoggerLevel.INFO , ConsoleColor.White},
- { LoggerLevel.WARNNING , ConsoleColor.Yellow},
- { LoggerLevel.ERROR , ConsoleColor.Red}
- };
- public void print2Console(uint level, string msg, Exception err = null)
- {
- ConsoleColor cc;
- if (logColor.TryGetValue(level, out cc))
- {
- Console.ForegroundColor = cc;
- }
- else
- {
- Console.ForegroundColor = ConsoleColor.White;
- }
- Console.WriteLine(msg);
- if (err != null)
- {
- Console.WriteLine(err.StackTrace);
- }
- Console.ForegroundColor = ConsoleColor.White;
- }
- public override void LogLevel(uint level, string msg, Exception err = null)
- {
- //print2Console(level, msg, err);
- switch (level)
- {
- //case LoggerLevel.TRACE:
- // logger.Debug(msg, err);
- // break;
- case LoggerLevel.DEBUG:
- logger.Debug(msg, err);
- break;
- case LoggerLevel.INFO:
- logger.Info(msg, err);
- break;
- //case LoggerLevel.LOG:
- // logger.Info(msg, err);
- // break;
- //case LoggerLevel.FINE:
- // logger.Info(msg, err);
- // break;
- case LoggerLevel.WARNNING:
- print2Console(level, msg, err);
- logger.Warn(msg, err);
- break;
- case LoggerLevel.ERROR:
- print2Console(level, msg, err);
- logger.Error(msg, err);
- break;
- //case LoggerLevel.FATAL:
- // logger.Fatal(msg, err);
- // break;
- default:
- print2Console(level, msg, err);
- logger.Error(msg, err);
- break;
- }
- }
- protected override void LogLevelInternal(uint level, object msg, object[] args, Exception exception)
- {
- if (IsLevelEnable(level))
- {
- //监控日志转成json格式
- if ("monitor".Equals(this.logger.Logger.Name))
- {
- //dynamic data = msg;
- //data.@version = "1";
- //data.@timestamp = DateTime.Now.ToString("o");
- //data.fields = new
- //{
- // level = "INFO",
- // category = this.logger.Logger.Name,
- // serverType = "csharp",
- // serverId = log4net.GlobalContext.Properties["ServerId"]
- //};
- string json = JsonConvert.SerializeObject(msg);
- //json转换没有@了,只能再加进去,后续想个高效一点的方式
- //json = json.Replace("timestamp", "@timestamp");
- //json = json.Replace("version", "@version");
- msg = json;
- }
- base.LogLevelInternal(level, msg, args, exception);
- }
- }
- }
- }
|