日志

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


namespace ConsoleApplication1
{
    /// <summary>
    /// Log 的类别
    /// </summary>
    public enum LogType
    {

        Debug,
        Trace,
        Info,
        Warn,
        Error
    }
    //定义委托
    public delegate void Log(string content, LogType type);


    sealed class LogManager : IDisposable
    {
        //书写日志的组件
        private Type componentType;
        //日志文件
        private string logfile;
        //读写流
        //using System.IO;
        private FileStream fs;
        //用来写日志的委托
        public Log WriteLog;

        //无参构造函数
        private LogManager()
        {
            WriteLog = new Log(PrepareLogFile);
            WriteLog += OpenStream;
            WriteLog += AppendLocalTime;
            WriteLog += AppendSeperator;
            WriteLog += AppendComponentType;
            WriteLog += AppendSeperator;
            WriteLog += AppendType;
            WriteLog += AppendSeperator;
            WriteLog += AppendContent;
            WriteLog += AppendNewLine;
            WriteLog += CloseStream;
        }


        //构造函数
        public LogManager(Type type, string file)
            : this()
        {
            logfile = file;
            componentType = type;
        }
        /// <summary>
        /// 释放 FileStream 对象
        /// </summary>


        public void Dispose()
        {
            if (fs != null)
            {
                fs.Dispose();
            }
            // Garbage Collector 垃圾回收
            GC.SuppressFinalize(this);

        }
        //如果日志文件不存在,则新建日志文件
        private void PrepareLogFile(string content, LogType type)
        {
            if (!File.Exists(logfile))
            {
                using (FileStream fs = File.Create(logfile))
                {

                }

            }
        }
        //打开文件流
        private void OpenStream(string content, LogType type)
        {
            fs = File.Open(logfile, FileMode.Append);
        }
        //关闭文件流
        private void CloseStream(string content, LogType type)
        {
            fs.Close();
            fs.Dispose();
        }
        //位日志源添加本地时间
        private void AppendLocalTime(string content, LogType type)
        {
            string time = DateTime.Now.ToLocalTime().ToString();
            //using System.Text;
            Byte[] con = Encoding.Default.GetBytes(time);
            fs.Write(con, 0, con.Length);
        }
        //添加日志内容
        private void AppendContent(string content, LogType type)
        {
            Byte[] con = Encoding.Default.GetBytes(content);
            fs.Write(con, 0, con.Length);
        }
        //位日志添加组件类型
        private void AppendComponentType(string content, LogType type)
        {
            Byte[] con = Encoding.Default.GetBytes(componentType.ToString());
            fs.Write(con, 0, con.Length);

        }
        //添加日志类型
        private void AppendType(string content, LogType type)
        {
            string typestring = string.Empty;
            switch (type)
            {
                case LogType.Trace:
                    typestring = "Trace";
                    break;

                case LogType.Debug:
                    typestring = "Debug";
                    break;

                case LogType.Info:
                    typestring = "Info";
                    break;

                case LogType.Warn:
                    typestring = "Warn";
                    break;

                case LogType.Error:
                    typestring = "Error";
                    break;
                default:
                    typestring = " ";
                    break;
            }
            Byte[] con = Encoding.Default.GetBytes(typestring);
            fs.Write(con, 0, con.Length);

        }
        //添加分隔符
        private void AppendSeperator(string content, LogType type)
        {
            Byte[] con = Encoding.Default.GetBytes("|");
            fs.Write(con, 0, con.Length);
        }
        //添加换行符
        private void AppendNewLine(string content, LogType type)
        {
            Byte[] con = Encoding.Default.GetBytes("\r\n");
            fs.Write(con, 0, con.Length);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            using (LogManager manager = new LogManager(Type.GetType("ConsoleApplication1.LogType"), "D:\\log.txt"))
            {
                manager.WriteLog("新建日志", LogType.Debug);
                manager.WriteLog("日志数据", LogType.Info);
                manager.WriteLog("发生错误", LogType.Error);
                manager.WriteLog("准备退出", LogType.Trace);
            }
            Console.WriteLine("添加日志成功");
            Console.Read();

        }
    }
}

posted @ 2012-05-07 18:01  |▍花舞花落泪 ╮  阅读(187)  评论(0编辑  收藏  举报