| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- 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 对外使用的公用方法
- /// <summary>
- /// 以Debug级别输出日志信息
- /// </summary>
- /// <param name="p_fileName">日志文件名称</param>
- /// <param name="p_logType">日志类型 true 记录日志,False 不记录日志</param>
- /// <param name="p_info">要输出的信息</param>
- 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 { }
- }
- /// <summary>
- /// 以Info级别输出日志信息
- /// </summary>
- /// <param name="p_fileName">日志文件名称</param>
- /// <param name="p_logType">日志类型 true 记录日志,False 不记录日志</param>
- /// <param name="p_info">要输出的信息</param>
- 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 { }
- }
- /// <summary>
- /// 以Warn级别输出日志信息
- /// </summary>
- /// <param name="p_fileName">日志文件名称</param>
- /// <param name="p_logType">日志类型 true 记录日志,False 不记录日志</param>
- /// <param name="p_info">要输出的信息</param>
- 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 { }
- }
- /// <summary>
- /// 以Error级别输出日志信息
- /// </summary>
- /// <param name="p_fileName">日志文件名称</param>
- /// <param name="p_info">要输出的信息</param>
- 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 { }
- }
- /// <summary>
- /// 以Fatal级别输出日志信息
- /// </summary>
- /// <param name="p_fileName">日志文件名称</param>
- /// <param name="p_info">要输出的信息</param>
- 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 { }
- }
- /// <summary>
- /// 创建SQL语句,覆盖文件
- /// </summary>
- /// <param name="p_fileName">文件名</param>
- /// <param name="p_sql">要保存的SQL语句</param>
- public static void CreateSqlFile(string p_fileName, List<string> 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 { }
- }
- /// <summary>
- /// 删除对应的文件
- /// </summary>
- /// <param name="p_fileName">文件名称</param>
- public static void DeleteLogFile(string p_fileName)
- {
- DeleteFile(p_fileName);
- }
- /// <summary>
- /// 根据文件名获取对应的文件内容,并返回文件内容
- /// </summary>
- /// <param name="p_fileName"></param>
- /// <returns></returns>
- 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 + " 日志文件不存在!");
- }
- /// <summary>
- /// 获取文件长度
- /// </summary>
- /// <param name="p_fileName"></param>
- /// <returns></returns>
- 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 自定义方法
- /// <summary>
- /// 设置当前时间
- /// </summary>
- /// <returns>返回系统时间</returns>
- private static string GetNowDateTime()
- {
- DateTime dt = DateTime.Now;
- return dt.ToString("yyyy-MM-dd HH:mm:ss,fff");
- }
- /// <summary>
- /// 返回对应的文件全名
- /// </summary>
- /// <param name="p_fileName"></param>
- /// <returns>返回对应的文件全名</returns>
- 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;
- }
- /// <summary>
- /// 删除日志文件信息
- /// </summary>
- /// <param name="p_fileName"></param>
- 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
- }
- }
|