Log File Operations (write, read, and clear)

Source: Internet
Author: User

# Region Log File
/// <Summary>
/// Proofread and write log files
/// </Summary>
/// <Param name = "checkinformation"> </param>
Public static void writecheckinformationtolog (string checkinformation)
{
Streamwriter Sw = NULL;
Streamwriter swhistory = NULL;
Try
{
Sw = new streamwriter (@ ". \ log.txt", false, encoding. Default );
Swhistory = new streamwriter (". \" + datetime. Now. tow.datestring () + "_log.txt", true, encoding. Default );
Sw. writeline (checkinformation );
Swhistory. writeline (checkinformation );
}
Catch (Exception fileExp)
{
Throw new Exception ("An error occurred while writing information to the log file. System Information: "+ fileExp. Message );
}
Finally
{
Sw. Flush ();
Sw. Close ();
SwHistory. Flush ();
SwHistory. Close ();
}
}
/// <Summary>
/// Read the log file
/// </Summary>
/// <Param name = "checkInformation"> </param>
Public void ReadCheckInformationFromLog ()
{
StreamReader sr = null;
Try
{
Sr = new StreamReader (". \ log.txt", Encoding. Default, true );
Sr. ReadLine ();
}
Catch (Exception fileExp)
{
Throw new Exception ("failed to read log file information. System Information: "+ fileExp. Message );
}
Finally
{
Sr. Close ();
}
}

/// <Summary>
/// Clear the log file
/// </Summary>
Public static void clearlogfile ()
{
Streamwriter Sw = NULL;

// Clear the Current Log File
Try
{
Sw = new streamwriter (@ ". \ log.txt", false, encoding. Default );
Sw. writeline ("");

}
Catch (exception fileexp)
{
Throw new exception ("failed to clear the log file. System Information: "+ fileexp. Message );
}
Finally
{
Sw. Flush ();
Sw. Close ();
}
}

# Endregion

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.