[C#일반 클래스] 로깅 클래스

8996 단어
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web; namespace KTCommon.LOG { public class TraceLog { /// /// /// public static TraceLog m_Trace = new TraceLog((HttpRuntime.AppDomainAppId == null) ? "d://LOG" : HttpRuntime.AppDomainAppPath + "//", "KTGJ"); public string m_LogFilePath = "";// private string m_xmlPath = ""; //Log private string m_FileNamePrefix = ""; StreamWriter SW; public TraceLog(string filePath, string fileNamePrefix) { m_xmlPath = filePath; m_FileNamePrefix = fileNamePrefix; } #region // Log public void Trace(string tipMsg) { string nodeTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); Trace(nodeTime, tipMsg); tipMsg = null; } public void Trace(string nodeTime, string tipMsg) { try { // string strNowY = DateTime.Now.Year.ToString(); string strNowM = DateTime.Now.Month.ToString(); string strNowD = DateTime.Now.Day.ToString(); string strNowH = DateTime.Now.Hour.ToString(); string fileName = m_FileNamePrefix + "_" + strNowH + "0000.log"; string filePath = m_xmlPath + "\\LOG\\" + strNowY + "\\" + strNowM + "\\" + strNowD + "\\"; if (nodeTime != "") { nodeTime = "[" + nodeTime + "] "; } //LOG , if (Directory.Exists(filePath) == false) { Directory.CreateDirectory(filePath); } m_LogFilePath = filePath + fileName; // if (File.Exists(filePath + fileName) == false) { if (SW != null) { SW.Flush(); SW.Close(); } File.Create(filePath + fileName).Close(); SW = new StreamWriter(filePath + fileName, true, Encoding.UTF8); } // if (SW == null) { SW = new StreamWriter(filePath + fileName, true, Encoding.UTF8); } // log SW.WriteLine(nodeTime + tipMsg); // SW.Flush(); } catch (Exception ex) { System.Diagnostics.Debug.Print("TraceLog Error:" + ex.Message.ToString()); } } #endregion // Log } }
 
전재 대상:https://www.cnblogs.com/jhli/p/5912142.html

좋은 웹페이지 즐겨찾기