using System; using System.Reflection; using log4net; using log4net.Core; using log4net.Layout; using log4net.Layout.Pattern; [assembly: log4net.Config.XmlConfigurator(Watch = true)] namespace CommonTool { public class LogHelper { private static readonly ILog Log = LogManager.GetLogger("log4net"); public static void Info(string message) { Log.Info(message); } public static void Debug(string message) { Log.Debug(message); } public static void Error(string message) { Log.Error(message); } public static void Fatal(string message) { Log.Fatal(message); } public static void Warn(string message) { Log.Warn(message); } } public class ReflectionLayout : PatternLayout { public ReflectionLayout() { AddConverter("property", typeof(ReflectionPatternConverter)); } } public class ReflectionPatternConverter : PatternLayoutConverter { protected override void Convert(System.IO.TextWriter writer,LoggingEvent loggingEvent) { if (Option != null) { // 写入指定键的值 WriteObject(writer,loggingEvent.Repository,LookupProperty(Option,loggingEvent)); } else { // 写入所有关键值对 WriteDictionary(writer,loggingEvent.Repository,loggingEvent.GetProperties()); } } /// /// 通过反射获取传入的日志对象的某个属性的值 /// /// /// /// private object LookupProperty(string property,LoggingEvent loggingEvent) { object propertyValue = string.Empty; PropertyInfo propertyInfo = loggingEvent.MessageObject.GetType().GetProperty(property); if (propertyInfo != null) { propertyValue = propertyInfo.GetValue(loggingEvent.MessageObject, null); } return propertyValue; } } public class MyLogImpl : LogImpl, IMyLog { /// /// The fully qualified name of this declaring type not the type of any subclass. /// private static readonly Type ThisDeclaringType = typeof(MyLogImpl); public MyLogImpl(ILogger logger) : base(logger){} #region Implementation of IMyLog public void Debug(int operatorId, string operand, int actionType, object message, string ip, string browser, string machineName) { Debug(operatorId, operand, actionType, message, ip, browser, machineName, null); } public void Debug(int operatorId, string operand, int actionType, object message, string ip, string browser, string machineName, Exception t) { if (IsDebugEnabled) { LoggingEvent loggingEvent = new LoggingEvent(ThisDeclaringType, Logger.Repository, Logger.Name, Level.Info, message, t); loggingEvent.Properties["Operator"] = operatorId; loggingEvent.Properties["Operand"] = operand; loggingEvent.Properties["ActionType"] = actionType; loggingEvent.Properties["IP"] = ip; loggingEvent.Properties["Browser"] = browser; loggingEvent.Properties["MachineName"] = machineName; Logger.Log(loggingEvent); } } public void Info(int operatorId, string operand, int actionType, object message, string ip, string browser, string machineName) { Info(operatorId, operand, actionType, message, ip, browser, machineName, null); } public void Info(int operatorId, string operand, int actionType, object message, string ip, string browser, string machineName, Exception t) { if (IsInfoEnabled) { LoggingEvent loggingEvent = new LoggingEvent(ThisDeclaringType, Logger.Repository, Logger.Name, Level.Info, message, t); loggingEvent.Properties["Operator"] = operatorId; loggingEvent.Properties["Operand"] = operand; loggingEvent.Properties["ActionType"] = actionType; loggingEvent.Properties["IP"] = ip; loggingEvent.Properties["Browser"] = browser; loggingEvent.Properties["MachineName"] = machineName; Logger.Log(loggingEvent); } } public void Warn(int operatorId, string operand, int actionType, object message, string ip, string browser, string machineName) { Warn(operatorId, operand, actionType, message, ip, browser, machineName, null); } public void Warn(int operatorId, string operand, int actionType, object message, string ip, string browser, string machineName, Exception t) { if (IsWarnEnabled) { LoggingEvent loggingEvent = new LoggingEvent(ThisDeclaringType, Logger.Repository, Logger.Name, Level.Info, message, t); loggingEvent.Properties["Operator"] = operatorId; loggingEvent.Properties["Operand"] = operand; loggingEvent.Properties["ActionType"] = actionType; loggingEvent.Properties["IP"] = ip; loggingEvent.Properties["Browser"] = browser; loggingEvent.Properties["MachineName"] = machineName; Logger.Log(loggingEvent); } } public void Error(int operatorId, string operand, int actionType, object message, string ip, string browser, string machineName) { Error(operatorId, operand, actionType, message, ip, browser, machineName, null); } public void Error(int operatorId, string operand, int actionType, object message, string ip, string browser, string machineName, Exception t) { if (IsWarnEnabled) { LoggingEvent loggingEvent = new LoggingEvent(ThisDeclaringType, Logger.Repository, Logger.Name, Level.Info, message, t); loggingEvent.Properties["Operator"] = operatorId; loggingEvent.Properties["Operand"] = operand; loggingEvent.Properties["ActionType"] = actionType; loggingEvent.Properties["IP"] = ip; loggingEvent.Properties["Browser"] = browser; loggingEvent.Properties["MachineName"] = machineName; Logger.Log(loggingEvent); } } public void Fatal(int operatorId, string operand, int actionType, object message, string ip, string browser, string machineName) { Fatal(operatorId, operand, actionType, message, ip, browser, machineName, null); } public void Fatal(int operatorId, string operand, int actionType, object message, string ip, string browser, string machineName, Exception t) { if (IsWarnEnabled) { LoggingEvent loggingEvent = new LoggingEvent(ThisDeclaringType, Logger.Repository, Logger.Name, Level.Info, message, t); loggingEvent.Properties["Operator"] = operatorId; loggingEvent.Properties["Operand"] = operand; loggingEvent.Properties["ActionType"] = actionType; loggingEvent.Properties["IP"] = ip; loggingEvent.Properties["Browser"] = browser; loggingEvent.Properties["MachineName"] = machineName; Logger.Log(loggingEvent); } } #endregion Implementation of IMyLog } public class MyLogManager { #region Static Member Variables /// /// The wrapper map to use to hold the /// ///EventIDLogImpl /// ///objects /// private static readonly WrapperMap SWrapperMap = new WrapperMap(WrapperCreationHandler); #endregion #region Constructor /// /// Private constructor to prevent object creation /// private MyLogManager() { } #endregion #region Type Specific Manager Methods /// /// Returns the named logger if it exists /// /// /// If the named logger exists (in the default hierarchy) then it /// returns a reference to the logger, otherwise it returns /// null. /// /// The fully qualified logger name to look for /// The logger found, or null public static IMyLog Exists(string name) { return Exists(Assembly.GetCallingAssembly(), name); } /// /// Returns the named logger if it exists /// /// /// If the named logger exists (in the specified domain) then it /// returns a reference to the logger, otherwise it returns /// null. /// /// the domain to lookup in /// The fully qualified logger name to look for /// The logger found, or null public static IMyLog Exists(string domain, string name) { return WrapLogger(LoggerManager.Exists(domain, name)); } /// /// Returns the named logger if it exists /// /// /// If the named logger exists (in the specified assembly's domain) then it /// returns a reference to the logger, otherwise it returns /// null. /// /// the assembly to use to lookup the domain /// The fully qualified logger name to look for /// The logger found, or null public static IMyLog Exists(Assembly assembly, string name) { return WrapLogger(LoggerManager.Exists(assembly, name)); } /// /// Returns all the currently defined loggers in the default domain. /// /// /// The root logger is not included in the returned array. /// /// All the defined loggers public static IMyLog[] GetCurrentLoggers() { return GetCurrentLoggers(Assembly.GetCallingAssembly()); } /// /// Returns all the currently defined loggers in the specified domain. /// /// the domain to lookup in /// /// The root logger is not included in the returned array. /// /// All the defined loggers public static IMyLog[] GetCurrentLoggers(string domain) { return WrapLoggers(LoggerManager.GetCurrentLoggers(domain)); } /// /// Returns all the currently defined loggers in the specified assembly's domain. /// /// the assembly to use to lookup the domain /// /// The root logger is not included in the returned array. /// /// All the defined loggers public static IMyLog[] GetCurrentLoggers(Assembly assembly) { return WrapLoggers(LoggerManager.GetCurrentLoggers(assembly)); } /// /// Retrieve or create a named logger. /// /// /// Retrieve a logger named as the /// parameter. If the named logger already exists, then the /// existing instance will be returned. Otherwise, a new instance is /// created. /// /// By default, loggers do not have a set level but inherit /// it from the hierarchy. This is one of the central features of /// log4net. /// /// The name of the logger to retrieve. /// the logger with the name specified public static IMyLog GetLogger(string name) { return GetLogger(Assembly.GetCallingAssembly(), name); } /// /// Retrieve or create a named logger. /// /// /// Retrieve a logger named as the /// parameter. If the named logger already exists, then the /// existing instance will be returned. Otherwise, a new instance is /// created. /// /// By default, loggers do not have a set level but inherit /// it from the hierarchy. This is one of the central features of /// log4net. /// /// the domain to lookup in /// The name of the logger to retrieve. /// the logger with the name specified public static IMyLog GetLogger(string domain, string name) { return WrapLogger(LoggerManager.GetLogger(domain, name)); } /// /// Retrieve or create a named logger. /// /// /// Retrieve a logger named as the /// parameter. If the named logger already exists, then the /// existing instance will be returned. Otherwise, a new instance is /// created. /// /// By default, loggers do not have a set level but inherit /// it from the hierarchy. This is one of the central features of /// log4net. /// /// the assembly to use to lookup the domain /// The name of the logger to retrieve. /// the logger with the name specified public static IMyLog GetLogger(Assembly assembly, string name) { return WrapLogger(LoggerManager.GetLogger(assembly, name)); } /// /// Shorthand for . /// /// /// Get the logger for the fully qualified name of the type specified. /// /// The full name of will /// be used as the name of the logger to retrieve. /// the logger with the name specified public static IMyLog GetLogger(Type type) { return GetLogger(Assembly.GetCallingAssembly(), type.FullName); } /// /// Shorthand for . /// /// /// Get the logger for the fully qualified name of the type specified. /// /// the domain to lookup in /// The full name of will /// be used as the name of the logger to retrieve. /// the logger with the name specified public static IMyLog GetLogger(string domain, Type type) { return WrapLogger(LoggerManager.GetLogger(domain, type)); } /// /// Shorthand for . /// /// /// Get the logger for the fully qualified name of the type specified. /// /// the assembly to use to lookup the domain /// The full name of will /// be used as the name of the logger to retrieve. /// the logger with the name specified public static IMyLog GetLogger(Assembly assembly, Type type) { return WrapLogger(LoggerManager.GetLogger(assembly, type)); } #endregion #region Extension Handlers /// /// Lookup the wrapper object for the logger specified /// /// the logger to get the wrapper for /// the wrapper for the logger specified private static IMyLog WrapLogger(ILogger logger) { return (IMyLog)SWrapperMap.GetWrapper(logger); } /// /// Lookup the wrapper objects for the loggers specified /// /// the loggers to get the wrappers for /// Lookup the wrapper objects for the loggers specified private static IMyLog[] WrapLoggers(ILogger[] loggers) { IMyLog[] results = new IMyLog[loggers.Length]; for (int i = 0; i < loggers.Length; i++) { results[i] = WrapLogger(loggers[i]); } return results; } /// /// Method to create the objects used by /// this manager. /// /// The logger to wrap /// The wrapper for the logger specified private static ILoggerWrapper WrapperCreationHandler(ILogger logger) { return new MyLogImpl(logger); } #endregion } public interface IMyLog : ILog { void Debug(int operatorId, string operand, int actionType, object message, string ip, string browser, string machineName); void Debug(int operatorId, string operand, int actionType, object message,string ip, string browser, string machineName, Exception t); void Info(int operatorId, string operand, int actionType, object message, string ip, string browser, string machineName); void Info(int operatorId, string operand, int actionType, object message, string ip, string browser, string machineName, Exception t); void Warn(int operatorId, string operand, int actionType, object message, string ip, string browser, string machineName); void Warn(int operatorId, string operand, int actionType, object message, string ip, string browser, string machineName, Exception t); void Error(int operatorId, string operand, int actionType, object message, string ip, string browser, string machineName); void Error(int operatorId, string operand, int actionType, object message, string ip, string browser, string machineName, Exception t); void Fatal(int operatorId, string operand, int actionType, object message, string ip, string browser, string machineName); void Fatal(int operatorId, string operand, int actionType, object message, string ip, string browser, string machineName, Exception t); } }