程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#日志,

C#日志,

編輯:C#入門知識

C#日志,


參考頁面:

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>
/// 日志類
/// </summary>
/// <remarks>Creator: v-zhuzhzh CreateTime: 2015/7/31 11:18:09</remarks>
/// <Description></Description>
public class Log
{
/// <summary>
/// 寫入日志.
/// </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) + "日志記錄.txt";
}
catch (Exception e)
{
strDicPath = "C:/temp/log/";
strPath = strDicPath + string.Format("{0:yyyy年-MM月-dd日}", DateTime.Now) + "日志記錄.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>
/// 寫入日志.
/// </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) + "日志記錄.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);

}
}
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved