You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

325 lines
11 KiB
C#

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
*
*LogFileLogFile
*LogFile
*/
//public static LogFile instance()
//{
// if (inst == null)
// {
// inst = new LogFile();
// }
// return inst;
//}
/*
*SetLogOn
*
*2010-10-30
*bool on logtrue 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)
{
}
}
}
}