编写操作日志类的方法
来源:互联网 发布:大数据的好处 编辑:程序博客网 时间:2024/06/05 02:29
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.IO;namespace Discuz.WebService{ public class LogWrite { private string filePath = null; public LogWrite() { try { string Dir = HttpContext.Current.Server.MapPath("~/Log/"); if (!Directory.Exists(Dir)) { Directory.CreateDirectory(Dir); } filePath = Dir + DateTime.Now.ToString("yyyy-MM-dd") + ".log"; } catch (Exception) { } } /// <summary> /// 写入错误信息 /// </summary> /// <param name="ex"></param> public void WriteLog(Exception ex, string IP) { using (StreamWriter sw = new StreamWriter(filePath, true, System.Text.Encoding.UTF8)) { sw.WriteLine(string.Format("时间:{0} 错误原因:{1}", DateTime.Now.ToString(), ex.Message)); sw.WriteLine(string.Format("用户IP:{0}", IP)); sw.WriteLine("错误信息:"); sw.WriteLine(ex.StackTrace); sw.WriteLine("-----------------------------------------------------------------------------------------------"); sw.WriteLine(""); sw.Flush(); sw.Close(); } } /// <summary> /// 写入错误信息 /// </summary> /// <param name="ex"></param> public void WriteLog(string msg, string IP) { using (StreamWriter sw = new StreamWriter(filePath, true, System.Text.Encoding.UTF8)) { sw.WriteLine(string.Format("时间:{0} 错误原因:{1}", DateTime.Now.ToString(), msg)); sw.WriteLine(string.Format("用户IP:{0}", IP)); sw.WriteLine("-----------------------------------------------------------------------------------------------"); sw.WriteLine(""); sw.Flush(); sw.Close(); } } }}