| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501 |
- 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());
- }
- }
- /// <summary>
- /// 通过反射获取传入的日志对象的某个属性的值
- /// </summary>
- /// <param name="property"></param>
- /// <param name="loggingEvent"></param>
- /// <returns></returns>
- 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
- {
- /// <summary>
- /// The fully qualified name of this declaring type not the type of any subclass.
- /// </summary>
- 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
- /// <summary>
- /// The wrapper map to use to hold the
- ///<see>
- ///<cref>EventIDLogImpl</cref>
- ///</see>
- ///objects
- /// </summary>
- private static readonly WrapperMap SWrapperMap = new WrapperMap(WrapperCreationHandler);
- #endregion
- #region Constructor
- /// <summary>
- /// Private constructor to prevent object creation
- /// </summary>
- private MyLogManager() { }
- #endregion
- #region Type Specific Manager Methods
- /// <summary>
- /// Returns the named logger if it exists
- /// </summary>
- /// <remarks>
- /// <para>If the named logger exists (in the default hierarchy) then it
- /// returns a reference to the logger, otherwise it returns
- /// <c>null</c>.</para>
- /// </remarks>
- /// <param name="name">The fully qualified logger name to look for</param>
- /// <returns>The logger found, or null</returns>
- public static IMyLog Exists(string name)
- {
- return Exists(Assembly.GetCallingAssembly(), name);
- }
- /// <summary>
- /// Returns the named logger if it exists
- /// </summary>
- /// <remarks>
- /// <para>If the named logger exists (in the specified domain) then it
- /// returns a reference to the logger, otherwise it returns
- /// <c>null</c>.</para>
- /// </remarks>
- /// <param name="domain">the domain to lookup in</param>
- /// <param name="name">The fully qualified logger name to look for</param>
- /// <returns>The logger found, or null</returns>
- public static IMyLog Exists(string domain, string name)
- {
- return WrapLogger(LoggerManager.Exists(domain, name));
- }
- /// <summary>
- /// Returns the named logger if it exists
- /// </summary>
- /// <remarks>
- /// <para>If the named logger exists (in the specified assembly's domain) then it
- /// returns a reference to the logger, otherwise it returns
- /// <c>null</c>.</para>
- /// </remarks>
- /// <param name="assembly">the assembly to use to lookup the domain</param>
- /// <param name="name">The fully qualified logger name to look for</param>
- /// <returns>The logger found, or null</returns>
- public static IMyLog Exists(Assembly assembly, string name)
- {
- return WrapLogger(LoggerManager.Exists(assembly, name));
- }
- /// <summary>
- /// Returns all the currently defined loggers in the default domain.
- /// </summary>
- /// <remarks>
- /// <para>The root logger is <b>not</b> included in the returned array.</para>
- /// </remarks>
- /// <returns>All the defined loggers</returns>
- public static IMyLog[] GetCurrentLoggers()
- {
- return GetCurrentLoggers(Assembly.GetCallingAssembly());
- }
- /// <summary>
- /// Returns all the currently defined loggers in the specified domain.
- /// </summary>
- /// <param name="domain">the domain to lookup in</param>
- /// <remarks>
- /// The root logger is <b>not</b> included in the returned array.
- /// </remarks>
- /// <returns>All the defined loggers</returns>
- public static IMyLog[] GetCurrentLoggers(string domain)
- {
- return WrapLoggers(LoggerManager.GetCurrentLoggers(domain));
- }
- /// <summary>
- /// Returns all the currently defined loggers in the specified assembly's domain.
- /// </summary>
- /// <param name="assembly">the assembly to use to lookup the domain</param>
- /// <remarks>
- /// The root logger is <b>not</b> included in the returned array.
- /// </remarks>
- /// <returns>All the defined loggers</returns>
- public static IMyLog[] GetCurrentLoggers(Assembly assembly)
- {
- return WrapLoggers(LoggerManager.GetCurrentLoggers(assembly));
- }
- /// <summary>
- /// Retrieve or create a named logger.
- /// </summary>
- /// <remarks>
- /// <para>Retrieve a logger named as the <paramref name="name"/>
- /// parameter. If the named logger already exists, then the
- /// existing instance will be returned. Otherwise, a new instance is
- /// created.</para>
- ///
- /// <para>By default, loggers do not have a set level but inherit
- /// it from the hierarchy. This is one of the central features of
- /// log4net.</para>
- /// </remarks>
- /// <param name="name">The name of the logger to retrieve.</param>
- /// <returns>the logger with the name specified</returns>
- public static IMyLog GetLogger(string name)
- {
- return GetLogger(Assembly.GetCallingAssembly(), name);
- }
- /// <summary>
- /// Retrieve or create a named logger.
- /// </summary>
- /// <remarks>
- /// <para>Retrieve a logger named as the <paramref name="name"/>
- /// parameter. If the named logger already exists, then the
- /// existing instance will be returned. Otherwise, a new instance is
- /// created.</para>
- ///
- /// <para>By default, loggers do not have a set level but inherit
- /// it from the hierarchy. This is one of the central features of
- /// log4net.</para>
- /// </remarks>
- /// <param name="domain">the domain to lookup in</param>
- /// <param name="name">The name of the logger to retrieve.</param>
- /// <returns>the logger with the name specified</returns>
- public static IMyLog GetLogger(string domain, string name)
- {
- return WrapLogger(LoggerManager.GetLogger(domain, name));
- }
-
- /// <summary>
- /// Retrieve or create a named logger.
- /// </summary>
- /// <remarks>
- /// <para>Retrieve a logger named as the <paramref name="name"/>
- /// parameter. If the named logger already exists, then the
- /// existing instance will be returned. Otherwise, a new instance is
- /// created.</para>
- ///
- /// <para>By default, loggers do not have a set level but inherit
- /// it from the hierarchy. This is one of the central features of
- /// log4net.</para>
- /// </remarks>
- /// <param name="assembly">the assembly to use to lookup the domain</param>
- /// <param name="name">The name of the logger to retrieve.</param>
- /// <returns>the logger with the name specified</returns>
- public static IMyLog GetLogger(Assembly assembly, string name)
- {
- return WrapLogger(LoggerManager.GetLogger(assembly, name));
- }
-
- /// <summary>
- /// Shorthand for <see cref="LogManager.GetLogger(string)"/>.
- /// </summary>
- /// <remarks>
- /// Get the logger for the fully qualified name of the type specified.
- /// </remarks>
- /// <param name="type">The full name of <paramref name="type"/> will
- /// be used as the name of the logger to retrieve.</param>
- /// <returns>the logger with the name specified</returns>
- public static IMyLog GetLogger(Type type)
- {
- return GetLogger(Assembly.GetCallingAssembly(), type.FullName);
- }
- /// <summary>
- /// Shorthand for <see cref="LogManager.GetLogger(string)"/>.
- /// </summary>
- /// <remarks>
- /// Get the logger for the fully qualified name of the type specified.
- /// </remarks>
- /// <param name="domain">the domain to lookup in</param>
- /// <param name="type">The full name of <paramref name="type"/> will
- /// be used as the name of the logger to retrieve.</param>
- /// <returns>the logger with the name specified</returns>
- public static IMyLog GetLogger(string domain, Type type)
- {
- return WrapLogger(LoggerManager.GetLogger(domain, type));
- }
- /// <summary>
- /// Shorthand for <see cref="LogManager.GetLogger(string)"/>.
- /// </summary>
- /// <remarks>
- /// Get the logger for the fully qualified name of the type specified.
- /// </remarks>
- /// <param name="assembly">the assembly to use to lookup the domain</param>
- /// <param name="type">The full name of <paramref name="type"/> will
- /// be used as the name of the logger to retrieve.</param>
- /// <returns>the logger with the name specified</returns>
- public static IMyLog GetLogger(Assembly assembly, Type type)
- {
- return WrapLogger(LoggerManager.GetLogger(assembly, type));
- }
-
- #endregion
-
- #region Extension Handlers
-
- /// <summary>
- /// Lookup the wrapper object for the logger specified
- /// </summary>
- /// <param name="logger">the logger to get the wrapper for</param>
- /// <returns>the wrapper for the logger specified</returns>
- private static IMyLog WrapLogger(ILogger logger)
- {
- return (IMyLog)SWrapperMap.GetWrapper(logger);
- }
- /// <summary>
- /// Lookup the wrapper objects for the loggers specified
- /// </summary>
- /// <param name="loggers">the loggers to get the wrappers for</param>
- /// <returns>Lookup the wrapper objects for the loggers specified</returns>
- 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;
- }
- /// <summary>
- /// Method to create the <see cref="ILoggerWrapper"/> objects used by
- /// this manager.
- /// </summary>
- /// <param name="logger">The logger to wrap</param>
- /// <returns>The wrapper for the logger specified</returns>
- 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);
- }
- }
|