LogHelper

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

namespace MvcApplication1.Common
{
    public class LogHelper
    {
        public static void WriteError(Exception ex)
        {
            string path = HttpContext.Current.Server.MapPath("~/Logs/" + DateTime.Now.ToString("yyyy-MM-dd") + ".text");
            FileStream fs;
            if (File.Exists(path))
            {
                fs = new FileStream(path, FileMode.Append, FileAccess.Write);
            }
            else
            {
                fs = File.Create(path);
            }
            string errorinfo = "日期:" + DateTime.Now.ToString("hh:mm:ss") + "
" + "摘要:" + ex.Message + "
" + "详细信息:" + ex.StackTrace;
            byte[] bit = System.Text.Encoding.UTF8.GetBytes(errorinfo);
            fs.Write(bit, 0, bit.Length);
            fs.Close();
            fs.Dispose();

        }
    }
}
原文地址:https://www.cnblogs.com/jbdxbl/p/9416397.html