C # log,

Source: Internet
Author: User

C # log,

Using System;
Using System. IO;
Using System. Linq;
Using System. Text;

Namespace LogDemo
{
/// <Summary>
/// Log class
/// </Summary>
/// <Remarks> Creator: v-zhuzhzh CreateTime: 11:18:09 </remarks>
/// <Description> </Description>
Public class Log
{
/// <Summary>
/// Write logs.
/// </Summary>
/// <Param name = "strList"> The STR list. </param>
/// <Remarks> </remarks>
/// <Description> </Description>
Public static void WriteLog (params object [] strList)
{
If (strList. Count () = 0) return;
String strDicPath = "";
String strPath = "";
Try
{
StrDicPath = System. Web. HttpContext. Current. Server. MapPath ("~ /Temp/log /");
StrPath = strDicPath + string. Format ("{0: yyyy-MM-dd}", DateTime. Now) + "log .txt ";
}
Catch (Exception e)
{
StrDicPath = "C:/temp/log /";
StrPath = strDicPath + string. Format ("{0: yyyy-MM-dd}", DateTime. Now) + "log .txt ";
}

If (! Directory. Exists (strDicPath) Directory. CreateDirectory (strDicPath );
If (! File. Exists (strPath) using (FileStream fs = File. Create (strPath )){}
String str = File. ReadAllText (strPath );
StringBuilder sb = new StringBuilder ();
Foreach (var item in strList)
{
Sb. Append ("\ r \ n" + DateTime. Now. ToString () + "-----" + item + "");
}
File. WriteAllText (strPath, sb. ToString () + "\ r \ n ----- z ----- \ r \ n" + str );
}


/// <Summary>
/// Write logs.
/// </Summary>
/// <Param name = "strList"> The STR list. </param>
/// <Remarks> </remarks>
/// <Description> </Description>
Public static void WriteLog (Action DefFunc, Func <string> ErrorFunc = null)
{
Try
{
DefFunc ();
}
Catch (Exception ex)
{
String strDicPath = System. Web. HttpContext. Current. Server. MapPath ("~ /Temp/log /");
String strPath = strDicPath + string. Format ("{0: yyyy-MM-dd}", DateTime. Now) + "log .txt ";
If (! Directory. Exists (strDicPath) Directory. CreateDirectory (strDicPath );
If (! File. Exists (strPath) using (FileStream fs = File. Create (strPath ));
String str = File. ReadAllText (strPath );
StringBuilder sb = new StringBuilder ();
If (ErrorFunc! = Null)
{
Sb. Append ("\ r \ n" + DateTime. Now. ToString () + "-----" + ErrorFunc ());
}
Sb. Append ("\ r \ n" + DateTime. Now. ToString () + "-----" + ex. Message );
Sb. Append ("\ r \ n" + DateTime. Now. ToString () + "-----" + ex. StackTrace );
File. WriteAllText (strPath, sb. ToString () + "\ r \ n -- z -------- \ r \ n" + str );

}
}
}
}

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.