File log writing and reading, file log writing and reading

Source: Internet
Author: User

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;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.