地铁二期项目正式开始

LogHelper.cs 4.5KB

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