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

C# 创建文件日志记录

2013-07-12 11:35 357 查看
#region 写错误日志
/// <summary>
/// 写错误日志
/// </summary>
/// <param name="errorMsg"></param>
/// <param name="LogPath">文件夹后面需要加上" / "符号,如:"../ErrorLog/"</param>
public static void WriteErrorLog(string errorMsg, string LogPath)
{
Common.WriteErrorLog(errorMsg, LogPath);
}
#endregion


在Web端调用日志记录方法:

try
{
}
catch (Exception ex)
{
WriteErrorLog("消息内容" + ex.Message.Replace(Environment.NewLine, string.Empty), HttpContext.Current.Server.MapPath("ErrorLog\\"));
}

#region 写错误日志
/// <summary>
/// 写错误日志
/// </summary>
/// <param name="errorMsg"></param>
/// <param name="LogPath">日志路径(仅文件夹)</param>
public static void WriteErrorLog(string errorMsg, string LogPath)
{
try
{
string FileName = String.Format("{0:yyyyMMdd}", DateTime.Now) + ".txt";
string FileFullPath = LogPath + FileName;
string WriteText = string.Empty;
if (File.Exists(FileFullPath))
{
StreamWriter writer = new StreamWriter(FileFullPath, true, System.Text.Encoding.GetEncoding("GB2312"));
writer.WriteLine(String.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now) + "  >>>>>>  " + errorMsg);
writer.Close();
}
else
{
FileStream NewText = File.Create(FileFullPath);
NewText.Close();
StreamWriter writer = new StreamWriter(FileFullPath, true, System.Text.Encoding.GetEncoding("GB2312"));
writer.WriteLine(String.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now) + ">>>>>>" + errorMsg);
writer.Close();
}
}
catch
{
}
}
#endregion
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C# 错误日志