1. 程式人生 > >【C#公共幫助類】 Log4net 幫助類

【C#公共幫助類】 Log4net 幫助類

  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 }

相關推薦

C#公共幫助 Log4net 幫助

1 using log4net.Core; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Reflection; 6 using System

C#多執行緒1.Thread的使用及注意要點

Thread隨便講講   因為在C#中,Thread類在我們的新業務上並不常用了(因為建立一個新執行緒要比直接從執行緒池拿執行緒更加耗費資源),並且在.NET4.0後新增了Task類即Async與await關鍵字,使得我們基本不再用Thread了,不過在學習多執行緒前,有必要先了解下Thread類,這裡就先隨

C#公共幫助 WebHelper幫助

1 using System; 2 using System.Data; 3 using System.Configuration; 4 using System.Web; 5 using System.Web.Security; 6 using System.Web.U

C#公共幫助給大家分享一些加密演算法 (DES、HashCode、RSA、AES等)

AES       高階加密標準(英語:Advanced Encryption Standard,縮寫:AES),在密碼學中又稱Rijndael加密法,是美國聯邦政府採用的一種區塊加密標準。這個標準用來替代原先的DES,已經被多方分析且廣為全世界所使用。AES先進加密演算法是一向被認為牢不可破的加密演算法,

C#公共幫助分頁邏輯處理

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Common 7 { 8 ///

C#公共幫助JsonHelper 操作幫助, 以後再也不用滿地找Json了,拿來直接用

using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Linq; using System.Web.Script.Serialization; usi

C#公共幫助列舉獨特

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.ComponentModel; 6 7 nam

C#公共幫助 ToolsHelper幫助

1 using System; 2 using System.Text; 3 using System.Text.RegularExpressions; 4 using System.Collections.Generic; 5 using System.Reflection;

C#復習總結匿名型由來

數據類型 over 無效 訪問性 屬性。 知乎 私有 不能 默認構造函數 1 屬性 這得先從屬性開始說,為什麽外部代碼訪問對象內部的數據用屬性而不是直接訪問呢,這樣豈不是更方便一些,但是事實證明直接訪問是不安全的。那麽,Anders Hejlsberg(安德斯&mid

C++/數據結構單鏈表的基本操作

clear default als troy pub 插入 else fonts pac #pragma once #ifndef _CLIST_H_ #define _CLIST_H_ #include <iostream> #include <

HTTP模擬工具C#/Winform源碼、Json綁定TreeView控件、使用了MetroModernUI、RestSharp、Dapper.Net、Newtonsoft.Json、SmartThreadPool這幾個主要開源框架

type form num -m 請求 resource dap bool dev HTTP模擬工具 開發語言:C#/Winform開發工具:Visual Studio 2017數據庫: SQLite使用框架:界面-MetroModernUI

Spring(三)--AOP面向切面編程、通知型及使用、切入點表達式

1.3 一聲 重復 信息 術語 臃腫 lib pac fin 1.概念:Aspect Oriented Programming 面向切面編程 在方法的前後添加方法 2.作用:本質上來說是一種簡化代碼的方式 繼承機制 封裝方法 動態代理

C++探索之旅第二部分第一課:面向對象初探,string的驚天內幕

信息技術 false cli 方法 復雜 weixin include 命令 就是 內容簡單介紹 1、第二部分第一課:面向對象初探。string的驚天內幕 2

C#學習之路001.基本操作

arp main cti 字符 thread 程序 AI 報錯 float 001【HelloWorld】分析代碼塊 //這裏是註釋 下面是引入命名空間 using System; using System.Collections.Generic; using Syst

將文件拖曳到窗體上, 並獲取其完整路徑 C++ Builder下實現

pat ext stc fff led CP tle 聲明 net 1. 在窗體的頭文件.h裏聲明處理函數和消息映射, 如: [cpp] view plain copy class TForm1 : public TForm {

C#復習總結細說委托

protected 希望 百度百科 內存 sting lin baidu 調用約定 multicast 1 前言 前幾天看到博客園一個前輩寫了一篇文章用“五分鐘重溫委托,匿名方法,Lambda,泛型委托,表達式樹”,文章寫的非常好,推薦閱讀一下,正

C#復習總結細說匿名方法

target [] targe left 沒有 如果 連接 program ont 1 前言 本系列會將【委托】 【匿名方法】【Lambda表達式】 【泛型委托】 【表達式樹】 【事件】等基礎知識總結一下。(本人小白一枚,有錯誤的地方希望大佬指正) 系類1:細說委托

C#復習總結細說泛型委托

聲明 sys red 合成 delegate -s 返回 line ron 1 前言 本系列會將【委托】 【匿名方法】【Lambda表達式】 【泛型委托】 【表達式樹】 【事件】等基礎知識總結一下。(本人小白一枚,有錯誤的地方希望大佬指正) 系類1:細說委托 系類2:

面試算法題Java Stack 的使用

obj har turn cte charat arch 默認 子類 size Java Stack 類棧是Vector的一個子類,它實現了一個標準的後進先出的棧。堆棧只定義了默認構造函數,用來創建一個空棧。 常用方法1 boolean empty() 測試堆棧是否為空

C陷阱與缺陷邊界計算與不對稱邊界

前言 本文與為什麼C語言從0開始編號搭配實用更佳。 如有不足還請指正! 正文 如果一個數組有10個元素,那麼這個陣列下表的允許取值範圍是什麼呢? 下面程式碼1,這段程式碼的執行結果是什麼?為什麼? #include <stdio.h&g