C#写日志文件帮助类
1、日志帮助类:
using System; using System.Collections.Generic; using System.Web; using System.IO; using System.Text; /// <summary> /// Summary description for NetLog /// </summary> public class NetLog { /// <summary> /// 写入日志到文本文件 /// </summary> /// <param name="strMessage">日志内容</param> public static void WriteTextLog(string strMessage) { string path = AppDomain.CurrentDomain.BaseDirectory + @"Log\"; if (!Directory.Exists(path)) Directory.CreateDirectory(path); DateTime time = DateTime.Now; string fileFullPath = path + time.ToString("yyyy-MM-dd") + ".System.txt"; StringBuilder str = new StringBuilder(); str.Append("Time:"+ time + ";Message: " + strMessage + "\r\n"); StreamWriter sw; if (!File.Exists(fileFullPath)) { sw = File.CreateText(fileFullPath); } else { sw = File.AppendText(fileFullPath); } sw.WriteLine(str.ToString()); sw.Close(); } }
2、调用方法:
NetLog.WriteTextLog("异常错误信息:"+ex.Message);
当你的才华还撑不起你的野心时,那你就应该静下心来学习;当你的能力还驾驭不了你的目标时,那就应该沉下心来历练!