using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Configuration;
namespace Dongke.IBOSS.PRD.WCF.Hosting
{
public static class LogFileOperation
{
#region 对外使用的公用方法
///
/// 以Debug级别输出日志信息
///
/// 日志文件名称
/// 日志类型 true 记录日志,False 不记录日志
/// 要输出的信息
public static void Debug(string p_fileName, bool p_logType, string p_info)
{
try
{
if (p_logType)
{
if (!string.IsNullOrEmpty(p_fileName))
{
string strDateTime = GetNowDateTime();
string strFileFullName = GetCreateFile(p_fileName);
using (StreamWriter sw = new StreamWriter(strFileFullName, true, System.Text.Encoding.UTF8))
{
sw.WriteLine(strDateTime + "(Debug) - " + p_info);
sw.Close();
}
}
}
}
catch { }
}
///
/// 以Info级别输出日志信息
///
/// 日志文件名称
/// 日志类型 true 记录日志,False 不记录日志
/// 要输出的信息
public static void Info(string p_fileName, bool p_logType, string p_info)
{
try
{
if (p_logType)
{
if (!string.IsNullOrEmpty(p_fileName))
{
string strDateTime = GetNowDateTime();
string strFileFullName = GetCreateFile(p_fileName);
using (StreamWriter sw = new StreamWriter(strFileFullName, true, System.Text.Encoding.UTF8))
{
sw.WriteLine(strDateTime + "(Info) - " + p_info);
sw.Close();
}
}
}
}
catch { }
}
///
/// 以Warn级别输出日志信息
///
/// 日志文件名称
/// 日志类型 true 记录日志,False 不记录日志
/// 要输出的信息
public static void Warn(string p_fileName, bool p_logType, string p_info)
{
try
{
if (p_logType)
{
if (!string.IsNullOrEmpty(p_fileName))
{
string strDateTime = GetNowDateTime();
string strFileFullName = GetCreateFile(p_fileName);
using (StreamWriter sw = new StreamWriter(strFileFullName, true, System.Text.Encoding.UTF8))
{
sw.WriteLine(strDateTime + "(Warn) - " + p_info);
sw.Close();
}
}
}
}
catch { }
}
///
/// 以Error级别输出日志信息
///
/// 日志文件名称
/// 要输出的信息
public static void Error(string p_fileName, string p_info)
{
try
{
if (!string.IsNullOrEmpty(p_fileName))
{
string strDateTime = GetNowDateTime();
string strFileFullName = GetCreateFile(p_fileName);
using (StreamWriter sw = new StreamWriter(strFileFullName, true, System.Text.Encoding.UTF8))
{
sw.WriteLine(strDateTime + "(Error) - " + p_info);
sw.Close();
}
}
}
catch { }
}
///
/// 以Fatal级别输出日志信息
///
/// 日志文件名称
/// 要输出的信息
public static void Fatal(string p_fileName, string p_info)
{
try
{
if (!string.IsNullOrEmpty(p_fileName))
{
string strDateTime = GetNowDateTime();
string strFileFullName = GetCreateFile(p_fileName);
using (StreamWriter sw = new StreamWriter(strFileFullName, true, System.Text.Encoding.UTF8))
{
sw.WriteLine(strDateTime + "(Fatal) - " + p_info);
sw.Close();
}
}
}
catch { }
}
///
/// 创建SQL语句,覆盖文件
///
/// 文件名
/// 要保存的SQL语句
public static void CreateSqlFile(string p_fileName, List p_sql)
{
try
{
if (!string.IsNullOrEmpty(p_fileName))
{
DeleteFile(p_fileName);
string strFileFullName = GetCreateFile(p_fileName);
using (StreamWriter sw = new StreamWriter(strFileFullName, false, System.Text.Encoding.UTF8))
{
foreach (string strSql in p_sql)
{
sw.WriteLine(strSql);
}
sw.Close();
}
}
}
catch { }
}
///
/// 删除对应的文件
///
/// 文件名称
public static void DeleteLogFile(string p_fileName)
{
DeleteFile(p_fileName);
}
///
/// 根据文件名获取对应的文件内容,并返回文件内容
///
///
///
public static byte[] ReadFile(string p_fileName)
{
string filePath = ConfigConst.StartupPath + "\\Log\\";
try
{
string strFileName = p_fileName;
if (!p_fileName.Contains("."))
strFileName = p_fileName + ".log";
filePath = filePath + strFileName;
if (File.Exists(filePath))
{
FileStream fsMyfile = new FileStream(filePath, FileMode.Open, FileAccess.Read);
byte[] byteFile = new byte[fsMyfile.Length];
BinaryReader brMyfile = new BinaryReader(fsMyfile, Encoding.UTF8);
brMyfile.Read(byteFile, 0, byteFile.Length);
brMyfile.Close();
fsMyfile.Close();
fsMyfile.Dispose();
return byteFile;
}
}
catch (Exception ex)
{
throw ex;
}
return System.Text.Encoding.UTF8.GetBytes(filePath + " 日志文件不存在!");
}
///
/// 获取文件长度
///
///
///
public static long GetFileSize(string p_fileName)
{
string filePath = ConfigConst.StartupPath + "\\Log\\";
try
{
string strFileName = p_fileName;
if (!p_fileName.Contains("."))
strFileName = p_fileName + ".log";
filePath = filePath + strFileName;
if (File.Exists(filePath))
{
FileStream fsMyfile = new FileStream(filePath, FileMode.Open, FileAccess.Read);
long fileSize = fsMyfile.Length;
fsMyfile.Close();
fsMyfile.Dispose();
return fileSize;
}
}
catch (Exception ex)
{
throw ex;
}
return 0;
}
#endregion
#region 自定义方法
///
/// 设置当前时间
///
/// 返回系统时间
private static string GetNowDateTime()
{
DateTime dt = DateTime.Now;
return dt.ToString("yyyy-MM-dd HH:mm:ss,fff");
}
///
/// 返回对应的文件全名
///
///
/// 返回对应的文件全名
private static string GetCreateFile(string p_fileName)
{
string filePath = ConfigConst.StartupPath + "\\Log\\";
DirectoryInfo dirFile = new DirectoryInfo(filePath);
if (!dirFile.Exists)
{
dirFile.Create();
}
string strFileName = p_fileName;
if (!p_fileName.Contains("."))
{
strFileName = p_fileName + ".log";
}
//if (strFileName.Contains(ConfigConst.LogFileNameOld))
//{
// string strTempFileAllName = filePath + strFileName;
// if (System.IO.File.Exists(strTempFileAllName))
// {
// System.IO.FileInfo fileSize = new System.IO.FileInfo(strTempFileAllName);
// if (fileSize.Length > (20 * 1024 * 1024)) //大于20M
// {
// strFileName = ConfigConst.LogFileNameOld + DateTime.Now.ToString("yyyyMMddHHmmss");
// ConfigConst.LogFileName = strFileName;
// strFileName = strFileName + ".log";
// }
// }
//}
return filePath + strFileName;
}
///
/// 删除日志文件信息
///
///
private static void DeleteFile(string p_fileName)
{
try
{
string filePath = ConfigConst.StartupPath + "\\Log\\";
string strFileName = p_fileName;
if (!p_fileName.Contains("."))
strFileName = p_fileName + ".log";
filePath = filePath + strFileName;
if (File.Exists(filePath))
System.IO.File.Delete(filePath);
}
catch { }
}
#endregion
}
}