C# 写入日志文件

一:Windows服务、控制台日志方法

public void WriteLog(string text)
{

string path = AppDomain.CurrentDomain.BaseDirectory;
path = System.IO.Path.Combine(path
, "Logs\\");

if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
string fileFullName = System.IO.Path.Combine(path
, string.Format("{0}.txt", DateTime.Now.ToString("yyyyMMdd-HHmm")));


using (StreamWriter output = System.IO.File.AppendText(fileFullName))
{
output.WriteLine(text);

output.Close();
}
}

 

二:Web写日志方法

 

 

public class WriteLog
{
public static void Write(string title, string content)
{
string sFilePath = HttpContext.Current.Server.MapPath("\\ErrorLog");
if (Directory.Exists(sFilePath) == false) //工程目录下 Log目录 '目录是否存在,为true则没有此目录
{
Directory.CreateDirectory(sFilePath); //建立目录 Directory为目录对象
}

 

//string sDirPath = Path.GetDirectoryName(sFilePath);
//sDirPath += @"\ErrorLog";

 

string sFname = sFilePath + @"\" + title + "-" + System.DateTime.Now.ToString("yyyyMMdd") + ".txt";

 

using (FileStream stream = new FileStream(sFname, FileMode.Append))
using (StreamWriter writer = new StreamWriter(stream))
{
writer.WriteLine("{0}-{1}", DateTime.Now.ToString(), content);
}
}

 

#region 创建错误日志
///-----------------------------------------------------------------------------
/// <summary>创建错误日志 在c:\ErrorLog\</summary>
/// <param name="strFunctionName">strFunctionName,调用方法名</param>
/// <param name="strErrorNum">strErrorNum,错误号</param>
/// <param name="strErrorDescription">strErrorDescription,错误内容</param>
/// <returns></returns>
public static void ErrorLog(string strFunctionName, string strErrorNum, string strErrorDescription, string content)
{
string strPath; //错误文件的路径
DateTime dt = DateTime.Now;
try
{
strPath = HttpContext.Current.Server.MapPath("\\ErrorLog"); //返回网站根目录下查找errolog文件夹地址
//strPath = "c:" + "\\ErrorLog";//暂时放在c:下

 

if (Directory.Exists(strPath) == false) //工程目录下 Log目录 '目录是否存在,为true则没有此目录
{
Directory.CreateDirectory(strPath); //建立目录 Directory为目录对象
}
strPath = strPath + "\\" + dt.ToString("yyyyMMdd");

 

if (Directory.Exists(strPath) == false) //目录是否存在 '工程目录下 Log\月 目录 yyyymm
{
Directory.CreateDirectory(strPath); //建立目录//日志文件,以 日 命名
}
strPath = strPath + "\\" + dt.ToString("yyyyMMdd") + ".txt";

 

 

 

StreamWriter FileWriter = new StreamWriter(strPath, true); //创建日志文件

 

FileWriter.WriteLine("---------------------------------------日志开始-----------------------------------------------------------");
FileWriter.WriteLine("时间: " + dt.ToString("HH:mm:ss"));
FileWriter.WriteLine("方法名: " + strFunctionName);
FileWriter.WriteLine("错误代码: " + strErrorNum);
FileWriter.WriteLine("错误内容: " + strErrorDescription.Replace("\r", ""));
FileWriter.WriteLine("执行内容: " + content.Replace("\r", ""));
//FileWriter.WriteLine("时间: " + dt.ToString("HH:mm:ss") + " 日志内容: " + strMatter);
FileWriter.WriteLine("---------------------------------------日志结束-----------------------------------------------------------");
FileWriter.Close(); //关闭StreamWriter对象
}
catch (Exception ex)
{
//("写错误日志时出现问题,请与管理员联系! 原错误:" + strMatter + "写日志错误:" + ex.Message.ToString());
string str = ex.Message.ToString();
}
}

 

#endregion
}
}

 

posted @ 2016-03-18 10:45  似曾相识燕归来  阅读(6313)  评论(0编辑  收藏  举报