地铁二期项目正式开始

LogHelper.cs 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using log4net;
  6. namespace YTSoft.Common
  7. {
  8. public class LogHelper
  9. {
  10. private ILog _log4Net = null;
  11. private const string DEFAULT_LOGGER_NAME = "Logger";
  12. /// <summary>
  13. /// Prevents a default instance of the <see cref="LogWriter"/> class from being created.
  14. /// </summary>
  15. /// <param name="log4NetInstance">The log4net instance to be used.</param>
  16. private LogHelper(ILog log4NetInstance)
  17. {
  18. _log4Net = log4NetInstance;
  19. }
  20. /// <summary>
  21. /// Gets a logger with the specified configuration name.
  22. /// </summary>
  23. /// <param name="configName">Name of the logger in the configuration.</param>
  24. /// <returns>The logger obtained.</returns>
  25. /// <exception cref="System.Configuration.ConfigurationException">Thrown when no logger with the specified configuration name was found.</exception>
  26. public static LogHelper GetLogger(string configName)
  27. {
  28. var logger = LogManager.GetLogger(configName);
  29. if (logger == null)
  30. {
  31. throw new ArgumentException(string.Format("No logger configuration named '{0}' was found in the configuration.", configName), "configName");
  32. }
  33. return new LogHelper(logger);
  34. }
  35. /// <summary>
  36. /// Gets the default.
  37. /// </summary>
  38. public static LogHelper Default
  39. {
  40. get
  41. {
  42. return GetLogger(DEFAULT_LOGGER_NAME);
  43. }
  44. }
  45. /// <summary>
  46. /// Writes an information level logging message.
  47. /// </summary>
  48. /// <param name="message">The message to be written.</param>
  49. public void WriteInfo(object message)
  50. {
  51. _log4Net.Info(message);
  52. }
  53. /// <summary>
  54. /// Writes a warning level logging message.
  55. /// </summary>
  56. /// <param name="message">The message to be written.</param>
  57. public void WriteWarning(object message)
  58. {
  59. _log4Net.Warn(message);
  60. }
  61. /// <summary>
  62. /// Writes a warning level logging message.
  63. /// </summary>
  64. /// <param name="message">The message to be written.</param>
  65. /// <param name="exception">The exception.</param>
  66. public void WriteWarning(object message, System.Exception exception)
  67. {
  68. _log4Net.Warn(message, exception);
  69. }
  70. /// <summary>
  71. /// Writes the error.
  72. /// </summary>
  73. /// <param name="message">The message to be written.</param>
  74. public void WriteError(object message)
  75. {
  76. _log4Net.Error(message);
  77. }
  78. /// <summary>
  79. /// Writes the error level logging message..
  80. /// </summary>
  81. /// <param name="message">The message to be written.</param>
  82. /// <param name="exception">The exception.</param>
  83. public void WriteError(object message, System.Exception exception)
  84. {
  85. _log4Net.Error(message, exception);
  86. }
  87. /// <summary>
  88. /// Writes the fatal error level logging message..
  89. /// </summary>
  90. /// <param name="message">The message to be written.</param>
  91. public void WriteFatal(object message)
  92. {
  93. _log4Net.Fatal(message);
  94. }
  95. /// <summary>
  96. /// Writes the fatal error level logging message..
  97. /// </summary>
  98. /// <param name="message">The message to be written.</param>
  99. /// <param name="exception">The exception.</param>
  100. public void WriteFatal(object message, System.Exception exception)
  101. {
  102. _log4Net.Fatal(message, exception);
  103. }
  104. public void DeleteLog()
  105. {
  106. string logDirPath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "Log");
  107. if (!Directory.Exists(logDirPath)) return;
  108. int days = 30;
  109. foreach (string filePath in Directory.GetFiles(logDirPath))
  110. {
  111. DateTime dt;
  112. DateTime.TryParse(Path.GetFileNameWithoutExtension(filePath).Replace(@"Log\", "").Replace(".", "-"), out dt);
  113. if (dt.AddDays(days).CompareTo(DateTime.Now) < 0)
  114. {
  115. File.Delete(filePath);
  116. }
  117. }
  118. }
  119. }
  120. }