LogHelper.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. using System;
  2. using System.Reflection;
  3. using log4net;
  4. using log4net.Core;
  5. using log4net.Layout;
  6. using log4net.Layout.Pattern;
  7. [assembly: log4net.Config.XmlConfigurator(Watch = true)]
  8. namespace CommonTool
  9. {
  10. public class LogHelper
  11. {
  12. private static readonly ILog Log = LogManager.GetLogger("log4net");
  13. public static void Info(string message)
  14. {
  15. Log.Info(message);
  16. }
  17. public static void Debug(string message)
  18. {
  19. Log.Debug(message);
  20. }
  21. public static void Error(string message)
  22. {
  23. Log.Error(message);
  24. }
  25. public static void Fatal(string message)
  26. {
  27. Log.Fatal(message);
  28. }
  29. public static void Warn(string message)
  30. {
  31. Log.Warn(message);
  32. }
  33. }
  34. public class ReflectionLayout : PatternLayout
  35. {
  36. public ReflectionLayout()
  37. {
  38. AddConverter("property", typeof(ReflectionPatternConverter));
  39. }
  40. }
  41. public class ReflectionPatternConverter : PatternLayoutConverter
  42. {
  43. protected override void Convert(System.IO.TextWriter writer,LoggingEvent loggingEvent)
  44. {
  45. if (Option != null)
  46. {
  47. // 写入指定键的值
  48. WriteObject(writer,loggingEvent.Repository,LookupProperty(Option,loggingEvent));
  49. }
  50. else
  51. {
  52. // 写入所有关键值对
  53. WriteDictionary(writer,loggingEvent.Repository,loggingEvent.GetProperties());
  54. }
  55. }
  56. /// <summary>
  57. /// 通过反射获取传入的日志对象的某个属性的值
  58. /// </summary>
  59. /// <param name="property"></param>
  60. /// <param name="loggingEvent"></param>
  61. /// <returns></returns>
  62. private object LookupProperty(string property,LoggingEvent loggingEvent)
  63. {
  64. object propertyValue = string.Empty;
  65. PropertyInfo propertyInfo = loggingEvent.MessageObject.GetType().GetProperty(property);
  66. if (propertyInfo != null)
  67. {
  68. propertyValue = propertyInfo.GetValue(loggingEvent.MessageObject, null);
  69. }
  70. return propertyValue;
  71. }
  72. }
  73. public class MyLogImpl : LogImpl, IMyLog
  74. {
  75. /// <summary>
  76. /// The fully qualified name of this declaring type not the type of any subclass.
  77. /// </summary>
  78. private static readonly Type ThisDeclaringType = typeof(MyLogImpl);
  79. public MyLogImpl(ILogger logger)
  80. : base(logger){}
  81. #region Implementation of IMyLog
  82. public void Debug(int operatorId, string operand, int actionType, object message, string ip, string browser,
  83. string machineName)
  84. {
  85. Debug(operatorId, operand, actionType, message, ip, browser, machineName, null);
  86. }
  87. public void Debug(int operatorId, string operand, int actionType, object message, string ip, string browser,
  88. string machineName, Exception t)
  89. {
  90. if (IsDebugEnabled)
  91. {
  92. LoggingEvent loggingEvent = new LoggingEvent(ThisDeclaringType, Logger.Repository, Logger.Name, Level.Info, message, t);
  93. loggingEvent.Properties["Operator"] = operatorId;
  94. loggingEvent.Properties["Operand"] = operand;
  95. loggingEvent.Properties["ActionType"] = actionType;
  96. loggingEvent.Properties["IP"] = ip;
  97. loggingEvent.Properties["Browser"] = browser;
  98. loggingEvent.Properties["MachineName"] = machineName;
  99. Logger.Log(loggingEvent);
  100. }
  101. }
  102. public void Info(int operatorId, string operand, int actionType, object message, string ip, string browser, string machineName)
  103. {
  104. Info(operatorId, operand, actionType, message, ip, browser, machineName, null);
  105. }
  106. public void Info(int operatorId, string operand, int actionType, object message, string ip, string browser, string machineName, Exception t)
  107. {
  108. if (IsInfoEnabled)
  109. {
  110. LoggingEvent loggingEvent = new LoggingEvent(ThisDeclaringType, Logger.Repository, Logger.Name, Level.Info, message, t);
  111. loggingEvent.Properties["Operator"] = operatorId;
  112. loggingEvent.Properties["Operand"] = operand;
  113. loggingEvent.Properties["ActionType"] = actionType;
  114. loggingEvent.Properties["IP"] = ip;
  115. loggingEvent.Properties["Browser"] = browser;
  116. loggingEvent.Properties["MachineName"] = machineName;
  117. Logger.Log(loggingEvent);
  118. }
  119. }
  120. public void Warn(int operatorId, string operand, int actionType, object message, string ip, string browser, string machineName)
  121. {
  122. Warn(operatorId, operand, actionType, message, ip, browser, machineName, null);
  123. }
  124. public void Warn(int operatorId, string operand, int actionType, object message, string ip, string browser, string machineName, Exception t)
  125. {
  126. if (IsWarnEnabled)
  127. {
  128. LoggingEvent loggingEvent = new LoggingEvent(ThisDeclaringType, Logger.Repository, Logger.Name, Level.Info, message, t);
  129. loggingEvent.Properties["Operator"] = operatorId;
  130. loggingEvent.Properties["Operand"] = operand;
  131. loggingEvent.Properties["ActionType"] = actionType;
  132. loggingEvent.Properties["IP"] = ip;
  133. loggingEvent.Properties["Browser"] = browser;
  134. loggingEvent.Properties["MachineName"] = machineName;
  135. Logger.Log(loggingEvent);
  136. }
  137. }
  138. public void Error(int operatorId, string operand, int actionType, object message, string ip, string browser, string machineName)
  139. {
  140. Error(operatorId, operand, actionType, message, ip, browser, machineName, null);
  141. }
  142. public void Error(int operatorId, string operand, int actionType, object message, string ip, string browser, string machineName, Exception t)
  143. {
  144. if (IsWarnEnabled)
  145. {
  146. LoggingEvent loggingEvent = new LoggingEvent(ThisDeclaringType, Logger.Repository, Logger.Name, Level.Info, message, t);
  147. loggingEvent.Properties["Operator"] = operatorId;
  148. loggingEvent.Properties["Operand"] = operand;
  149. loggingEvent.Properties["ActionType"] = actionType;
  150. loggingEvent.Properties["IP"] = ip;
  151. loggingEvent.Properties["Browser"] = browser;
  152. loggingEvent.Properties["MachineName"] = machineName;
  153. Logger.Log(loggingEvent);
  154. }
  155. }
  156. public void Fatal(int operatorId, string operand, int actionType, object message, string ip, string browser, string machineName)
  157. {
  158. Fatal(operatorId, operand, actionType, message, ip, browser, machineName, null);
  159. }
  160. public void Fatal(int operatorId, string operand, int actionType, object message, string ip, string browser, string machineName, Exception t)
  161. {
  162. if (IsWarnEnabled)
  163. {
  164. LoggingEvent loggingEvent = new LoggingEvent(ThisDeclaringType, Logger.Repository, Logger.Name, Level.Info, message, t);
  165. loggingEvent.Properties["Operator"] = operatorId;
  166. loggingEvent.Properties["Operand"] = operand;
  167. loggingEvent.Properties["ActionType"] = actionType;
  168. loggingEvent.Properties["IP"] = ip;
  169. loggingEvent.Properties["Browser"] = browser;
  170. loggingEvent.Properties["MachineName"] = machineName;
  171. Logger.Log(loggingEvent);
  172. }
  173. }
  174. #endregion Implementation of IMyLog
  175. }
  176. public class MyLogManager
  177. {
  178. #region Static Member Variables
  179. /// <summary>
  180. /// The wrapper map to use to hold the
  181. ///<see>
  182. ///<cref>EventIDLogImpl</cref>
  183. ///</see>
  184. ///objects
  185. /// </summary>
  186. private static readonly WrapperMap SWrapperMap = new WrapperMap(WrapperCreationHandler);
  187. #endregion
  188. #region Constructor
  189. /// <summary>
  190. /// Private constructor to prevent object creation
  191. /// </summary>
  192. private MyLogManager() { }
  193. #endregion
  194. #region Type Specific Manager Methods
  195. /// <summary>
  196. /// Returns the named logger if it exists
  197. /// </summary>
  198. /// <remarks>
  199. /// <para>If the named logger exists (in the default hierarchy) then it
  200. /// returns a reference to the logger, otherwise it returns
  201. /// <c>null</c>.</para>
  202. /// </remarks>
  203. /// <param name="name">The fully qualified logger name to look for</param>
  204. /// <returns>The logger found, or null</returns>
  205. public static IMyLog Exists(string name)
  206. {
  207. return Exists(Assembly.GetCallingAssembly(), name);
  208. }
  209. /// <summary>
  210. /// Returns the named logger if it exists
  211. /// </summary>
  212. /// <remarks>
  213. /// <para>If the named logger exists (in the specified domain) then it
  214. /// returns a reference to the logger, otherwise it returns
  215. /// <c>null</c>.</para>
  216. /// </remarks>
  217. /// <param name="domain">the domain to lookup in</param>
  218. /// <param name="name">The fully qualified logger name to look for</param>
  219. /// <returns>The logger found, or null</returns>
  220. public static IMyLog Exists(string domain, string name)
  221. {
  222. return WrapLogger(LoggerManager.Exists(domain, name));
  223. }
  224. /// <summary>
  225. /// Returns the named logger if it exists
  226. /// </summary>
  227. /// <remarks>
  228. /// <para>If the named logger exists (in the specified assembly's domain) then it
  229. /// returns a reference to the logger, otherwise it returns
  230. /// <c>null</c>.</para>
  231. /// </remarks>
  232. /// <param name="assembly">the assembly to use to lookup the domain</param>
  233. /// <param name="name">The fully qualified logger name to look for</param>
  234. /// <returns>The logger found, or null</returns>
  235. public static IMyLog Exists(Assembly assembly, string name)
  236. {
  237. return WrapLogger(LoggerManager.Exists(assembly, name));
  238. }
  239. /// <summary>
  240. /// Returns all the currently defined loggers in the default domain.
  241. /// </summary>
  242. /// <remarks>
  243. /// <para>The root logger is <b>not</b> included in the returned array.</para>
  244. /// </remarks>
  245. /// <returns>All the defined loggers</returns>
  246. public static IMyLog[] GetCurrentLoggers()
  247. {
  248. return GetCurrentLoggers(Assembly.GetCallingAssembly());
  249. }
  250. /// <summary>
  251. /// Returns all the currently defined loggers in the specified domain.
  252. /// </summary>
  253. /// <param name="domain">the domain to lookup in</param>
  254. /// <remarks>
  255. /// The root logger is <b>not</b> included in the returned array.
  256. /// </remarks>
  257. /// <returns>All the defined loggers</returns>
  258. public static IMyLog[] GetCurrentLoggers(string domain)
  259. {
  260. return WrapLoggers(LoggerManager.GetCurrentLoggers(domain));
  261. }
  262. /// <summary>
  263. /// Returns all the currently defined loggers in the specified assembly's domain.
  264. /// </summary>
  265. /// <param name="assembly">the assembly to use to lookup the domain</param>
  266. /// <remarks>
  267. /// The root logger is <b>not</b> included in the returned array.
  268. /// </remarks>
  269. /// <returns>All the defined loggers</returns>
  270. public static IMyLog[] GetCurrentLoggers(Assembly assembly)
  271. {
  272. return WrapLoggers(LoggerManager.GetCurrentLoggers(assembly));
  273. }
  274. /// <summary>
  275. /// Retrieve or create a named logger.
  276. /// </summary>
  277. /// <remarks>
  278. /// <para>Retrieve a logger named as the <paramref name="name"/>
  279. /// parameter. If the named logger already exists, then the
  280. /// existing instance will be returned. Otherwise, a new instance is
  281. /// created.</para>
  282. ///
  283. /// <para>By default, loggers do not have a set level but inherit
  284. /// it from the hierarchy. This is one of the central features of
  285. /// log4net.</para>
  286. /// </remarks>
  287. /// <param name="name">The name of the logger to retrieve.</param>
  288. /// <returns>the logger with the name specified</returns>
  289. public static IMyLog GetLogger(string name)
  290. {
  291. return GetLogger(Assembly.GetCallingAssembly(), name);
  292. }
  293. /// <summary>
  294. /// Retrieve or create a named logger.
  295. /// </summary>
  296. /// <remarks>
  297. /// <para>Retrieve a logger named as the <paramref name="name"/>
  298. /// parameter. If the named logger already exists, then the
  299. /// existing instance will be returned. Otherwise, a new instance is
  300. /// created.</para>
  301. ///
  302. /// <para>By default, loggers do not have a set level but inherit
  303. /// it from the hierarchy. This is one of the central features of
  304. /// log4net.</para>
  305. /// </remarks>
  306. /// <param name="domain">the domain to lookup in</param>
  307. /// <param name="name">The name of the logger to retrieve.</param>
  308. /// <returns>the logger with the name specified</returns>
  309. public static IMyLog GetLogger(string domain, string name)
  310. {
  311. return WrapLogger(LoggerManager.GetLogger(domain, name));
  312. }
  313. /// <summary>
  314. /// Retrieve or create a named logger.
  315. /// </summary>
  316. /// <remarks>
  317. /// <para>Retrieve a logger named as the <paramref name="name"/>
  318. /// parameter. If the named logger already exists, then the
  319. /// existing instance will be returned. Otherwise, a new instance is
  320. /// created.</para>
  321. ///
  322. /// <para>By default, loggers do not have a set level but inherit
  323. /// it from the hierarchy. This is one of the central features of
  324. /// log4net.</para>
  325. /// </remarks>
  326. /// <param name="assembly">the assembly to use to lookup the domain</param>
  327. /// <param name="name">The name of the logger to retrieve.</param>
  328. /// <returns>the logger with the name specified</returns>
  329. public static IMyLog GetLogger(Assembly assembly, string name)
  330. {
  331. return WrapLogger(LoggerManager.GetLogger(assembly, name));
  332. }
  333. /// <summary>
  334. /// Shorthand for <see cref="LogManager.GetLogger(string)"/>.
  335. /// </summary>
  336. /// <remarks>
  337. /// Get the logger for the fully qualified name of the type specified.
  338. /// </remarks>
  339. /// <param name="type">The full name of <paramref name="type"/> will
  340. /// be used as the name of the logger to retrieve.</param>
  341. /// <returns>the logger with the name specified</returns>
  342. public static IMyLog GetLogger(Type type)
  343. {
  344. return GetLogger(Assembly.GetCallingAssembly(), type.FullName);
  345. }
  346. /// <summary>
  347. /// Shorthand for <see cref="LogManager.GetLogger(string)"/>.
  348. /// </summary>
  349. /// <remarks>
  350. /// Get the logger for the fully qualified name of the type specified.
  351. /// </remarks>
  352. /// <param name="domain">the domain to lookup in</param>
  353. /// <param name="type">The full name of <paramref name="type"/> will
  354. /// be used as the name of the logger to retrieve.</param>
  355. /// <returns>the logger with the name specified</returns>
  356. public static IMyLog GetLogger(string domain, Type type)
  357. {
  358. return WrapLogger(LoggerManager.GetLogger(domain, type));
  359. }
  360. /// <summary>
  361. /// Shorthand for <see cref="LogManager.GetLogger(string)"/>.
  362. /// </summary>
  363. /// <remarks>
  364. /// Get the logger for the fully qualified name of the type specified.
  365. /// </remarks>
  366. /// <param name="assembly">the assembly to use to lookup the domain</param>
  367. /// <param name="type">The full name of <paramref name="type"/> will
  368. /// be used as the name of the logger to retrieve.</param>
  369. /// <returns>the logger with the name specified</returns>
  370. public static IMyLog GetLogger(Assembly assembly, Type type)
  371. {
  372. return WrapLogger(LoggerManager.GetLogger(assembly, type));
  373. }
  374. #endregion
  375. #region Extension Handlers
  376. /// <summary>
  377. /// Lookup the wrapper object for the logger specified
  378. /// </summary>
  379. /// <param name="logger">the logger to get the wrapper for</param>
  380. /// <returns>the wrapper for the logger specified</returns>
  381. private static IMyLog WrapLogger(ILogger logger)
  382. {
  383. return (IMyLog)SWrapperMap.GetWrapper(logger);
  384. }
  385. /// <summary>
  386. /// Lookup the wrapper objects for the loggers specified
  387. /// </summary>
  388. /// <param name="loggers">the loggers to get the wrappers for</param>
  389. /// <returns>Lookup the wrapper objects for the loggers specified</returns>
  390. private static IMyLog[] WrapLoggers(ILogger[] loggers)
  391. {
  392. IMyLog[] results = new IMyLog[loggers.Length];
  393. for (int i = 0; i < loggers.Length; i++)
  394. {
  395. results[i] = WrapLogger(loggers[i]);
  396. }
  397. return results;
  398. }
  399. /// <summary>
  400. /// Method to create the <see cref="ILoggerWrapper"/> objects used by
  401. /// this manager.
  402. /// </summary>
  403. /// <param name="logger">The logger to wrap</param>
  404. /// <returns>The wrapper for the logger specified</returns>
  405. private static ILoggerWrapper WrapperCreationHandler(ILogger logger)
  406. {
  407. return new MyLogImpl(logger);
  408. }
  409. #endregion
  410. }
  411. public interface IMyLog : ILog
  412. {
  413. void Debug(int operatorId, string operand, int actionType, object message, string ip, string browser,
  414. string machineName);
  415. void Debug(int operatorId, string operand, int actionType, object message,string ip, string browser, string machineName, Exception t);
  416. void Info(int operatorId, string operand, int actionType, object message, string ip, string browser,
  417. string machineName);
  418. void Info(int operatorId, string operand, int actionType, object message, string ip, string browser, string machineName, Exception t);
  419. void Warn(int operatorId, string operand, int actionType, object message, string ip, string browser,
  420. string machineName);
  421. void Warn(int operatorId, string operand, int actionType, object message, string ip, string browser, string machineName, Exception t);
  422. void Error(int operatorId, string operand, int actionType, object message, string ip, string browser,
  423. string machineName);
  424. void Error(int operatorId, string operand, int actionType, object message, string ip, string browser, string machineName, Exception t);
  425. void Fatal(int operatorId, string operand, int actionType, object message, string ip, string browser,
  426. string machineName);
  427. void Fatal(int operatorId, string operand, int actionType, object message, string ip, string browser, string machineName, Exception t);
  428. }
  429. }