码迷,mamicode.com
首页 > Windows程序 > 详细

C#日志

时间:2017-01-07 17:47:58      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:mod   action   sha   ret   direct   each   ram   orm   frame   

参考页面:

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>
/// 日志类
/// </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);

}
}
}
}

C#日志

标签:mod   action   sha   ret   direct   each   ram   orm   frame   

原文地址:http://www.cnblogs.com/liubibi23/p/6259688.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!