微信支付自帶的簡易log
阿新 • • 發佈:2018-06-20
lena code pat ram log err nbsp PC deb
using System; using System.Collections.Generic; using System.Web; using System.IO; namespace WxPayAPI { public class Log { //在網站根目錄下創建日誌目錄 public static string path = HttpContext.Current.Request.PhysicalApplicationPath + "logs"; /** * 向日誌文件寫入調試信息 * @param className 類名 * @param content 寫入內容*/ public static void Debug(string className, string content) { if(WxPayConfig.LOG_LEVENL >= 3) { WriteLog("DEBUG", className, content); } } /** * 向日誌文件寫入運行時信息 * @param className 類名 * @param content 寫入內容*/ public static void Info(string className, string content) { if (WxPayConfig.LOG_LEVENL >= 2) { WriteLog("INFO", className, content); } } /** * 向日誌文件寫入出錯信息 * @param className 類名 * @param content 寫入內容*/ public static void Error(string className, string content) { if(WxPayConfig.LOG_LEVENL >= 1) { WriteLog("ERROR", className, content); } } /** * 實際的寫日誌操作 * @param type 日誌記錄類型 * @param className 類名 * @param content 寫入內容 */ protected static void WriteLog(string type, string className, string content) { if(!Directory.Exists(path))//如果日誌目錄不存在就創建 { Directory.CreateDirectory(path); } string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");//獲取當前系統時間 string filename = path + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";//用日期對日誌文件命名 //創建或打開日誌文件,向日誌文件末尾追加記錄 StreamWriter mySw = File.AppendText(filename); //向日誌文件寫入內容 string write_content = time + " " + type + " " + className + ": " + content; mySw.WriteLine(write_content); //關閉日誌文件 mySw.Close(); } } }
public class WxPayConfig { //=======【日誌級別】=================================== /* 日誌等級,0.不輸出日誌;1.只輸出錯誤信息; 2.輸出錯誤和正常信息; 3.輸出錯誤信息、正常信息和調試信息 */ public const int LOG_LEVENL = 0; }
微信支付自帶的簡易log