Asp.net writes logs to files

Source: Internet
Author: User

Using system;
Using system. IO;

Namespace writelog
{
/// <Summary>
/// Summary of writeinlog.
/// </Summary>
Public class writeinlog
{
Private string logfilename;
Private int logfilesizes;

/// <Summary>
/// Write the log file
/// </Summary>
Public writeinlog ()
{
Logfilename = @ "D: \ logfile.txt ";
}

/// <Summary>
/// Automatically delete the log file size. This method has been reloaded.
/// </Summary>
/// <Param name = "filesize"> log file size, in KB </param>
Public writeinlog (INT filesize): this ()
{
If (filesize! = 0)
{
This. logfilesizes = filesize * 1024;
}
Else
{
This. logfilesizes = 1024;
}
}
/// <Summary>
/// Complete log file name, for example, @ "D: \ logfile.txt"
/// </Summary>
Public String logfilename
{
Set
{
This. logfilename = value;
}
}

/// <Summary>
/// Write log information
/// </Summary>
/// <Param name = "MSG"> log content </param>
/// <Param name = "isautodelete"> whether to automatically delete logs </param>
Public void writeinlog (string MSG)
{
If (logfilesizes! = 0)
{
Writeinlog (MSG, true );
}
Else
{
Writeinlog (MSG, false );
}
}

/// <Summary>
/// Write log information
/// </Summary>
/// <Param name = "MSG"> log content </param>
/// <Param name = "isautodelete"> whether to automatically delete logs </param>
Private void writeinlog (string MSG, bool isautodelete)
{
Try
{
Fileinfo = new fileinfo (logfilename );
If (isautodelete)
{
If (fileinfo. exists & fileinfo. length> = logfilesizes)
{
Fileinfo. Delete ();
}
}
Using (filestream FS = fileinfo. openwrite ())
{
Streamwriter Sw = new streamwriter (FS );
Sw. basestream. Seek (0, seekorigin. End );
SW. writeline ("========================================== ");
Sw. Write ("add Date:" + datetime. Now. tostring () + "\ r \ n ");
Sw. Write ("log Content:" + MSG + "\ r \ n ");
SW. writeline ("========================================== ");
Sw. Flush ();
Sw. Close ();
}
}
Catch (exception ex)
{
Ex. tostring ();
}
}

}
}

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.