您的位置:首页 > 编程语言 > C#

一段c#写的记录日志方法,分月分日记录

2012-10-31 15:43 393 查看
public class Log
{
private static readonly object obj = new object();
/// <summary>
/// 操作日志
/// </summary>
/// <param name="s">日志能容</param>
public static void WriteLog(string title,string content)
{
WriteLogs(title, content, "操作日志");
}
/// <summary>
/// 错误日志
/// </summary>
/// <param name="s">日志内容</param>
public static void WriteError(string title,string content)
{
WriteLogs(title, content, "错误日志");
}

public static void WriteLogs(string title, string content, string type)
{
lock (obj)
{
string path = AppDomain.CurrentDomain.BaseDirectory;
if (!string.IsNullOrEmpty(path))
{
path = AppDomain.CurrentDomain.BaseDirectory + "log";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path = path + "\\" + DateTime.Now.ToString("yyMM");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path = path + "\\" + DateTime.Now.ToString("dd") + ".txt";
if (!File.Exists(path))
{
FileStream fs = File.Create(path);
fs.Close();
}
if (File.Exists(path))
{
StreamWriter sw = new StreamWriter(path, true, System.Text.Encoding.Default);
sw.WriteLine(DateTime.Now + " " + title);
sw.WriteLine("日志类型:" + type);
sw.WriteLine("详情:" + content);
sw.WriteLine("----------------------------------------");
sw.Close();
}
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐