| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- using log4net;
- using log4net.Core;
- using log4net.Layout;
- using log4net.Layout.Pattern;
- namespace IwbZero.ToolCommon.LogHelpers
- {
- /// <summary>
- /// Log4Net扩展(扩展一个Engine的LOG LEVEL,输出Engine运行日志)
- /// </summary>
- public static class Log4NetExtend
- {
- public static readonly log4net.Core.Level EngineLevel = new Level(Level.Finer.Value, "ENGINE");
- private static void AddEngineLevel(ILog log)
- {
- if (!log.Logger.Repository.LevelMap.AllLevels.Contains(EngineLevel))
- {
- log.Logger.Repository.LevelMap.Add(EngineLevel);
- }
- }
- public static void Engine(this ILog log, object message)
- {
- AddEngineLevel(log);
- log.Logger.Log(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType,
- EngineLevel, message, null);
- }
- public static void EngineFormat(this ILog log, string message, params object[] args)
- {
- AddEngineLevel(log);
- string formattedMessage = string.Format(message, args);
- log.Logger.Log(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType,
- EngineLevel, formattedMessage, null);
- }
- }
- /// <summary>
- /// 输出当前进程名转换器
- /// </summary>
- public class ProcessPatternConvert : PatternLayoutConverter
- {
- private static readonly string ProcessName = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
- protected override void Convert(System.IO.TextWriter writer, log4net.Core.LoggingEvent loggingEvent)
- {
- writer.Write(ProcessName);//输出当前进程名
- }
- }
- public class CustomPatternLayout : PatternLayout
- {
- public CustomPatternLayout()
- {
- AddConverter("Engine", typeof(ProcessPatternConvert));
- }
- }
- }
|