|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- using log4net;
- using log4net.Appender;
- using log4net.Config;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Text;
-
- namespace CnasSynchronousCommon
- {
- public static class AppLog
- {
- //private static ILog log;
- public static ILog logInfo;
- public static ILog logErr;
- public static ILog logServiceInfo;
- static AppLog()
- {
- XmlConfigurator.ConfigureAndWatch(new FileInfo(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile));
- //log = LogManager.GetLogger(typeof(AppLog));
- logInfo = LogManager.GetLogger("Log");
- logErr = LogManager.GetLogger("Err");
- logServiceInfo = LogManager.GetLogger("ServiceLog");
- }
- public static void DeleteLog(int days)
- {
- DeleteLog(logInfo, days);
- DeleteLog(logErr, days);
- DeleteLog(logServiceInfo, days);
- }
- private static void DeleteLog(ILog _log, int days)
- {
- var apps = _log.Logger.Repository.GetAppenders();
- if (apps.Length <= 0)
- {
- return;
- }
- var now = DateTime.UtcNow.AddDays(days);
- foreach (var item in apps)
- {
- if (item is RollingFileAppender)
- {
- RollingFileAppender roll = item as RollingFileAppender;
- var dir = Path.GetDirectoryName(roll.File);
- var files = Directory.GetFiles(dir, "*.txt");
-
- foreach (var filePath in files)
- {
- var file = new FileInfo(filePath);
- if (file.CreationTimeUtc < now)
- {
- try
- {
- file.Delete();
- }
- catch (Exception ex)
- {
- Error(ex.Message);
- }
- }
- }
- }
- }
- }
-
- public static void Info()
- {
- throw new NotImplementedException();
- }
-
- //public static void Debug(object message)
- //{
- // log.Debug(message);
- //}
-
- //public static void DebugFormatted(string format, params object[] args)
- //{
- // log.DebugFormat(format, args);
- //}
-
- //public static void Info(object message)
- //{
- // log.Info(message);
- //}
-
- //public static void InfoFormatted(string format, params object[] args)
- //{
- // log.InfoFormat(format, args);
- //}
-
- //public static void Warn(object message)
- //{
- // log.Warn(message);
- //}
-
- //public static void Warn(object message, Exception exception)
- //{
- // log.Warn(message, exception);
- //}
-
- //public static void WarnFormatted(string format, params object[] args)
- //{
- // log.WarnFormat(format, args);
- //}
-
- //public static void Error(object message)
- //{
- // log.Error(message);
- //}
-
- //public static void Error(object message, Exception exception)
- //{
- // log.Error(message, exception);
- //}
-
- //public static void ErrorFormatted(string format, params object[] args)
- //{
- // log.ErrorFormat(format, args);
- //}
-
- //public static void Fatal(object message)
- //{
- // log.Fatal(message);
- //}
-
- //public static void Fatal(object message, Exception exception)
- //{
- // log.Fatal(message, exception);
- //}
-
- //public static void FatalFormatted(string format, params object[] args)
- //{
- // log.FatalFormat(format, args);
- //}
-
- /// <summary>
- /// 记录正常的消息
- /// </summary>
- /// <param name="msg">消息内容</param>
- public static void Info(string msg)
- {
- logInfo.Info(msg);
- }
- /// <summary>
- /// 记录异常信息
- /// </summary>
- /// <param name="msg">异常信息内容</param>
- public static void Error(string msg)
- {
- StackTrace stackTrace = new StackTrace();
- StackFrame stackFrame = stackTrace.GetFrame(1);
- MethodBase methodBase = stackFrame.GetMethod();
- logErr.Error("类名:" + methodBase.ReflectedType.Name + " 方法名:" + methodBase.Name + " 信息:" + msg);
- }
-
- /// <summary>
- /// 记录服务执行信息
- /// </summary>
- /// <param name="msg"></param>
- public static void ServiceInfo(string msg)
- {
- StackTrace stackTrace = new StackTrace();
- StackFrame stackFrame = stackTrace.GetFrame(1);
- MethodBase methodBase = stackFrame.GetMethod();
- logServiceInfo.Info("类名:" + methodBase.ReflectedType.Name + " 方法名:" + methodBase.Name + " 信息:" + msg);
- }
- }
- }
|