【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
    }
}

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 }

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 }

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

时间: 2024-11-01 12:15:22

【C#公共帮助类】 Log4net 帮助类的相关文章

公共的数据库访问访问类 SqlHelper.cs

/// <summary> /// 类说明:公共的数据库访问访问类 /// </summary> using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.SqlClient; using System.Collections; namespace DotNet.Utilities { /// <summary> /// 

【C++】C++自学旅程(7):类类型初识——类的定义

新年第一博,新年快乐! 一直不明白面对对象的“对象”是什么意思,想象不出:在学OC时出现了奇怪的语法,让程序“自己”运行:EDA的沙龙中提到了类类型,但不知道这是个什么鬼.终于这些问题的谜底要被我自己自学揭开了,哈哈哈!类类型我来啦! 类(Class)是一种自定义数据类型,是具有相同属性和行为的一组对象的集合,它是面向对象中最为重要的概念之一.类类型中有变量来存储属性和数据,叫做成员变量或数据成员,还有操作这些数据的函数,叫做成员函数. 类类型的作用: 实现封装 数据隐藏 继承和派生多态 ……

类,抽象基类,接口类三者间的区别与联系(C++)

联系很明显,三个都是‘类’,如果读者对类的概念不清楚,可以参照wid的博文http://www.cnblogs.com/mr-wid/archive/2013/02/18/2916309.html. 下面着重解释一下区别,但此文仅是个人理解,如果觉得我说的不对的地方,还请赐教. (1)结构上的区别: 普通类:数据+方法+实现 抽象类:数据+方法(一定包含虚方法n>=1)+部分方法的实现 接口类:方法(纯虚方法) (2)概念上的区别: 普通的类和另外两个的区别很明显,普通类就是猫狗之类的,而抽象类

java 类的匿名类和封装

/* 匿名对象:没有引用类型变量指向的对象称作为匿名对象. 需求: 使用 java类描述一个学生类. 匿名对象要注意的事项: 1. 我们一般不会给匿名对象赋予属性值,因为永远无法获取到. 2. 两个匿名对象永远都不可能是同一个对象. 匿名对象好处:简化书写.可以尽快释放对象的堆内存空间 匿名对象的应用场景: 1. 如果一个对象需要调用一个方法一次的时候,而调用完这个方法之后,该对象就不再使用了,这时候可以使用 匿名对象. 2. 可以作为实参调用一个函数. */ //学生类 class Stude

黑马程序员--Java基础学习笔记【Object类、String类】

------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- Object类 在Java类继承结构中,java.lang.Object类位于顶端. 所有类都直接或间接继承Object类. (接口不能继承Object类,接口中隐含定义了Object类的所有公共方法,但是全抽象) Object类型的引用变量可以指向任何类型对象. Object类成员方法 public String toString() { return getClass().getName(

c++——派生类和基类转换

派生类经常(但不总是)覆盖它继承的虚函数.如果派生类没有覆盖其基类中的某个虚函数,则该虚函数的行为类似于其他的普通成员,派生类会直接继承其在基类中的版本. c++11允许派生类显式地注明它使用某个成员函数覆盖了它继承的虚函数.具体做法是在形参列表后面.或者在const成员函数的const关键字后面.或者在引用成员函数的引用限定符后面添加一个关键字override. 派生类也必须使用基类的构造函数来初始化它的基类部分.(除非基类没有显式定义构造函数,这样在派生类的构造函数中可以不显式调用基类的构造

黑马程序员——Java基础---反射Class类、Constructor类、Field类

------<a href="http://www.itheima.com" target="blank">Java培训.Android培训.iOS培训..Net培训</a>.期待与您交流! ------- 反射的应用场景 一.概述 反射技术: Java反射机制是在运行状态中,对于任意一个类,都能够知道这个类中的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取的信息以及动态调用对象的方法的功能称为java语言的

基类与派生类的关系

任何一个类都可以派生出一个新类,派生类也可以再派生出新类,因此,基类和派生类是相对而言的. 基类与派生类之间的关系可以有如下几种描述: 1. 派生类是基类的具体化 类的层次通常反映了客观世界中某种真实的模型.在这种情况下,不难看出:基类是对若干个派生类的抽象,而派生类是基类的具体化.基类抽取了它的派生类的公共特征,而派生类通过增加行为将抽象类变为某种有用的类型. 2. 派生类是基类定义的延续 先定义一个抽象基类,该基类中有些操作并未实现.然后定义非抽象的派生类,实现抽象基类中定义的操作.例如,虚

C++ Primer 学习笔记_66_面向对象编程 -定义基类跟派生类[续]

面向对象编程 --定义基类和派生类[续] 四.virtual与其他成员函数 C++中的函数调用默认不使用动态绑定.要触发动态绑定,必须满足两个条件: 1)只有指定为虚函数的成员函数才能进行动态绑定,成员函数默认为非虚函数,非虚函数不进行动态绑定. 2)必须通过基类类型的引用或指针进行函数调用. 1.从派生类到基类的转换 因为每个派生类对象都包含基类部分,所以可以将基类类型的引用绑定到派生类对象的基类部分可以用指向基类的指针指向派生类对象: void print_total(const Item_

C++——类继承以及类初始化顺序

对于类以及类继承, 几个主要的问题:1) 继承方式: public/protected/private继承. 这是c++搞的, 实际上继承方式是一种允许子类控制的思想. 子类通过public继承, 可以把基类真实还原, 而private继承则完全把基类屏蔽掉. 这种屏蔽是相对于对象层而言的, 就是说子类的对象完全看不到基类的方法, 如果继承方式是private的话, 即使方法在基类中为public的方法. 但继承方式并不影响垂直方向的访问特性, 那就是子类的函数对基类的成员访问是不受继承方式的影