|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
using System.IO;
|
|
|
using System.Threading;
|
|
|
using System.Globalization;
|
|
|
using System.Timers;
|
|
|
|
|
|
namespace CommService
|
|
|
{
|
|
|
/*
|
|
|
*类名称:LogFile
|
|
|
*创建人:韩荣伟
|
|
|
*创建时间:2010-10-30
|
|
|
*功能描述:log文件处理类
|
|
|
*/
|
|
|
class LogFile
|
|
|
{
|
|
|
private string sPath;
|
|
|
//private static Mutex mut = new Mutex();
|
|
|
//private static Mutex mutException = new Mutex();
|
|
|
|
|
|
//private static LogFile inst = null;
|
|
|
private Mutex mut = new Mutex();
|
|
|
private Mutex mutException = new Mutex();
|
|
|
|
|
|
//private LogFile inst = null;
|
|
|
private string log;
|
|
|
private DateTime dtNow;
|
|
|
private bool logOn;
|
|
|
private long maxSize;
|
|
|
private string fname;
|
|
|
|
|
|
private string sExceptionPath;
|
|
|
private string sExceptionFName;
|
|
|
|
|
|
public static int LOGLVL_DEBUG = -1;
|
|
|
public static int LOGLVL_NORMAL = 0;
|
|
|
public static int LOGLVL_ERROR = 1;
|
|
|
private int nLogLvl_Min = LOGLVL_NORMAL;
|
|
|
public string sRootPath;
|
|
|
|
|
|
Log logWrite= new Log();
|
|
|
/*
|
|
|
*方法名称:instance
|
|
|
*创建人:韩荣伟
|
|
|
*创建时间:2010-10-30
|
|
|
*参数描述:
|
|
|
*返回描述:LogFile,LogFile实例
|
|
|
*功能描述:生成LogFile实例
|
|
|
*/
|
|
|
//public static LogFile instance()
|
|
|
//{
|
|
|
// if (inst == null)
|
|
|
// {
|
|
|
// inst = new LogFile();
|
|
|
// }
|
|
|
// return inst;
|
|
|
//}
|
|
|
/*
|
|
|
*方法名称:SetLogOn
|
|
|
*创建人:韩荣伟
|
|
|
*创建时间:2010-10-30
|
|
|
*参数描述:bool on log开关,true 开,false 关
|
|
|
*返回描述:void
|
|
|
*功能描述:设置log开关
|
|
|
*/
|
|
|
public void SetLogMinLevel(string sLevelName)
|
|
|
{
|
|
|
if (sLevelName.CompareTo("DEBUG") == 0)
|
|
|
{
|
|
|
nLogLvl_Min = LOGLVL_DEBUG;
|
|
|
}
|
|
|
else if (sLevelName.CompareTo("NORMAL") == 0)
|
|
|
{
|
|
|
nLogLvl_Min = LOGLVL_NORMAL;
|
|
|
}
|
|
|
else if (sLevelName.CompareTo("ERROR") == 0)
|
|
|
{
|
|
|
nLogLvl_Min = LOGLVL_ERROR;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
nLogLvl_Min = LOGLVL_NORMAL;
|
|
|
}
|
|
|
}
|
|
|
/*
|
|
|
*方法名称:CheckFSize
|
|
|
*创建人:韩荣伟
|
|
|
*创建时间:2010-10-30
|
|
|
*参数描述:void
|
|
|
*返回描述:void
|
|
|
*功能描述:设置log开关
|
|
|
*/
|
|
|
private void CheckFSize(int nLogLevel)
|
|
|
{
|
|
|
string sFullPath = "";
|
|
|
string sPrefix = "";
|
|
|
string sFName = "";
|
|
|
|
|
|
if (nLogLevel == LOGLVL_NORMAL || nLogLevel == LOGLVL_DEBUG)
|
|
|
{
|
|
|
sFullPath = sPath;
|
|
|
sPrefix = "logbackup";
|
|
|
sFName = fname;
|
|
|
}
|
|
|
else if (nLogLevel == LOGLVL_ERROR)
|
|
|
{
|
|
|
sFullPath = sExceptionPath;
|
|
|
sPrefix = "errbackup";
|
|
|
sFName = sExceptionFName;
|
|
|
}
|
|
|
|
|
|
//if (nLogLevel == LOGLVL_NORMAL)
|
|
|
//{
|
|
|
FileInfo fi = new FileInfo(sFullPath);
|
|
|
if (fi.Exists && fi.Length >= maxSize)
|
|
|
{
|
|
|
string pathName = Path.GetDirectoryName(sFullPath);
|
|
|
string newFName = sFName;
|
|
|
DateTime dtNow = DateTime.Now;
|
|
|
|
|
|
newFName = sPrefix + dtNow.ToString("_yyyyMMdd_HHmmss_fff", CultureInfo.InvariantCulture) + ".txt";
|
|
|
try
|
|
|
{
|
|
|
fi.MoveTo(pathName + "\\" + newFName);
|
|
|
}
|
|
|
catch (Exception e1)
|
|
|
{
|
|
|
//fi.CopyTo(pathName + "\\" + newFName);
|
|
|
|
|
|
//if ((nLogLevel == LOGLVL_NORMAL || nLogLevel == LOGLVL_DEBUG))
|
|
|
//{
|
|
|
// sPath = pathName + "\\log" + dtNow.ToString("_yyyyMMdd_HHmmss_fff", CultureInfo.InvariantCulture) + ".txt";
|
|
|
//}
|
|
|
//else if (nLogLevel == LOGLVL_ERROR)
|
|
|
//{
|
|
|
// sExceptionPath = pathName + "\\err" + dtNow.ToString("_yyyyMMdd_HHmmss_fff", CultureInfo.InvariantCulture) + ".txt";
|
|
|
//}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
/*
|
|
|
*方法名称:init
|
|
|
*创建人:韩荣伟
|
|
|
*创建时间:2010-10-30
|
|
|
*参数描述:string logPath 路径, string logFName文件名
|
|
|
*返回描述:void
|
|
|
*功能描述:初始化路径文件名
|
|
|
*/
|
|
|
public void init(string sAPPBase,string slogPath, string slogFName, string sExceptionPath, string sExceptionFName, long maxSizeInByte)
|
|
|
{
|
|
|
DirectoryInfo di = new DirectoryInfo(sAPPBase + slogPath);
|
|
|
if (di.Exists == false)
|
|
|
{
|
|
|
di.Create();
|
|
|
}
|
|
|
sRootPath = sAPPBase + slogPath;
|
|
|
sPath = sRootPath + "\\" + slogFName;
|
|
|
logOn = true;
|
|
|
maxSize = maxSizeInByte;
|
|
|
fname = slogFName;
|
|
|
|
|
|
di = new DirectoryInfo(sAPPBase + slogPath+sExceptionPath);
|
|
|
if (di.Exists == false)
|
|
|
{
|
|
|
di.Create();
|
|
|
}
|
|
|
this.sExceptionPath = sAPPBase + slogPath + sExceptionPath + "\\"+ sExceptionFName;
|
|
|
this.sExceptionFName = sExceptionFName;
|
|
|
}
|
|
|
/*
|
|
|
*方法名称:write
|
|
|
*创建人:韩荣伟
|
|
|
*创建时间:2010-10-30
|
|
|
*参数描述:string logstr 日志字符串
|
|
|
*返回描述:void
|
|
|
*功能描述:写入日志字符串
|
|
|
*/
|
|
|
public void write(string logstr, int nLogLevel)
|
|
|
{
|
|
|
if (nLogLevel < nLogLvl_Min ||logOn == false)
|
|
|
return;
|
|
|
|
|
|
//lock
|
|
|
try
|
|
|
{
|
|
|
mut.WaitOne();
|
|
|
{
|
|
|
CheckFSize(nLogLevel);
|
|
|
|
|
|
//format
|
|
|
dtNow = DateTime.Now;
|
|
|
log = dtNow.ToString("yyyy-MM-dd HH:mm:ss ", CultureInfo.InvariantCulture);
|
|
|
log += logstr + "\r\n";
|
|
|
//open file //write //close file
|
|
|
try
|
|
|
{
|
|
|
//using (FileStream fs
|
|
|
// = File.Open((nLogLevel == LOGLVL_ERROR ? sExceptionPath : sPath),
|
|
|
// FileMode.OpenOrCreate | FileMode.Append))
|
|
|
//{
|
|
|
// Byte[] info = new UTF8Encoding(true).GetBytes(log);
|
|
|
// // Add some information to the file.
|
|
|
// fs.Write(info, 0, info.Length);
|
|
|
// fs.Close();
|
|
|
//}
|
|
|
Msg msg = new Msg(log, nLogLevel == LOGLVL_ERROR ? sExceptionPath : sPath);
|
|
|
logWrite.Write(msg);
|
|
|
}
|
|
|
catch (Exception e1)
|
|
|
{
|
|
|
|
|
|
}
|
|
|
}
|
|
|
mut.ReleaseMutex();
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
|
|
|
}
|
|
|
//unlock
|
|
|
|
|
|
}
|
|
|
/*
|
|
|
*方法名称:writeHexString
|
|
|
*创建人:韩荣伟
|
|
|
*创建时间:2010-10-30
|
|
|
*参数描述:string logstr 日志字符串
|
|
|
* string toHex 待转16进制码的字符串
|
|
|
*返回描述:void
|
|
|
*功能描述:写入日志字符串,和转16进制码的字符串
|
|
|
*/
|
|
|
public void writeHexString(string logstr, byte[] toHex, int nLogLevel)
|
|
|
{
|
|
|
if (nLogLevel < nLogLvl_Min || logOn == false)
|
|
|
return;
|
|
|
|
|
|
|
|
|
//lock
|
|
|
try
|
|
|
{
|
|
|
mut.WaitOne();
|
|
|
{
|
|
|
CheckFSize(nLogLevel);
|
|
|
|
|
|
//format
|
|
|
dtNow = DateTime.Now;
|
|
|
log = dtNow.ToString("yyyy-MM-dd HH:mm:ss ", CultureInfo.InvariantCulture);
|
|
|
log += logstr;// +"\r\n";
|
|
|
//open file //write //close file
|
|
|
//FileInfo fi
|
|
|
try
|
|
|
{
|
|
|
//using (FileStream fs
|
|
|
// = File.Open((nLogLevel == LOGLVL_ERROR ? sExceptionPath : sPath),
|
|
|
// FileMode.OpenOrCreate | FileMode.Append))
|
|
|
//{
|
|
|
string Hexstr = BitConverter.ToString(toHex).Replace("-", " ");
|
|
|
|
|
|
// Byte[] info = new UTF8Encoding(true).GetBytes(log + Hexstr + "\r\n");
|
|
|
// //// Add some information to the file.
|
|
|
// fs.Write(info, 0, info.Length);
|
|
|
// fs.Close();
|
|
|
//}
|
|
|
Msg msg = new Msg(log + Hexstr, nLogLevel == LOGLVL_ERROR ? sExceptionPath : sPath);
|
|
|
logWrite.Write(msg);
|
|
|
}
|
|
|
catch (Exception e1)
|
|
|
{
|
|
|
|
|
|
}
|
|
|
}
|
|
|
mut.ReleaseMutex();
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
/*
|
|
|
*方法名称:DeleteAtFolder
|
|
|
*创建人:韩荣伟
|
|
|
*创建时间:2010-10-30
|
|
|
*参数描述:string dir 目录, string sFlag 文件前缀
|
|
|
*返回描述:void
|
|
|
*功能描述:删除过期文件
|
|
|
*/
|
|
|
|
|
|
public void DeleteAtFolder(string dir, string sFlag)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
if (Directory.Exists(dir)) //如果存在这个文件夹删除之
|
|
|
{
|
|
|
string sBase = sFlag + "_000000_000.txt";
|
|
|
foreach (string d in Directory.GetFileSystemEntries(dir))
|
|
|
{
|
|
|
if (File.Exists(d))
|
|
|
{
|
|
|
string sFileName = Path.GetFileName(d);
|
|
|
if (sFileName.IndexOf("backup") > 0)
|
|
|
{
|
|
|
if (sFileName.Remove(0, 10).CompareTo(sBase) < 0)
|
|
|
{
|
|
|
File.Delete(d); //直接删除其中的文件
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
DeleteAtFolder(d, sFlag); //递归删除子文件夹
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
catch (Exception e1)
|
|
|
{
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}
|