C # log Writing Method

Source: Internet
Author: User

 

/// <Summary>

 

/// Generate doc-pdf log

/// </Summary>

/// <Paramname = "item"> operation item name </param>

/// <Paramname = "errorContent"> error message </param>

/// <Paramname = "FileName_Prefix"> file name prefix (full name of the time combination) </param>

Public static void savegeneraldoc1_logfiles (string item, stringerrorContent, string FileName_Prefix)

{

StreamWriter sw = null;

DateTime date = DateTime. Now;

String FileName = string. Empty;

Try

{

If (HttpContext. Current = null)

{

FileName = HttpRuntime. AppDomainAppPath + "/Logs/GeneralDocPdf/" + FileName_Prefix + "_" + date. Month + date. Day + ". log ";

}

Else

{

FileName = HttpContext. Current. Server. MapPath ("~ /Logs/GeneralDocPdf/"+ FileName_Prefix +" _ "+ date. Month + date. Day +". log ");

}

# Region check whether the log directory exists

String forderPathStr = null;

If (HttpContext. Current = null)

{

ForderPathStr = HttpRuntime. AppDomainAppPath + "/Logs/GeneralDocPdf ";

}

Else

{

ForderPathStr = HttpContext. Current. Server. MapPath ("~ /Logs/GeneralDocPdf ");

}

If (! Directory. Exists (forderPathStr ))

{

Directory. CreateDirectory (forderPathStr );

}

 

If (! File. Exists (FileName ))

{

Sw = File. CreateText (FileName); // if the File does not exist, create and add the content.

}

Else

{

Sw = File. AppendText (FileName); // If the log File already exists, content is appended to the File

}

# Endregion

Sw. WriteLine (item );

Sw. WriteLine (errorContent); // write row

Sw. WriteLine ("[Time]" + System. DateTime. Now );

Sw. writeLine when there are already too many other than too many ");

Sw. Flush ();

}

Finally

{

If (sw! = Null)

Sw. Close ();

}

}

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.