LogFactoryBase.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Diagnostics;
  7. namespace SuperSocket.SocketBase.Logging
  8. {
  9. /// <summary>
  10. /// LogFactory Base class
  11. /// </summary>
  12. public abstract class LogFactoryBase : ILogFactory
  13. {
  14. /// <summary>
  15. /// Gets the config file file path.
  16. /// </summary>
  17. protected string ConfigFile { get; private set; }
  18. /// <summary>
  19. /// Gets a value indicating whether the server instance is running in isolation mode and the multiple server instances share the same logging configuration.
  20. /// </summary>
  21. protected bool IsSharedConfig { get; private set; }
  22. /// <summary>
  23. /// Initializes a new instance of the <see cref="LogFactoryBase"/> class.
  24. /// </summary>
  25. /// <param name="configFile">The config file.</param>
  26. protected LogFactoryBase(string configFile)
  27. {
  28. if (Path.IsPathRooted(configFile))
  29. {
  30. ConfigFile = configFile;
  31. return;
  32. }
  33. if (Path.DirectorySeparatorChar != '\\')
  34. {
  35. configFile = Path.GetFileNameWithoutExtension(configFile) + ".unix" + Path.GetExtension(configFile);
  36. }
  37. var currentAppDomain = AppDomain.CurrentDomain;
  38. var isolation = IsolationMode.None;
  39. var isolationValue = currentAppDomain.GetData(typeof(IsolationMode).Name);
  40. if (isolationValue != null)
  41. isolation = (IsolationMode)isolationValue;
  42. if (isolation == IsolationMode.None)
  43. {
  44. var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configFile);
  45. if (File.Exists(filePath))
  46. {
  47. ConfigFile = filePath;
  48. return;
  49. }
  50. filePath = Path.Combine(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config"), configFile);
  51. if (File.Exists(filePath))
  52. {
  53. ConfigFile = filePath;
  54. return;
  55. }
  56. ConfigFile = configFile;
  57. return;
  58. }
  59. else //The running AppServer is in isolated appdomain
  60. {
  61. //1. search the appDomain's base directory
  62. var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configFile);
  63. if (File.Exists(filePath))
  64. {
  65. ConfigFile = filePath;
  66. return;
  67. }
  68. //go to the application's root
  69. //the appdomain's root is /WorkingDir/DomainName, so get parent path twice to reach the application root
  70. var rootDir = Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).Parent.FullName;
  71. //2. search the file with appdomain's name as prefix in the application's root
  72. //the config file whose name have appDomain's name has higher priority
  73. filePath = Path.Combine(rootDir, AppDomain.CurrentDomain.FriendlyName + "." + configFile);
  74. if (File.Exists(filePath))
  75. {
  76. ConfigFile = filePath;
  77. return;
  78. }
  79. //3. search in the application's root without appdomain's name as prefix
  80. filePath = Path.Combine(rootDir, configFile);
  81. if (File.Exists(filePath))
  82. {
  83. ConfigFile = filePath;
  84. IsSharedConfig = true;
  85. return;
  86. }
  87. rootDir = Path.Combine(rootDir, "Config");
  88. //Search the config file with appdomain's name as prefix in the Config dir
  89. filePath = Path.Combine(rootDir, AppDomain.CurrentDomain.FriendlyName + "." + configFile);
  90. if (File.Exists(filePath))
  91. {
  92. ConfigFile = filePath;
  93. return;
  94. }
  95. filePath = Path.Combine(rootDir, configFile);
  96. if (File.Exists(filePath))
  97. {
  98. ConfigFile = filePath;
  99. IsSharedConfig = true;
  100. return;
  101. }
  102. ConfigFile = configFile;
  103. return;
  104. }
  105. }
  106. /// <summary>
  107. /// Gets the log by name.
  108. /// </summary>
  109. /// <param name="name">The name.</param>
  110. /// <returns></returns>
  111. public abstract ILog GetLog(string name);
  112. }
  113. }