自定義Log 寫到文件中
阿新 • • 發佈:2018-04-14
public stream ndt generic file alt IT ram --
- 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="action">動作</param>
- /// <param name="strMessage">日誌內容</param>
- /// <param name="time">時間</param>
- public static void WriteTextLog(string action, string strMessage, DateTime time)
- {
- string path = AppDomain.CurrentDomain.BaseDirectory + @"System\Log\";
- 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: " + action + "\r\n");
- 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();
- }
- }
自定義Log 寫到文件中