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#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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 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)
{
}
}
}
}