日志记录到txt文件

时间:2014-06-06 07:34:37   收藏:0   阅读:311

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
CreateErrorLog("test");
}
private static string m_fileName = System.Web.HttpContext.Current.Server.MapPath("~/Error/" + DateTime.Now.ToString("yyyyMMdd") + ".log");
public static String FileName
{
get { return (m_fileName); }
set { if (value != null || value != "") { m_fileName = value; } }
}

public static void CreateErrorLog(string message)
{
if (File.Exists(m_fileName))
{
///如果日志文件已经存在,则直接写入日志文件
StreamWriter sr = File.AppendText(FileName);
sr.WriteLine("\n");
sr.WriteLine(DateTime.Now.ToString() + message);
sr.Close();
}
else
{
///创建日志文件
StreamWriter sr = File.CreateText(FileName);
sr.WriteLine("\n");
sr.WriteLine(DateTime.Now.ToString() + message);
sr.Close();
}
}
}
}

日志记录到txt文件,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!