C # Logs

Source: Internet
Author: User

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

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.