【C#公共帮助类】 Log4net 帮助类
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了【C#公共帮助类】 Log4net 帮助类,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含14440字,纯文字阅读大概需要21分钟。
内容图文
首先,我们要在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 成员 publicvoid Info(string clientIP, string clientUser, string requestUri, string action, object message) { Info(clientIP, clientUser, requestUri, action, message, null); } publicvoid 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); } } publicvoid Warn(string clientIP, string clientUser, string requestUri, string action, object message) { Warn(clientIP, clientUser, requestUri, action, message, null); } publicvoid 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); } } publicvoid Error(string clientIP, string clientUser, string requestUri, string action, object message) { Error(clientIP, clientUser, requestUri, action, message, null); } publicvoid 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); } } publicvoid Fatal(string clientIP, string clientUser, string requestUri, string action, object message) { Fatal(clientIP, clientUser, requestUri, action, message, null); } publicvoid 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> 17privatestaticreadonly 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> 26private 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> 42publicstatic IExtLog Exists(string name) 43 { 44return 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> 58publicstatic IExtLog Exists(string domain, string name) 59 { 60return 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> 74publicstatic IExtLog Exists(Assembly assembly, string name) 75 { 76return 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> 86publicstatic IExtLog[] GetCurrentLoggers() 87 { 88return 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> 99publicstatic IExtLog[] GetCurrentLoggers(string domain) 100 { 101return WrapLoggers(LoggerManager.GetCurrentLoggers(domain)); 102 } 103104///<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>112publicstatic IExtLog[] GetCurrentLoggers(Assembly assembly) 113 { 114return WrapLoggers(LoggerManager.GetCurrentLoggers(assembly)); 115 } 116117///<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>132publicstatic IExtLog GetLogger(string name) 133 { 134return GetLogger(Assembly.GetCallingAssembly(), name); 135 } 136137///<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>153publicstatic IExtLog GetLogger(string domain, string name) 154 { 155return WrapLogger(LoggerManager.GetLogger(domain, name)); 156 } 157158///<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>174publicstatic IExtLog GetLogger(Assembly assembly, string name) 175 { 176return WrapLogger(LoggerManager.GetLogger(assembly, name)); 177 } 178179///<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>188publicstatic IExtLog GetLogger(Type type) 189 { 190return GetLogger(Assembly.GetCallingAssembly(), type.FullName); 191 } 192193///<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>203publicstatic IExtLog GetLogger(string domain, Type type) 204 { 205return WrapLogger(LoggerManager.GetLogger(domain, type)); 206 } 207208///<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>218publicstatic IExtLog GetLogger(Assembly assembly, Type type) 219 { 220return WrapLogger(LoggerManager.GetLogger(assembly, type)); 221 } 222223#endregion224225#region Extension Handlers 226227///<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>232privatestatic IExtLog WrapLogger(ILogger logger) 233 { 234return (IExtLog)s_wrapperMap.GetWrapper(logger); 235 } 236237///<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>242privatestatic IExtLog[] WrapLoggers(ILogger[] loggers) 243 { 244 IExtLog[] results = new IExtLog[loggers.Length]; 245for (int i = 0; i < loggers.Length; i++) 246 { 247 results[i] = WrapLogger(loggers[i]); 248 } 249return results; 250 } 251252///<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>258privatestatic ILoggerWrapper WrapperCreationHandler(ILogger logger) 259 { 260returnnew ExtLogImpl(logger); 261 } 262263#endregion264 } 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); 12void Info(string clientIP, string clientUser, string requestUri, string action, object message, Exception t); 1314void Warn(string clientIP, string clientUser, string requestUri, string action, object message); 15void Warn(string clientIP, string clientUser, string requestUri, string action, object message, Exception t); 1617void Error(string clientIP, string clientUser, string requestUri, string action, object message); 18void Error(string clientIP, string clientUser, string requestUri, string action, object message, Exception t); 1920void Fatal(string clientIP, string clientUser, string requestUri, string action, object message); 21void Fatal(string clientIP, string clientUser, string requestUri, string action, object message, Exception t); 22 } 23 }
原创文章 转载请尊重劳动成果 http://yuangang.cnblogs.com
原文:http://www.cnblogs.com/yuangang/p/5498214.html
内容总结
以上是互联网集市为您收集整理的【C#公共帮助类】 Log4net 帮助类全部内容,希望文章能够帮你解决【C#公共帮助类】 Log4net 帮助类所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。