zoukankan      html  css  js  c++  java
  • 【C#公共帮助类】 Log4net 帮助类

    首先,我们要在Common类库中引用log4net.dll

    ExtLogImpl.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using log4net.Core;
    
    namespace log4net.Ext
    {
        public class ExtLogImpl : LogImpl, IExtLog
        {
            /// <summary>
            /// The fully qualified name of this declaring type not the type of any subclass.
            /// </summary>
            private readonly static Type ThisDeclaringType = typeof(ExtLogImpl);
            public ExtLogImpl(ILogger logger)
                : base(logger)
            {
            }
            #region IExtLog 成员
    
            public void Info(string clientIP, string clientUser, string requestUri, string action, object message)
            {
                Info(clientIP, clientUser, requestUri, action, message, null);
            }
    
            public void Info(string clientIP, string clientUser, string requestUri, string action, object message, Exception t)
            {
                if (this.IsInfoEnabled)
                {
                    LoggingEvent loggingEvent = new LoggingEvent(ThisDeclaringType, Logger.Repository, Logger.Name, Level.Info, message, t);
                    loggingEvent.Properties["ClientIP"] = clientIP;
                    loggingEvent.Properties["ClientUser"] = clientUser;
                    loggingEvent.Properties["RequestUrl"] = requestUri;
                    loggingEvent.Properties["Action"] = action;
                    Logger.Log(loggingEvent);
                }
            }
    
            public void Warn(string clientIP, string clientUser, string requestUri, string action, object message)
            {
                Warn(clientIP, clientUser, requestUri, action, message, null);
            }
    
            public void Warn(string clientIP, string clientUser, string requestUri, string action, object message, Exception t)
            {
                if (this.IsWarnEnabled)
                {
                    LoggingEvent loggingEvent = new LoggingEvent(ThisDeclaringType, Logger.Repository, Logger.Name, Level.Warn, message, t);
                    loggingEvent.Properties["ClientIP"] = clientIP;
                    loggingEvent.Properties["ClientUser"] = clientUser;
                    loggingEvent.Properties["RequestUrl"] = requestUri;
                    loggingEvent.Properties["Action"] = action;
                    Logger.Log(loggingEvent);
                }
            }
    
            public void Error(string clientIP, string clientUser, string requestUri, string action, object message)
            {
                Error(clientIP, clientUser, requestUri, action, message, null);
            }
    
            public void Error(string clientIP, string clientUser, string requestUri, string action, object message, Exception t)
            {
                if (this.IsErrorEnabled)
                {
                    LoggingEvent loggingEvent = new LoggingEvent(ThisDeclaringType, Logger.Repository, Logger.Name, Level.Error, message, t);
                    loggingEvent.Properties["ClientIP"] = clientIP;
                    loggingEvent.Properties["ClientUser"] = clientUser;
                    loggingEvent.Properties["RequestUrl"] = requestUri;
                    loggingEvent.Properties["Action"] = action;
                    Logger.Log(loggingEvent);
                }
            }
    
            public void Fatal(string clientIP, string clientUser, string requestUri, string action, object message)
            {
                Fatal(clientIP, clientUser, requestUri, action, message, null);
            }
    
            public void Fatal(string clientIP, string clientUser, string requestUri, string action, object message, Exception t)
            {
                if (this.IsFatalEnabled)
                {
                    LoggingEvent loggingEvent = new LoggingEvent(ThisDeclaringType, Logger.Repository, Logger.Name, Level.Fatal, message, t);
                    loggingEvent.Properties["ClientIP"] = clientIP;
                    loggingEvent.Properties["ClientUser"] = clientUser;
                    loggingEvent.Properties["RequestUrl"] = requestUri;
                    loggingEvent.Properties["Action"] = action;
                    Logger.Log(loggingEvent);
                }
            }
            #endregion
        }
    }
    View Code

    ExtLogManager.cs

      1 using log4net.Core;
      2 using System;
      3 using System.Collections.Generic;
      4 using System.Linq;
      5 using System.Reflection;
      6 using System.Text;
      7 
      8 namespace log4net.Ext
      9 {
     10    public class ExtLogManager
     11     {
     12         #region Static Member Variables
     13 
     14         /// <summary>
     15         /// The wrapper map to use to hold the <see cref="WebLogImpl"/> objects
     16         /// </summary>
     17         private static readonly WrapperMap s_wrapperMap = new WrapperMap(new WrapperCreationHandler(WrapperCreationHandler));
     18 
     19         #endregion
     20 
     21         #region Constructor
     22 
     23         /// <summary>
     24         /// Private constructor to prevent object creation
     25         /// </summary>
     26         private ExtLogManager() { }
     27 
     28         #endregion
     29 
     30         #region Type Specific Manager Methods
     31 
     32         /// <summary>
     33         /// Returns the named logger if it exists
     34         /// </summary>
     35         /// <remarks>
     36         /// <para>If the named logger exists (in the default hierarchy) then it
     37         /// returns a reference to the logger, otherwise it returns
     38         /// <c>null</c>.</para>
     39         /// </remarks>
     40         /// <param name="name">The fully qualified logger name to look for</param>
     41         /// <returns>The logger found, or null</returns>
     42         public static IExtLog Exists(string name)
     43         {
     44             return Exists(Assembly.GetCallingAssembly(), name);
     45         }
     46 
     47         /// <summary>
     48         /// Returns the named logger if it exists
     49         /// </summary>
     50         /// <remarks>
     51         /// <para>If the named logger exists (in the specified domain) then it
     52         /// returns a reference to the logger, otherwise it returns
     53         /// <c>null</c>.</para>
     54         /// </remarks>
     55         /// <param name="domain">the domain to lookup in</param>
     56         /// <param name="name">The fully qualified logger name to look for</param>
     57         /// <returns>The logger found, or null</returns>
     58         public static IExtLog Exists(string domain, string name)
     59         {
     60             return WrapLogger(LoggerManager.Exists(domain, name));
     61         }
     62 
     63         /// <summary>
     64         /// Returns the named logger if it exists
     65         /// </summary>
     66         /// <remarks>
     67         /// <para>If the named logger exists (in the specified assembly's domain) then it
     68         /// returns a reference to the logger, otherwise it returns
     69         /// <c>null</c>.</para>
     70         /// </remarks>
     71         /// <param name="assembly">the assembly to use to lookup the domain</param>
     72         /// <param name="name">The fully qualified logger name to look for</param>
     73         /// <returns>The logger found, or null</returns>
     74         public static IExtLog Exists(Assembly assembly, string name)
     75         {
     76             return WrapLogger(LoggerManager.Exists(assembly, name));
     77         }
     78 
     79         /// <summary>
     80         /// Returns all the currently defined loggers in the default domain.
     81         /// </summary>
     82         /// <remarks>
     83         /// <para>The root logger is <b>not</b> included in the returned array.</para>
     84         /// </remarks>
     85         /// <returns>All the defined loggers</returns>
     86         public static IExtLog[] GetCurrentLoggers()
     87         {
     88             return GetCurrentLoggers(Assembly.GetCallingAssembly());
     89         }
     90 
     91         /// <summary>
     92         /// Returns all the currently defined loggers in the specified domain.
     93         /// </summary>
     94         /// <param name="domain">the domain to lookup in</param>
     95         /// <remarks>
     96         /// The root logger is <b>not</b> included in the returned array.
     97         /// </remarks>
     98         /// <returns>All the defined loggers</returns>
     99         public static IExtLog[] GetCurrentLoggers(string domain)
    100         {
    101             return WrapLoggers(LoggerManager.GetCurrentLoggers(domain));
    102         }
    103 
    104         /// <summary>
    105         /// Returns all the currently defined loggers in the specified assembly's domain.
    106         /// </summary>
    107         /// <param name="assembly">the assembly to use to lookup the domain</param>
    108         /// <remarks>
    109         /// The root logger is <b>not</b> included in the returned array.
    110         /// </remarks>
    111         /// <returns>All the defined loggers</returns>
    112         public static IExtLog[] GetCurrentLoggers(Assembly assembly)
    113         {
    114             return WrapLoggers(LoggerManager.GetCurrentLoggers(assembly));
    115         }
    116 
    117         /// <summary>
    118         /// Retrieve or create a named logger.
    119         /// </summary>
    120         /// <remarks>
    121         /// <para>Retrieve a logger named as the <paramref name="name"/>
    122         /// parameter. If the named logger already exists, then the
    123         /// existing instance will be returned. Otherwise, a new instance is
    124         /// created.</para>
    125         /// 
    126         /// <para>By default, loggers do not have a set level but inherit
    127         /// it from the hierarchy. This is one of the central features of
    128         /// log4net.</para>
    129         /// </remarks>
    130         /// <param name="name">The name of the logger to retrieve.</param>
    131         /// <returns>the logger with the name specified</returns>
    132         public static IExtLog GetLogger(string name)
    133         {
    134             return GetLogger(Assembly.GetCallingAssembly(), name);
    135         }
    136 
    137         /// <summary>
    138         /// Retrieve or create a named logger.
    139         /// </summary>
    140         /// <remarks>
    141         /// <para>Retrieve a logger named as the <paramref name="name"/>
    142         /// parameter. If the named logger already exists, then the
    143         /// existing instance will be returned. Otherwise, a new instance is
    144         /// created.</para>
    145         /// 
    146         /// <para>By default, loggers do not have a set level but inherit
    147         /// it from the hierarchy. This is one of the central features of
    148         /// log4net.</para>
    149         /// </remarks>
    150         /// <param name="domain">the domain to lookup in</param>
    151         /// <param name="name">The name of the logger to retrieve.</param>
    152         /// <returns>the logger with the name specified</returns>
    153         public static IExtLog GetLogger(string domain, string name)
    154         {
    155             return WrapLogger(LoggerManager.GetLogger(domain, name));
    156         }
    157 
    158         /// <summary>
    159         /// Retrieve or create a named logger.
    160         /// </summary>
    161         /// <remarks>
    162         /// <para>Retrieve a logger named as the <paramref name="name"/>
    163         /// parameter. If the named logger already exists, then the
    164         /// existing instance will be returned. Otherwise, a new instance is
    165         /// created.</para>
    166         /// 
    167         /// <para>By default, loggers do not have a set level but inherit
    168         /// it from the hierarchy. This is one of the central features of
    169         /// log4net.</para>
    170         /// </remarks>
    171         /// <param name="assembly">the assembly to use to lookup the domain</param>
    172         /// <param name="name">The name of the logger to retrieve.</param>
    173         /// <returns>the logger with the name specified</returns>
    174         public static IExtLog GetLogger(Assembly assembly, string name)
    175         {
    176             return WrapLogger(LoggerManager.GetLogger(assembly, name));
    177         }
    178 
    179         /// <summary>
    180         /// Shorthand for <see cref="LogManager.GetLogger(string)"/>.
    181         /// </summary>
    182         /// <remarks>
    183         /// Get the logger for the fully qualified name of the type specified.
    184         /// </remarks>
    185         /// <param name="type">The full name of <paramref name="type"/> will 
    186         /// be used as the name of the logger to retrieve.</param>
    187         /// <returns>the logger with the name specified</returns>
    188         public static IExtLog GetLogger(Type type)
    189         {
    190             return GetLogger(Assembly.GetCallingAssembly(), type.FullName);
    191         }
    192 
    193         /// <summary>
    194         /// Shorthand for <see cref="LogManager.GetLogger(string)"/>.
    195         /// </summary>
    196         /// <remarks>
    197         /// Get the logger for the fully qualified name of the type specified.
    198         /// </remarks>
    199         /// <param name="domain">the domain to lookup in</param>
    200         /// <param name="type">The full name of <paramref name="type"/> will 
    201         /// be used as the name of the logger to retrieve.</param>
    202         /// <returns>the logger with the name specified</returns>
    203         public static IExtLog GetLogger(string domain, Type type)
    204         {
    205             return WrapLogger(LoggerManager.GetLogger(domain, type));
    206         }
    207 
    208         /// <summary>
    209         /// Shorthand for <see cref="LogManager.GetLogger(string)"/>.
    210         /// </summary>
    211         /// <remarks>
    212         /// Get the logger for the fully qualified name of the type specified.
    213         /// </remarks>
    214         /// <param name="assembly">the assembly to use to lookup the domain</param>
    215         /// <param name="type">The full name of <paramref name="type"/> will 
    216         /// be used as the name of the logger to retrieve.</param>
    217         /// <returns>the logger with the name specified</returns>
    218         public static IExtLog GetLogger(Assembly assembly, Type type)
    219         {
    220             return WrapLogger(LoggerManager.GetLogger(assembly, type));
    221         }
    222 
    223         #endregion
    224 
    225         #region Extension Handlers
    226 
    227         /// <summary>
    228         /// Lookup the wrapper object for the logger specified
    229         /// </summary>
    230         /// <param name="logger">the logger to get the wrapper for</param>
    231         /// <returns>the wrapper for the logger specified</returns>
    232         private static IExtLog WrapLogger(ILogger logger)
    233         {
    234             return (IExtLog)s_wrapperMap.GetWrapper(logger);
    235         }
    236 
    237         /// <summary>
    238         /// Lookup the wrapper objects for the loggers specified
    239         /// </summary>
    240         /// <param name="loggers">the loggers to get the wrappers for</param>
    241         /// <returns>Lookup the wrapper objects for the loggers specified</returns>
    242         private static IExtLog[] WrapLoggers(ILogger[] loggers)
    243         {
    244             IExtLog[] results = new IExtLog[loggers.Length];
    245             for (int i = 0; i < loggers.Length; i++)
    246             {
    247                 results[i] = WrapLogger(loggers[i]);
    248             }
    249             return results;
    250         }
    251 
    252         /// <summary>
    253         /// Method to create the <see cref="ILoggerWrapper"/> objects used by
    254         /// this manager.
    255         /// </summary>
    256         /// <param name="logger">The logger to wrap</param>
    257         /// <returns>The wrapper for the logger specified</returns>
    258         private static ILoggerWrapper WrapperCreationHandler(ILogger logger)
    259         {
    260             return new ExtLogImpl(logger);
    261         }
    262 
    263         #endregion
    264     }
    265 }
    View Code

    IExtLog.cs

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using log4net;
     6 
     7 namespace log4net.Ext
     8 {
     9     public interface IExtLog : ILog
    10     {
    11         void Info(string clientIP, string clientUser, string requestUri, string action, object message);
    12         void Info(string clientIP, string clientUser, string requestUri, string action, object message, Exception t);
    13 
    14         void Warn(string clientIP, string clientUser, string requestUri, string action, object message);
    15         void Warn(string clientIP, string clientUser, string requestUri, string action, object message, Exception t);
    16 
    17         void Error(string clientIP, string clientUser, string requestUri, string action, object message);
    18         void Error(string clientIP, string clientUser, string requestUri, string action, object message, Exception t);
    19 
    20         void Fatal(string clientIP, string clientUser, string requestUri, string action, object message);
    21         void Fatal(string clientIP, string clientUser, string requestUri, string action, object message, Exception t);
    22     }
    23 }
    View Code

    原创文章 转载请尊重劳动成果 http://yuangang.cnblogs.com

  • 相关阅读:
    LOJ 6089 小Y的背包计数问题 —— 前缀和优化DP
    洛谷 P1969 积木大赛 —— 水题
    洛谷 P1965 转圈游戏 —— 快速幂
    洛谷 P1970 花匠 —— DP
    洛谷 P1966 火柴排队 —— 思路
    51Nod 1450 闯关游戏 —— 期望DP
    洛谷 P2312 & bzoj 3751 解方程 —— 取模
    洛谷 P1351 联合权值 —— 树形DP
    NOIP2007 树网的核
    平面最近点对(加强版)
  • 原文地址:https://www.cnblogs.com/yuangang/p/5498214.html
Copyright © 2011-2022 走看看