ITPub博客

首页 > 应用开发 > IT综合 > c#简易 log

c#简易 log

原创 IT综合 作者:yellowlee 时间:2008-12-29 10:07:02 0 删除 编辑

初学C#时的习作。

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace database
{
class DebugLog
{
#region common define
private static string LOGFILE = "logfile.log";
private static string APPLOGINFO = "应用程序启动";
private static string APPLOGCLOSE = "应用程序关闭";
private static string APPLOGSEPARATOR = " ||... ";
private static string APPLOGFOOTER = "--------------------------------------------------";
private static StreamWriter sw;
#endregion

//设置字符集
public static void setEncoing()
{
//sw.Encoding = System.Text.Encoding.Unicode;
}

//建立应用程序日志
public static void CreateAppLog()
{
if (File.Exists(LOGFILE))
{
sw = File.AppendText(LOGFILE);
}

else
{
sw = File.CreateText(LOGFILE);
}

sw.WriteLine(System.DateTime.Now + APPLOGSEPARATOR + APPLOGINFO);
CloseLog();
}

//建立运行时日志
public static void CreateLog()
{
if (File.Exists(LOGFILE))
{
sw = File.AppendText(LOGFILE);
}

else
{
sw = File.CreateText(LOGFILE);
}

}

//关闭应用程序日志
public static void CloseAppLog()
{
CreateLog();
sw.WriteLine(System.DateTime.Now + APPLOGSEPARATOR + APPLOGCLOSE);
sw.WriteLine(APPLOGFOOTER);
sw.Close();
}

//关闭运行时日志
public static void CloseLog()
{
sw.WriteLine(System.DateTime.Now + APPLOGSEPARATOR + APPLOGCLOSE);
sw.Close();
}

//写log obj
public static void WriteLog(object obj)
{
string str = obj.ToString();

CreateLog();

if (File.Exists(LOGFILE))
{
sw.WriteLine(System.DateTime.Now + APPLOGSEPARATOR + str);
}

else
{
sw = File.CreateText(LOGFILE);
sw.WriteLine(System.DateTime.Now + APPLOGSEPARATOR + str);
}

sw.Close();

}

//写log string
public static void WriteLog(string str)
{

CreateLog();

if (File.Exists(LOGFILE))
{
sw.WriteLine(System.DateTime.Now + APPLOGSEPARATOR + str);
}

else
{
sw = File.CreateText(LOGFILE);
sw.WriteLine(System.DateTime.Now + APPLOGSEPARATOR + str);
}

sw.Close();

}

//写log obj
public static void Debuglog(object ob)
{
WriteLog(ob);
}

//写错误日志
public static void Debuglog(Exception e)
{
WriteLog(e.Message);
}
}
}

[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/16179598/viewspace-1015401/,如需转载,请注明出处,否则将追究法律责任。

上一篇: 新的开始
请登录后发表评论 登录
全部评论

注册时间:2008-12-27

  • 博文量
    316
  • 访问量
    682491