C # log,
Reference page:
Http://www.yuanjiaocheng.net/webapi/webapi-filters.html
Http://www.yuanjiaocheng.net/webapi/create-crud-api-1.html
Http://www.yuanjiaocheng.net/webapi/create-crud-api-1-get.html
Http://www.yuanjiaocheng.net/webapi/create-crud-api-1-post.html
Http://www.yuanjiaocheng.net/webapi/create-crud-api-1-put.html
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 );
}
}
}
}