| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- using System;
- using System.IO;
- using System.Text;
- using Dongke.WinForm.Utilities;
- namespace PCLCommunication
- {
- /// <summary>
- /// 日志输出
- /// </summary>
- public static class LogOut
- {
- public static string LogDirectory = ApplicationInformation.StartupDirectory + "Log\\";
- public static string Prefix_Name = null;
- public static int File_Exist_Days = 30;
- public static int File_Max_Size = 10;// 10m
- private static object lock_object = new object();
- /// <summary>
- /// 异常日志
- /// </summary>
- /// <param name="moduleName"></param>
- /// <param name="ex"></param>
- /// <param name="message"></param>
- public static void Error(string moduleName, Exception ex, string message = null)
- {
- if (ex == null && message == null)
- {
- return;
- }
- lock (lock_object)
- {
- string pp = LogDirectory + "Error\\";
- if (!string.IsNullOrWhiteSpace(moduleName))
- {
- pp += moduleName + "\\";
- }
- if (!Directory.Exists(pp))
- {
- Directory.CreateDirectory(pp);
- }
- string path = pp + "error_" + Prefix_Name + "_" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
- DeleteFiles(pp);
- BackFile(path);
- StringBuilder db = new StringBuilder("-------------------------------------------------------------------------");
- db.AppendLine();
- db.AppendLine("[ERROR] " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff"));
- System.Diagnostics.StackTrace ss = new System.Diagnostics.StackTrace(true);
- System.Reflection.MethodBase mb = ss.GetFrame(1).GetMethod();
- ////取得父方法命名空间
- //db.AppendLine(mb.DeclaringType.Namespace);
- ////取得父方法类名
- //db.AppendLine(mb.DeclaringType.Name);
- ////取得父方法类全名
- //db.AppendLine(mb.DeclaringType.FullName);
- ////取得父方法名
- //db.AppendLine(mb.Name);
- db.AppendLine(mb.DeclaringType.ToString() + "[" + mb.ToString() + "]");
- if (message != null)
- {
- db.Append("Message : ");
- db.AppendLine(message);
- }
- if (ex != null)
- {
- db.AppendLine("Exception : ");
- db.AppendLine(ex.Message);
- }
- File.AppendAllText(path, db.ToString());
- }
- }
- /// <summary>
- /// 调试日志
- /// </summary>
- /// <param name="moduleName"></param>
- /// <param name="ex"></param>
- /// <param name="message"></param>
- public static void Debug(string moduleName, string message)
- {
- if (message == null)
- {
- return;
- }
- lock (lock_object)
- {
- string pp = LogDirectory + "Debug\\";
- if (!string.IsNullOrWhiteSpace(moduleName))
- {
- pp += moduleName + "\\";
- }
- if (!Directory.Exists(pp))
- {
- Directory.CreateDirectory(pp);
- }
- string path = pp + "debug_" + Prefix_Name + "_" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
- DeleteFiles(pp);
- BackFile(path);
- StringBuilder db = new StringBuilder("-------------------------------------------------------------------------");
- db.AppendLine();
- db.AppendLine("[DEBUG] " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff"));
- System.Diagnostics.StackTrace ss = new System.Diagnostics.StackTrace(true);
- System.Reflection.MethodBase mb = ss.GetFrame(1).GetMethod();
- ////取得父方法命名空间
- //db.AppendLine(mb.DeclaringType.Namespace);
- ////取得父方法类名
- //db.AppendLine(mb.DeclaringType.Name);
- ////取得父方法类全名
- //db.AppendLine(mb.DeclaringType.FullName);
- ////取得父方法名
- //db.AppendLine(mb.Name);
- db.AppendLine(mb.DeclaringType.ToString() + "[" + mb.ToString() + "]");
- if (message != null)
- {
- db.Append("Message : ");
- db.AppendLine(message);
- }
- File.AppendAllText(path, db.ToString());
- }
- }
- /// <summary>
- /// 调试日志
- /// </summary>
- /// <param name="moduleName"></param>
- /// <param name="ex"></param>
- /// <param name="message"></param>
- public static void Info(string moduleName, string message)
- {
- if (message == null)
- {
- return;
- }
- lock (lock_object)
- {
- string pp = LogDirectory + "Info\\";
- if (!string.IsNullOrWhiteSpace(moduleName))
- {
- pp += moduleName + "\\";
- }
- if (!Directory.Exists(pp))
- {
- Directory.CreateDirectory(pp);
- }
- string path = pp + "info_" + Prefix_Name + "_" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
- DeleteFiles(pp);
- BackFile(path);
- StringBuilder db = new StringBuilder("-------------------------------------------------------------------------");
- db.AppendLine();
- db.AppendLine("[INFO] " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff"));
- System.Diagnostics.StackTrace ss = new System.Diagnostics.StackTrace(true);
- System.Reflection.MethodBase mb = ss.GetFrame(1).GetMethod();
- ////取得父方法命名空间
- //db.AppendLine(mb.DeclaringType.Namespace);
- ////取得父方法类名
- //db.AppendLine(mb.DeclaringType.Name);
- ////取得父方法类全名
- //db.AppendLine(mb.DeclaringType.FullName);
- ////取得父方法名
- //db.AppendLine(mb.Name);
- db.AppendLine(mb.DeclaringType.ToString() + "[" + mb.ToString() + "]");
- if (message != null)
- {
- db.Append("Message : ");
- db.AppendLine(message);
- }
- File.AppendAllText(path, db.ToString());
- }
- }
- private static void BackFile(string fName)
- {
- try
- {
- if (File.Exists(fName))
- {
- FileInfo fi = new FileInfo(fName);
- double l = (double)fi.Length / 1024 / 1024;
- if (l > File_Max_Size)
- {
- fName = fName.Remove(fName.Length - 4) + "_" + DateTime.Now.ToString("HHmmss") + ".log";
- fi.MoveTo(fName);
- }
- }
- }
- catch
- {
- }
- }
- private static void DeleteFiles(string path)
- {
- try
- {
- string[] dirs = Directory.GetFiles(path);
- if (dirs != null && dirs.Length > 0)
- {
- DateTime dateNow = DateTime.Now.Date.AddDays(0 - File_Exist_Days);
- foreach (string item in dirs)
- {
- if (File.GetCreationTime(item) < dateNow)
- {
- File.Delete(item);
- }
- }
- }
- }
- catch
- {
- }
- }
- }
- }
|