using System;
using System.IO;
using System.Text;
using Curtain.Framework.Windows;
namespace PCLCommunication
{
///
/// 日志输出
///
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();
///
/// 异常日志
///
///
///
///
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());
}
}
///
/// 调试日志
///
///
///
///
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());
}
}
///
/// 调试日志
///
///
///
///
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
{
}
}
}
}