首页 > 代码库 > 日志class

日志class

技术分享
  1 public class LogCommon  2     {  3         #region 全局变量  4         /// <summary>  5         /// 日志配置文件路径  6         /// </summary>  7         private static string ConfigPath = string.Empty;  8   9  10         /// <summary> 11         /// 日志文件根目录 12         /// </summary> 13         private static string LogBasePath = string.Empty; 14  15         /// <summary> 16         /// 日志 17         /// </summary> 18         private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); 19  20         #endregion 21  22         #region 加载日志配置文件 23         /// <summary> 24         ///加载日志配置文件  25         /// </summary> 26         public static void LoadLogConfig() 27         { 28             //string strRemot = Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).Parent.Parent.FullName; 29  30             string strRemot = AppDomain.CurrentDomain.BaseDirectory; 31             LogBasePath = strRemot + "\\Log\\"; 32         } 33         #endregion 34  35         #region 根据规则,命名日志文件 36         /// <summary> 37         ///根据规则,命名日志文件  38         /// </summary> 39         /// <returns></returns> 40         public static string GetLogFileName(string LogName) 41         { 42             //加载日志文件配置信息 43             LoadLogConfig(); 44             //文件前缀 45             string prefx = string.Empty; 46             //日期格式 47             string date = "yyyyMMdd"; 48             //扩展名 49             string extention = "log"; 50             //分隔符号 51             string split = string.Empty; 52  53             //根据文件进行配置 54  55             string fileName = prefx + split + DateTime.Now.ToString(date) + "." + extention; 56             return fileName; 57         } 58         #endregion 59  60         #region 记录日志 61         /// <summary> 62         ///记录日志  63         /// </summary> 64         public static void LogInfor(string LogName, string Log) 65         { 66             //获取Log文件名称 67             string Logfile = GetLogFileName(LogName); 68             string LogPath = System.Windows.Forms.Application.StartupPath + "\\Log\\" + LogName + "\\"; 69             string path = LogPath + Logfile; 70             if (!Directory.Exists(LogPath)) 71             { 72                 Directory.CreateDirectory(LogPath); 73             } 74             FilesManager.WriteLineAppendTxt(path, Log); 75  76         } 77         /// <summary> 78         ///记录操作日志  79         /// </summary> 80         public static void LogLocalInfor(string LogName, string Log) 81         { 82             //获取Log文件名称 83             string Logfile = GetLogFileName(LogName); 84             string LogPath = System.Windows.Forms.Application.StartupPath + "\\Log\\" + LogName + "\\"; 85             string path = LogPath + Logfile; 86             if (!Directory.Exists(LogPath)) 87             { 88                 Directory.CreateDirectory(LogPath); 89             } 90             FilesManager.WriteLineAppendTxt(path, Log); 91  92         } 93         /// <summary> 94         ///记录异常日志信息 95         /// </summary> 96         /// <param name="ObjectName"></param> 97         /// <param name="FunctionName"></param> 98         /// <param name="ErrorMsg"></param> 99         public static void ErrorLogInfo(string ObjectName, string FunctionName, string ErrorMsg)100         {101             StringBuilder log = new StringBuilder();102             log.Append(DateTime.Now.ToString("yyyyMMddHHmmss"));103             log.Append("  ");104             log.Append(ObjectName + "  ");105             log.Append(ErrorMsg);106             Log.ErrorFormat("{0}-  {1}   -{2}", ObjectName, FunctionName, log.ToString());107 108         }109         /// <summary>110         /// 记录异常日志111         /// </summary>112         /// <param name="objectName"></param>113         /// <param name="ex"></param>114         public static void ErrorLogInfo(string objectName, string FunctionName, Exception ex)115         {116             StringBuilder log = new StringBuilder();117             log.Append(DateTime.Now.ToString("yyyyMMddHHmmss"));118             log.Append("  ");119             log.Append(objectName + "  ");120             log.Append(ex.Message + "   ");121             log.AppendLine(ex.HelpLink + "   ");122             log.AppendLine(ex.Source + "   ");123             log.AppendLine(ex.StackTrace + "  ");124             log.AppendLine(ex.TargetSite.Name + "   ");125             if (ex.InnerException != null)126             {127                 log.AppendLine(ex.InnerException.Message + "");128             }129             //记录异常日志130             Log.Error(log.ToString());131 132         }133         /// <summary>134         /// 记录异常日志135         /// </summary>136         /// <param name="Msg"></param>137         /// <param name="ex"></param>138         public static void ErrorLogInfo(string Msg, Exception ex)139         {140             Log.Error(Msg, ex);141         }142         #endregion143     }
View Code

 

日志class