Log4NetLogFactory.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Xml;
  7. using log4net;
  8. using log4net.Config;
  9. namespace SuperSocket.SocketBase.Logging
  10. {
  11. /// <summary>
  12. /// Log4NetLogFactory
  13. /// </summary>
  14. public class Log4NetLogFactory : LogFactoryBase
  15. {
  16. /// <summary>
  17. /// Initializes a new instance of the <see cref="Log4NetLogFactory"/> class.
  18. /// </summary>
  19. public Log4NetLogFactory()
  20. : this("log4net.config")
  21. {
  22. }
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="Log4NetLogFactory"/> class.
  25. /// </summary>
  26. /// <param name="log4netConfig">The log4net config.</param>
  27. public Log4NetLogFactory(string log4netConfig)
  28. : base(log4netConfig)
  29. {
  30. if (!IsSharedConfig)
  31. {
  32. log4net.Config.XmlConfigurator.Configure(new FileInfo(ConfigFile));
  33. }
  34. else
  35. {
  36. //Disable Performance logger
  37. var xmlDoc = new XmlDocument();
  38. xmlDoc.Load(ConfigFile);
  39. var docElement = xmlDoc.DocumentElement;
  40. var perfLogNode = docElement.SelectSingleNode("logger[@name='Performance']");
  41. if (perfLogNode != null)
  42. docElement.RemoveChild(perfLogNode);
  43. log4net.Config.XmlConfigurator.Configure(docElement);
  44. }
  45. }
  46. /// <summary>
  47. /// Gets the log by name.
  48. /// </summary>
  49. /// <param name="name">The name.</param>
  50. /// <returns></returns>
  51. public override ILog GetLog(string name)
  52. {
  53. return new Log4NetLog(LogManager.GetLogger(name));
  54. }
  55. }
  56. }