using System;
using System.IO;
namespace JackyHelper
{
/// <summary>
/// 登录日志帮助文件
/// </summary>
public class LogHelper
{
/// <summary>
/// 写日志
/// </summary>
/// <param name="message"></param>
public static void WriteLog(string message)
{
string path = AppDomain.CurrentDomain.BaseDirectory + @"\Log";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string childFolderName = "Log_" + DateTime.Now.ToString("yyyy_MM");
string childPath = path + @"\" + childFolderName;
if (!Directory.Exists(childPath))
{
Directory.CreateDirectory(childPath);
}
string fileName = DateTime.Now.ToString("yyyy-MM-dd") + ".log";
string logPath = childPath + @"\" + fileName;
using (StreamWriter sw = new StreamWriter(logPath, true))
{
sw.WriteLine("时间:{0}\t{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), message);
}
}
/// <summary>
/// 把异常信息写到日志中
/// </summary>
/// <param name="message"></param>
/// <param name="ex"></param>
public static void WriteLog(string message, Exception ex)
{
string path = AppDomain.CurrentDomain.BaseDirectory + @"\Log";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string childFolderName = "Log_" + DateTime.Now.ToString("yyyy_MM");
string childPath = path + @"\" + childFolderName;
if (!Directory.Exists(childPath))
{
Directory.CreateDirectory(childPath);
}
string fileName = DateTime.Now.ToString("yyyy-MM-dd") + ".log";
string logPath = childPath + @"\" + fileName;
using (StreamWriter sw = new StreamWriter(logPath, true))
{
sw.WriteLine("时间:{0}\t[{1}]:{2}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), message, ex.Message);
}
}
}
}
登录日志帮助文件
最新推荐文章于 2024-03-06 10:39:49 发布