asp.net寫入日誌到文本文件

[csharp] view plain copyui

  1. using System;  spa

  2. using System.Collections.Generic;  .net

  3. using System.Web;  日誌

  4. using System.IO;  blog

  5. using System.Text;  ip

  6.   

  7. /// <summary>  get

  8. /// Summary description for NetLog  string

  9. /// </summary>  it

  10. public class NetLog  io

  11. {  

  12.     /// <summary>  

  13.     /// 寫入日誌到文本文件  

  14.     /// </summary>  

  15.     /// <param name="action">動做</param>  

  16.     /// <param name="strMessage">日誌內容</param>  

  17.     /// <param name="time">時間</param>  

  18.     public static void WriteTextLog(string action, string strMessage, DateTime time)  

  19.     {  

  20.         string path = AppDomain.CurrentDomain.BaseDirectory + @"System\Log\";  

  21.         if (!Directory.Exists(path))  

  22.             Directory.CreateDirectory(path);  

  23.   

  24.         string fileFullPath = path + time.ToString("yyyy-MM-dd") + ".System.txt";  

  25.         StringBuilder str = new StringBuilder();  

  26.         str.Append("Time:    " + time.ToString() + "\r\n");  

  27.         str.Append("Action:  " + action + "\r\n");  

  28.         str.Append("Message: " + strMessage + "\r\n");  

  29.         str.Append("-----------------------------------------------------------\r\n\r\n");  

  30.         StreamWriter sw;  

  31.         if (!File.Exists(fileFullPath))  

  32.         {  

  33.             sw = File.CreateText(fileFullPath);  

  34.         }  

  35.         else  

  36.         {  

  37.             sw = File.AppendText(fileFullPath);  

  38.         }  

  39.         sw.WriteLine(str.ToString());  

  40.         sw.Close();  

  41.     }  

  42. }  

相關文章
相關標籤/搜索