| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- 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();
- /// <summary>
- /// Prevents a default instance of the <see cref="LogWriter"/> class from being created.
- /// </summary>
- /// <param name="log4NetInstance">The log4net instance to be used.</param>
- private LogHelper(ILog log4NetInstance)
- {
- _log4Net = log4NetInstance;
- }
- /// <summary>
- /// Gets a logger with the specified configuration name.
- /// </summary>
- /// <param name="configName">Name of the logger in the configuration.</param>
- /// <returns>The logger obtained.</returns>
- /// <exception cref="System.Configuration.ConfigurationException">Thrown when no logger with the specified configuration name was found.</exception>
- 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);
- }
- /// <summary>
- /// Gets the default.
- /// </summary>
- public static LogHelper Default
- {
- get
- {
- return GetLogger(DEFAULT_LOGGER_NAME);
- }
- }
- /// <summary>
- /// Writes an information level logging message.
- /// </summary>
- /// <param name="message">The message to be written.</param>
- public void WriteInfo(object message)
- {
-
- _log4Net.Info(cpu+message);
- }
- /// <summary>
- /// Writes a warning level logging message.
- /// </summary>
- /// <param name="message">The message to be written.</param>
- public void WriteWarning(object message)
- {
- _log4Net.Warn(cpu + message);
- }
- /// <summary>
- /// Writes a warning level logging message.
- /// </summary>
- /// <param name="message">The message to be written.</param>
- /// <param name="exception">The exception.</param>
- public void WriteWarning(object message, System.Exception exception)
- {
- _log4Net.Warn(cpu + message, exception);
- }
- /// <summary>
- /// Writes the error.
- /// </summary>
- /// <param name="message">The message to be written.</param>
- public void WriteError(object message)
- {
- _log4Net.Error(cpu + message);
- }
- /// <summary>
- /// Writes the error level logging message..
- /// </summary>
- /// <param name="message">The message to be written.</param>
- /// <param name="exception">The exception.</param>
- public void WriteError(object message, System.Exception exception)
- {
- _log4Net.Error(cpu + message, exception);
- }
- /// <summary>
- /// Writes the fatal error level logging message..
- /// </summary>
- /// <param name="message">The message to be written.</param>
- public void WriteFatal(object message)
- {
- _log4Net.Fatal(cpu + message);
- }
- /// <summary>
- /// Writes the fatal error level logging message..
- /// </summary>
- /// <param name="message">The message to be written.</param>
- /// <param name="exception">The exception.</param>
- 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);
- }
- }
- }
- }
- }
|