整理的日志类

#region 日志分类
/// <summary>
/// 写入日志
/// </summary>
/// <param name="type">日志类型</param>
/// <param name="message">日志内容</param>
public static void Writelog(string type, string message)
{
string logContent = string.Format("[{0}] =>{1}", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), message);
//自定修改-by-wgh-2017-11-20
Isexist();
IsexistType(type);
string errLogFilePath = HttpRuntime.AppDomainAppPath.ToString() + @"\Log\" + @"\\" + type + "\\" + type.Trim() + DateTime.Now.ToString("yyyyMMdd") + ".txt";
StreamWriter sw;
if (!File.Exists(errLogFilePath))
{
FileStream fs1 = new FileStream(errLogFilePath, FileMode.Create, FileAccess.Write);
sw = new StreamWriter(fs1);
}
else
{
sw = new StreamWriter(errLogFilePath, true);
}
sw.WriteLine(logContent);
sw.Flush();
sw.Close();
}
#endregion

#region 判断是否存在日志文件
// 判断是否存在日志文件
private static void Isexist()
{
// string path = Environment.CurrentDirectory + @"\Log\";//winform项目
string path = HttpRuntime.AppDomainAppPath.ToString() + @"\Log\";//web项目
if (!File.Exists(path))
{
Directory.CreateDirectory(path);
}
}

private static void IsexistType(string Type)
{
// string path = Environment.CurrentDirectory + @"\Log\";
string path = HttpRuntime.AppDomainAppPath.ToString() + @"\Log\" + @"\\" + Type + "\\";//web项目
if (!File.Exists(path))
{
Directory.CreateDirectory(path);
}
}
#endregion

posted @ 2017-11-21 17:30  .net&new  阅读(106)  评论(0编辑  收藏  举报