asp.net寫入日誌到文字檔案
阿新 • • 發佈:2019-01-22
reference:
http://blog.csdn.net/lz00728/article/details/7477910
- using System;
- using System.Collections.Generic;
- using System.Web;
- using System.IO;
- using System.Text;
- /// <summary>
- /// Summary description for NetLog
- /// </summary>
- publicclass NetLog
- {
- /// <summary>
- /// 寫入日誌到文字檔案
-
/// </summary>
- /// <param name="action">動作</param>
- /// <param name="strMessage">日誌內容</param>
- /// <param name="time">時間</param>
- publicstaticvoid WriteTextLog(string action, string strMessage, DateTime time)
- {
-
string path = AppDomain.CurrentDomain.BaseDirectory + @
- if (!Directory.Exists(path))
- Directory.CreateDirectory(path);
- string fileFullPath = path + time.ToString("yyyy-MM-dd") + ".System.txt";
- StringBuilder str = new StringBuilder();
- str.Append("Time: " + time.ToString() + "\r\n");
-
str.Append("Action: "
- str.Append("Message: " + strMessage + "\r\n");
- str.Append("-----------------------------------------------------------\r\n\r\n");
- StreamWriter sw;
- if (!File.Exists(fileFullPath))
- {
- sw = File.CreateText(fileFullPath);
- }
- else
- {
- sw = File.AppendText(fileFullPath);
- }
- sw.WriteLine(str.ToString());
- sw.Close();
- }
- }