/// <summary> /// 写日志 /// </summary> /// <param name="config">记录配置(文件保存路径)</param> /// <param name="entity">记录实体(记录包含信息)</param> public static void Log(LoggerConfig config, LogEntity entity) { // 日志项占位符替换 var ps = new Dictionary <string, Func <string> > { ["LogOut"] = delegate() { return(EL.Parse(config.LogOutTemplate, new Dictionary <string, object> { ["Date"] = entity.LogTime.ToString(config.DateFormat) })); //return "./log/" + entity.LoggerName + ".log"; } }; var logitem = EL.Parse(config.LogItemTemplate, entity, config.DynanicMap, @"\$\{", @"\}"); // 文件名占位符替换 var filename = EL.Parse(config.FileNameTemplate, entity, config.DynanicMap, @"\$\{", @"\}"); // 输出->控制台 Console.WriteLine(logitem); //Console.WriteLine(EL.Parse(config.FileNameFormat, new { Date = entity.LogTime.ToString(config.DateFormat), entity.LoggerName })); //string.Format("", ) // {name, value, type, format, convertor: (format)=>string} // 输出->文件 TODO: 根据配置文件限制Trace等日志输出到文件 switch (entity.LogLevel) { case LogLevels.Error: // 是否错误日志输出独立,默认独立 ErrOut: "./log/${LoggerName}/error/${Date}.log" File.WriteAllText(config.DynanicMap["ErrOut"](entity), logitem + "\r\n", true); break; default: File.WriteAllText(config.DynanicMap["LogOut"](entity), logitem + "\r\n", true); break; } }
/// <summary> /// 获取日志器 /// </summary> /// <typeparam name="CategoryName"></typeparam> /// <returns></returns> public static ILogger GetLogger <CategoryName>() { return(new DefaultLogger(new LoggerConfig { LogOutTemplate = "./log/${LoggerName}/${Date}.log", ClassFullName = typeof(CategoryName).FullName, LoggerName = typeof(CategoryName).Name, FileNameTemplate = "${Date}.log", TimeFormat = "HH:mm:ss.FFFFFFK", DateFormat = "yyyy-MM-dd", LogItemTemplate = "[${DateTime}] [${LogLevel}] [${LoggerName}] ${Message}", DynanicMap = new Dictionary <string, Func <object, string> > { ["Date"] = delegate(object entity) { return (entity as LogEntity)?.LogTime.ToString("yyyy-MM-dd"); }, ["DateTime"] = delegate(object entity) { return (entity as LogEntity)?.LogTime.ToString("yyyy-MM-dd HH:mm:ss"); }, ["LoggerName"] = delegate(object entity) { return (entity as LogEntity)?.LoggerName; }, ["LogLevel"] = delegate(object entity) { return (entity as LogEntity).LogLevel.ToString(); }, ["Message"] = delegate(object entity) { return (entity as LogEntity)?.Message; }, ["ErrOut"] = delegate(object entity) { // return EL.Parse((entity as LogEntity)?.ErrOut, ) return EL.Parse("./log/${LoggerName}/error/${Date}.log", new Dictionary <string, object> { ["Date"] = (entity as LogEntity)?.LogTime.ToString("yyyy-MM-dd"), ["LoggerName"] = (entity as LogEntity)?.LoggerName }); }, ["LogOut"] = delegate(object entity) { return EL.Parse("./log/${LoggerName}/${Date}.log", new Dictionary <string, object> { ["Date"] = (entity as LogEntity)?.LogTime.ToString("yyyy-MM-dd"), ["LoggerName"] = (entity as LogEntity)?.LoggerName }); }, ["LogItem"] = delegate(object entity) { return EL.Parse("[${DateTime}] [${LogLevel}] [${LoggerName}] ${Message}", new { DateTime = (entity as LogEntity)?.LogTime.ToString("yyyy-MM-dd HH:mm:ss"), (entity as LogEntity)?.LogLevel, (entity as LogEntity)?.LoggerName, (entity as LogEntity)?.Message }); } } })); }
/// <summary> /// 配置文件包含日志器名以及文件位置和日志格式等内容 /// </summary> /// <param name="config">日志器配置</param> public DefaultLogger(LoggerConfig config) { Config = config; KeyValues = new Dictionary <string, Func <object, string> > { ["LogOut"] = delegate(object entity) { return(EL.Parse(config.LogOutTemplate, new Dictionary <string, object> { ["Date"] = (entity as LogEntity).LogTime.ToString(Config.DateFormat) })); //return "./log/" + entity.LoggerName + ".log"; } }; }