A Simple Log Writer And Log Searching Class
2012-04-01 14:52
246 查看
using System; using System.Collections.Generic; using System.Text; using System.IO; class clsLog { private StringBuilder strLog; private string strFileName = string.Empty; //Initialize Logging //############################################################################################################################### public void StartLogging(string strLogFileName) { strLog = new StringBuilder(); strLog.Append("Logging Started At : [" + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt") + "]" + Environment.NewLine + Environment.NewLine); strFileName = strLogFileName; } //############################################################################################################################### //Write Log Entry //############################################################################################################################### public void WriteLogEntry(string strEntry) { strLog.Append("[" + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt") + "] " + strEntry + Environment.NewLine); } //############################################################################################################################### //New Line //############################################################################################################################### public void NewLine() { strLog.Append(Environment.NewLine); } //############################################################################################################################### //Stop Logging //############################################################################################################################### public void StopLogging() { strLog.Append(Environment.NewLine + "Logging Stopped At : [" + DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss tt") + "]" + Environment.NewLine); if (System.IO.File.Exists(strFileName)) { using (StreamWriter sw = File.AppendText(strFileName)) { sw.Write(strLog.ToString()); sw.Close(); } } else { System.IO.File.WriteAllText(strFileName,strLog.ToString()); } } //############################################################################################################################### //Search Log //############################################################################################################################### public string SearchLog(string strLogFileName, DateTime dtFrom, DateTime dtTo) { if (!File.Exists(strLogFileName)) { return null; } string strReturnString = string.Empty; using (StreamReader sr = new StreamReader(strLogFileName)) //strFileName { String line; while ((line = sr.ReadLine()) != null) { DateTime strDateTime; if (line.Length > 22) { DateTime.TryParse(line.Substring(0, 25).Replace('[', ' ').Replace(']', ' ').Trim(), out strDateTime); if (strDateTime >= dtFrom && strDateTime
相关文章推荐
- XML class for processing and building simple XML documents
- WinForm C#: Simple Runtime Control ReSizing and Dragging & Moving Class
- Compound class names are not supported. Consider searching for one class name and filtering the results
- Learning_log—struct and class
- log and searching
- A very simple MFC class to Encode and Decode an url string
- C++ class template(simple and clear definition)
- Java Class 介绍 canonical name, simple name and class name
- sqlsrv.class.php A simple and convenient php sqlsrv class
- WinForm C#: Simple Runtime Control ReSizing and Dragging & Moving Class
- A simple HTTP PHP class to crawl a URL for internal and external URLs
- 根据学习建立一个Simple Hibernate with Log4J and Junit Log
- JAVA Some Simple Class And Method Use.
- WinForm C#: Simple Runtime Control ReSizing and Dragging & Moving Class
- How to read Android crash log and stack trace
- logandfileandconfigandconverion
- a simple search with AngularJS and PHP
- java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter. <init>(Z)V
- DriverManager setLogWriter
- java class and object initialization--java类和对象初始化