记录错误信息到TXT文件中

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace WindowsFormsApplication1
{
    /// <summary>
    /// WriteInLog 的摘要说明。
    /// </summary>
    public class WriteInLog
    {
        private string logFileName;
        private int logFileSizes;

        /// <summary>
        /// 写入日志文件
        /// </summary>
        public WriteInLog()
        {
            logFileName = @"~/ErrorLog/logfile.txt";
        }

        /// <summary>
        /// 自动删除日志文件大小,此方法已经重载.
        /// </summary>
        /// <param name="fileSize">日志文件大小,单位KB</param>
        public WriteInLog(int fileSize)
            : this()
        {
            if (fileSize != 0)
            {
                this.logFileSizes = fileSize * 1024;
            }
            else
            {
                this.logFileSizes = 1024;
            }
        }
        /// <summary>
        /// 日志文件完全名,如:@"e:\logfile.txt"
        /// </summary>
        public string LogFileName
        {
            set
            {
                this.logFileName = value;
            }
        }

        /// <summary>
        /// 写入日志信息
        /// </summary>
        /// <param name="msg">日志内容</param>
        /// <param name="IsAutoDelete">是否自动删除日志</param>
        public void writeInLog(string msg)
        {
            if (logFileSizes != 0)
            {
                writeInLog(msg, true);
            }
            else
            {
                writeInLog(msg, false);
            }
        }

        /// <summary>
        /// 写入日志信息
        /// </summary>
        /// <param name="msg">日志内容</param>
        /// <param name="IsAutoDelete">是否自动删除日志</param>
        private void writeInLog(string msg, bool IsAutoDelete)
        {
            try
            {
                logFileName = System.Web.HttpContext.Current.Server.MapPath(logFileName);
                if (!File.Exists(logFileName))
                    File.Create(logFileName);
                FileInfo fileinfo = new FileInfo(logFileName);
                if (IsAutoDelete)
                {
                    if (fileinfo.Exists && fileinfo.Length >= logFileSizes)
                    {
                        fileinfo.Delete();
                    }
                }
                using (FileStream fs = fileinfo.OpenWrite())
                {
                    StreamWriter sw = new StreamWriter(fs);
                    sw.BaseStream.Seek(0, SeekOrigin.End);
                    sw.WriteLine("=====================================");
                    sw.Write("添加日期为:" + DateTime.Now.ToString() + "\r\n");
                    sw.Write("日志内容为:" + msg + "\r\n");
                    sw.WriteLine("=====================================");
                    sw.Flush();
                    sw.Close();
                }
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }

    }

}

 

Error.aspx页:

public partial class Error : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Context.Error != null)
        {
            //错误信息
            string errInfo = Context.Error.Message;
            string SouPage=Request.Url.ToString();
            WindowsFormsApplication1.WriteInLog write=new WindowsFormsApplication1.WriteInLog();
            write.writeInLog(errInfo + SouPage);
            //清除错误信息
            Context.ClearError();
        }
    }
}

 

 

全局页:

 

    void Application_Error(object sender, EventArgs e)
    {
        Server.Transfer("~/Error.aspx");

    }

posted @ 2011-12-28 15:21  踏浪帅  阅读(234)  评论(0编辑  收藏  举报