初学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);
}
}
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/16179598/viewspace-1015401/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/16179598/viewspace-1015401/