C#.NET根据不同业务类别类型写入不同文件中,动态创建log4net实例验证
参考了:https://www.cnblogs.com/-dawn/p/8598566.html
GetLog4netInstance:
using log4net; using log4net.Appender; using log4net.Config; using log4net.Core; using log4net.Filter; using log4net.Layout; using log4net.Repository; using System; using System.Collections.Generic; using System.Linq; using System.Reflection.Emit; using System.Text; namespace ConsoleApp1 { public class GetLog4netInstance { private static Dictionary<string, ILog> Logers = new Dictionary<string, ILog>(); /// <summary> /// 获取log实例 /// </summary> /// <param name="name"></param> /// <returns></returns> public static ILog GetLogger(string name) { if (Logers.ContainsKey(name)) return Logers[name]; else { lock (Logers) { if (Logers.ContainsKey(name)) return Logers[name]; else { var newLoger = CreateLogerInstance(name); Logers.Add(name, newLoger); return newLoger; } } } } /// <summary> /// 创建Log实例 /// </summary> /// <param name="name"></param> /// <returns></returns> private static ILog CreateLogerInstance(string name) { // Pattern Layout //%d 时间,[%t] 线程ID,%-2.18M 方法名(最少2位,最多18位),%m%n 消息和换行 string PatternStr = "%d [%t] %-2.18M - %m%n"; PatternLayout layout = new PatternLayout(PatternStr); // Level Filter LevelMatchFilter filter = new LevelMatchFilter(); filter.LevelToMatch = Level.All; filter.ActivateOptions(); // File Appender RollingFileAppender appender = new RollingFileAppender(); // 目录 //appender.File = $"logs\\{name}\\"; //按名称单独一个目录 appender.File = "logs\\"; // 所有名称放同一目录 // 立即写入磁盘 appender.ImmediateFlush = true; // true:追加到文件;false:覆盖文件 appender.AppendToFile = true; // 新的日期或者文件大小达到上限,新建一个文件 appender.RollingStyle = RollingFileAppender.RollingMode.Composite; // 文件大小达到上限,新建文件时,文件编号放到文件后缀前面 appender.PreserveLogFileNameExtension = true; // 时间模式 //appender.DatePattern = "yyyyMMdd'.txt'"; // 日期.txt appender.DatePattern = "'" + name + "-'" + "yyyyMMdd'.txt'"; // 名称-日期.txt,"'名称-'yyyyMMdd'.txt'" // 最小锁定模型以允许多个进程可以写入同一个文件 appender.LockingModel = new FileAppender.MinimalLock(); appender.Name = name+"Appender"; appender.AddFilter(filter); appender.Layout = layout; appender.ActivateOptions(); // 文件大小上限 appender.MaximumFileSize = "100MB"; // 设置无限备份=-1 ,最大备份数为30 appender.MaxSizeRollBackups = 999; appender.StaticLogFileName = false; //create instance string repositoryName = name+"Repository"; ILoggerRepository repository = LoggerManager.CreateRepository(repositoryName); BasicConfigurator.Configure(repository, appender); //After the log instance initialization, we can get the instance from the LogManager by the special log instance name. Then you can start your logging trip. ILog logger = LogManager.GetLogger(repositoryName, name); return logger; } } }
使用:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApp1 { internal class Program { static void Main(string[] args) { try { var AccountLoger = GetLog4netInstance.GetLogger("DD-子商户号"); var BalanceLoger = GetLog4netInstance.GetLogger("A#.ZZ"); int i = 0; while (i < 10) { string msg = i + ":hello world。"; BalanceLoger.Debug(msg); AccountLoger.Debug(msg); i++; } } catch (Exception ex) { Console.WriteLine("ex!"+ex.Message); } Console.WriteLine("结束!"); Console.ReadKey(); } } }
效果:
END
标签:log4net,name,C#,System,动态创建,Logers,using,appender From: https://www.cnblogs.com/runliuv/p/18229157