ITPub博客

首页 > Linux操作系统 > Linux操作系统 > c#简易log

c#简易log

原创 Linux操作系统 作者:yellowlee 时间:2008-12-29 10:15:21 0 删除 编辑
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-524530/,如需转载,请注明出处,否则将追究法律责任。

上一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2008-12-27

  • 博文量
    316
  • 访问量
    660839