C# LoggingHelp 自定义记录日志帮助类
public class LoggingHelper
{
/// <summary>
/// 获取文件路径
/// </summary>
/// <returns></returns>
public static string GetLogFilePath()
{
string log_file_path = OPOConfig.GetValue("Log_File_Path");
if (log_file_path == null)
{
log_file_path = AppDomain.CurrentDomain.BaseDirectory + "\\Content\\Log\\";
}
return log_file_path;
}
/// <summary>
/// 写入日志文件
/// </summary>
/// <param name="logType"></param>
/// <param name="message"></param>
/// <param name="writeType"></param>
/// <param name="logFileName"></param>
public static void WriteLog(string logType, string message, int writeType = 0, string logFileName = "")
{
if (string.IsNullOrWhiteSpace(logFileName))
{
logFileName = DateTime.Now.Date.ToString("yyyyMMdd");
}
string strlogpath = GetLogFilePath() + "\\日志文件" + logFileName+"\\";
if (!Directory.Exists(strlogpath))//判断文件夹是否存在
{
Directory.CreateDirectory(strlogpath); //创建新文件夹
}
try
{
DeleteFile(strlogpath, 30);
DateTime now = DateTime.Now;//现在
string fileName = string.Empty;//文件名
fileName += DateTime.Today.ToString("yyyyMMdd").Replace("/", "").Replace("-", "").Replace("\\", "").Replace(" ", "").Replace(":", "").Replace(":", "") + "_" + DateTime.Now.Hour + ".log";
string fullPath = strlogpath + fileName;
while (IsFileInUse(fullPath))
{
System.Threading.Thread.Sleep(100);
}
using (StreamWriter output = new StreamWriter(fullPath, true, System.Text.Encoding.UTF8))
{
switch (writeType)
{
case 1:
output.WriteLine("\r\n");
output.WriteLine("-------------------开始:-" + now.ToString() + "-------------------------------");
output.WriteLine("类型:" + logType);
output.WriteLine("描述:" + message);
output.Close();
break;
case 2:
output.WriteLine("\r\n");
output.WriteLine("类型:" + logType);
output.WriteLine("描述:" + message);
output.Close();
break;
case 3:
output.WriteLine("类型:" + logType);
output.WriteLine("描述:" + message);
output.WriteLine("-------------------结束:-" + now.ToString() + "-------------------------------");
output.WriteLine("\r\n");
output.Close();
break;
default:
output.WriteLine("\r\n");
output.WriteLine("-------------------开始:-" + now.ToString() + "-------------------------------");
output.WriteLine("类型:" + logType);
output.WriteLine("描述:" + message);
output.WriteLine("-------------------结束:-" + now.ToString() + "-------------------------------");
output.Close();
break;
}
if (writeType == 0)
{
}
else
{
}
}
}
catch (Exception error)
{
WriteLogError(error.Message);//记录日志异常
}
}
private static void WriteLogError(string error)
{
string strlogpath = GetLogFilePath() + "LogError";
string fileName = DateTime.Now.ToString("yyyy-MM-dd") + ".log";
string fullPath = strlogpath + fileName;
if (!Directory.Exists(fullPath))//判断文件夹是否存在
{
Directory.CreateDirectory(fullPath); //创建新文件夹
}
try
{
while (IsFileInUse(fullPath))
{
System.Threading.Thread.Sleep(100);
}
DateTime now = DateTime.Now; //取现在
using (StreamWriter output = new StreamWriter(fullPath, true, System.Text.Encoding.UTF8))
{
output.WriteLine("----------------------------------------------------------------------------------");
output.WriteLine("Begin: " + now.ToString());
output.WriteLine("Type: Log Exception");
output.WriteLine("Description: " + error);
output.WriteLine("End");
output.Close(); //关闭
}
}
catch
{
}
}
/// <summary>
/// 文件是否被占用
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static bool IsFileInUse(string fileName)
{
bool inUse = true;
FileStream fs = null;
try
{
fs = new FileStream(fileName, FileMode.Open, FileAccess.Read,
FileShare.None);
inUse = false;
}
catch
{
}
finally
{
if (fs != null)
fs.Close();
}
return inUse;//true表示正在使用,false没有使用
}
/// <summary>
/// 删除文件夹
/// </summary>
/// <param name="fileDirect"></param>
/// <param name="saveDay"></param>
private static void DeleteFile(string fileDirect, int saveDay)
{
DateTime nowTime = DateTime.Now;
DirectoryInfo root = new DirectoryInfo(fileDirect);
DirectoryInfo[] dics = root.GetDirectories();//获取文件夹
FileAttributes attr = File.GetAttributes(fileDirect);
if (attr == FileAttributes.Directory)//判断是不是文件夹
{
foreach (DirectoryInfo file in dics)//遍历文件夹
{
TimeSpan t = nowTime - file.CreationTime; //当前时间 减去 文件创建时间
int day = t.Days;
if (day > saveDay) //保存的时间 ; 单位:天
{
Directory.Delete(file.FullName, true); //删除超过时间的文件夹
}
}
}
}
}