using System; using System.Collections.Generic; using System.Text; using System.IO; using log4net; namespace YTSoft.Common { public class LogHelper { private ILog _log4Net = null; private const string DEFAULT_LOGGER_NAME = "Logger"; public string cpu =new SystemInfo().GetCpuPerformancevalue(); /// /// Prevents a default instance of the class from being created. /// /// The log4net instance to be used. private LogHelper(ILog log4NetInstance) { _log4Net = log4NetInstance; } /// /// Gets a logger with the specified configuration name. /// /// Name of the logger in the configuration. /// The logger obtained. /// Thrown when no logger with the specified configuration name was found. public static LogHelper GetLogger(string configName) { var logger = LogManager.GetLogger(configName); if (logger == null) { throw new ArgumentException(string.Format("No logger configuration named '{0}' was found in the configuration.", configName), "configName"); } return new LogHelper(logger); } /// /// Gets the default. /// public static LogHelper Default { get { return GetLogger(DEFAULT_LOGGER_NAME); } } /// /// Writes an information level logging message. /// /// The message to be written. public void WriteInfo(object message) { _log4Net.Info(cpu+message); } /// /// Writes a warning level logging message. /// /// The message to be written. public void WriteWarning(object message) { _log4Net.Warn(cpu + message); } /// /// Writes a warning level logging message. /// /// The message to be written. /// The exception. public void WriteWarning(object message, System.Exception exception) { _log4Net.Warn(cpu + message, exception); } /// /// Writes the error. /// /// The message to be written. public void WriteError(object message) { _log4Net.Error(cpu + message); } /// /// Writes the error level logging message.. /// /// The message to be written. /// The exception. public void WriteError(object message, System.Exception exception) { _log4Net.Error(cpu + message, exception); } /// /// Writes the fatal error level logging message.. /// /// The message to be written. public void WriteFatal(object message) { _log4Net.Fatal(cpu + message); } /// /// Writes the fatal error level logging message.. /// /// The message to be written. /// The exception. public void WriteFatal(object message, System.Exception exception) { _log4Net.Fatal(cpu + message, exception); } public void DeleteLog() { string logDirPath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "Log"); if (!Directory.Exists(logDirPath)) return; int days = 30; foreach (string filePath in Directory.GetFiles(logDirPath)) { DateTime dt; DateTime.TryParse(Path.GetFileNameWithoutExtension(filePath).Replace(@"Log\", "").Replace(".", "-"), out dt); if (dt.AddDays(days).CompareTo(DateTime.Now) < 0) { File.Delete(filePath); } } } } }