Reference page:
Http://www.yuanjiaocheng.net/Entity/first.html
Http://www.yuanjiaocheng.net/Entity/jieshao.html
Http://www.yuanjiaocheng.net/entity/tixijiegou.html
Http://www.yuanjiaocheng.net/entity/setenvrionment.html
Http://www.yuanjiaocheng.net/entity/createdatamodel.html
Using System;
Using System.IO;
Using System.Linq;
Using System.Text;
namespace Logdemo
{
// <summary>
/// Log class
// </summary>
/// <remarks>creator:v-zhuzhzh createtime:2015/7/31 11:18:09</remarks>
// <Description></Description>
Public class Log
{
// <summary>
/// write log.
// </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 month-dd Day}", DateTime.Now) + "log record. txt";
}
catch (Exception e)
{
Strdicpath = "c:/temp/log/";
strpath = Strdicpath + string. Format ("{0:yyyy-mm month-dd Day}", DateTime.Now) + "log record. 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.writ Ealltext (strpath, sb.) ToString () + "\ r \ n-----z-----\ r \ n" + str);
}
// <summary>
/// write log.
// </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 month-dd Day}", DateTime.Now) + "log record. 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);
}
}
}
}
C # Logs