File log writing and reading, file log writing and reading
Private static string logPath = @ "D: \ LogS \ Logs \";
Public static string FloderPath
{
Get
{
Return logPath;
}
Set
{
LogPath = value;
}
}
Private static object lockHelper = new object ();
// Write a page
Public ActionResult Logtext ()
{
Return View ();
}
// Write a page entry to submit text
Public ActionResult Add (string Text)
{
String textlog = Text;
// Write
Write ("test:", textlog );
String fileName = "test:" + "-" + DateTime. Now. ToString ("yyyy_MM_dd") + ". log ";
// Read
Var str = Read (fileName, "UTF-8 ");
Var Temp = new
{
Nsuccess = false,
Message = "read successfully written"
};
Return Json (Temp );
}
/// <Summary>
/// Write logs
/// </Summary>
/// <Param name = "LogType"> Log Type </param>
/// <Param name = "Strings"> message </param>
Public static bool Write (string LogType, string str)
{
Try
{
// String fileName = LogType + "-" + DateTime. Now. ToString ("yyyy_MM_dd") + ". log ";
If (! System. IO. Directory. Exists (FloderPath ))
{
System. IO. Directory. CreateDirectory (FloderPath );
}
Return Write (LogType, str, "UTF-8 ");
}
Catch
{
Return false;
}
}
/// <Summary>
/// Write the log gb2312 UTF-8
/// </Summary>
/// <Param name = "LogType"> Log Type </param>
/// <Param name = "str"> message </param>
/// <Param name = "encoding"> encoding gb2312 UTF-8 </param>
Public static bool Write (string LogType, string str, string encoding)
{
If (! System. IO. Directory. Exists (FloderPath ))
{
System. IO. Directory. CreateDirectory (FloderPath );
}
String fileName = LogType + "-" + DateTime. Now. ToString ("yyyy_MM_dd") + ". log ";
Bool _ isTrue = false;
Lock (lockHelper)
{
System. IO. FileStream f = null;
System. IO. StreamWriter f2 = null;
Try
{
If (! System. IO. file. exists (logPath + fileName) {f = System. IO. file. create (logPath + fileName); f. close (); f. dispose (); f = null ;}
F2 = new System. IO. StreamWriter (logPath + fileName, true, System. Text. Encoding. GetEncoding (encoding ));
F2.WriteLine ("---------------------------------------- header -------------------------------------");
F2.WriteLine (str );
F2.WriteLine ("------------------------------------------ footer -------------------------------------");
_ IsTrue = true;
}
Catch {}
Finally
{
If (f! = Null) {f. Close (); f. Dispose (); f = null ;}
If (f2! = Null) {f2.Close (); f2.Dispose (); f2 = null ;}
}
}
Return _ isTrue;
}
/// <Summary>
/// Read the content of the object
/// </Summary>
/// <Param name = "fileName"> file </param>
/// <Param name = "encoding"> encoding gb2312 UTF-8 </param>
/// <Returns> ArrayList </returns>
Public static ArrayList Read (string fileName, string encoding)
{
String lineText = null; ArrayList txtTextArr = new ArrayList ();
If (! System. IO. File. Exists (FloderPath + fileName) {txtTextArr = null; return txtTextArr ;}
Lock (lockHelper)
{
StreamReader reader = encoding. Equals ("")? New StreamReader (FloderPath + fileName): new StreamReader (FloderPath + fileName, System. Text. Encoding. GetEncoding (encoding ));
While (lineText = reader. ReadLine ())! = Null)
{
TxtTextArr. Add (lineText );
}
Reader. Close ();
Reader. Dispose ();
}
Return txtTextArr;
}