一个打印日志类

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

namespace functiontest
{
    ///


    ///打印日志
    ///XingSQ
    /// 2015-12-10
    ///

    public class Log
    {
        ///
        /// 构造日志对象
        ///

        public Log()
        {
            logfilename = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "heatweblog.log";
        }
        ///
        /// 构造日志对象
        ///

        /// 日志所在路径
        public Log(string path)
        {
            logfilename = path + "\\log.log";
        }

        ///
        /// 创建log文件
        ///

        ///
        private void CreateFile(string path)
        {
            if (!File.Exists(path))
            {
                File.Create(path).Close();
            }
        }
        ///
        /// 写log日志
        ///

        /// 标题1
        /// 标题2
        /// log内容
        public void WriteLog(string title1, string title2, string logtext)
        {
            try
            {
                CreateFile(logfilename);               
                StreamWriter writer = File.AppendText(logfilename);
                writer.WriteLine(DateTime.Now.ToString() + "--------" + title1 + "--------" + title2 + "--------" + logtext);
                writer.Flush();
                writer.Close();
                FileSlim(logfilename, 100);
            }
            catch (Exception e)
            {
                throw (e);
            }
        }

        ///
        /// 写log日志(只在debug 状态下执行)
        ///

        /// 标题1
        /// 标题2
        /// log内容
        [Conditional("DEBUG")]
        public void WriteLogBug(string title1, string title2, string logtext)
        {
            try
            {
                CreateFile(logfilename);
                StreamWriter writer = File.AppendText(logfilename);
                writer.WriteLine(DateTime.Now.ToString() + "--------" + title1 + "--------" + title2 + "--------" + logtext);
                writer.Flush();
                writer.Close();
                FileSlim(logfilename, 100);
            }
            catch (Exception e)
            {
                throw (e);
            }
        }

        ///
        /// 写log日志
        ///

        /// 日志内容
        public void WriteLog(string logtext)
        {
            try
            {
                CreateFile(logfilename);
                StreamWriter writer = File.AppendText(logfilename);
                writer.WriteLine(DateTime.Now.ToString() + "--------" + logtext);
                writer.Flush();
                writer.Close();
                FileSlim(logfilename, 100);
            }
            catch (Exception e)
            {
                throw (e);
            }
        }

        ///
        /// 写log日志(只在debug 状态下执行)
        ///

        /// 日志内容
        [Conditional("DEBUG")]
        public void WriteLogDebug(string logtext)
        {
            try
            {
                CreateFile(logfilename);
                StreamWriter writer = File.AppendText(logfilename);
                writer.WriteLine(DateTime.Now.ToString() + "--------" + logtext);
                writer.Flush();
                writer.Close();
                FileSlim(logfilename, 100);
            }
            catch (Exception e)
            {
                throw (e);
            }
        }

        ///
        /// 取的文件大小(单位:字节)
        ///

        /// 文件完成路径(包括文件名)
        /// 文件大小(单位:字节)
        private long GetFileSize(string filepath)
        {
            FileInfo finfo = new FileInfo(filepath);
            return finfo.Length;
        }

        ///
        /// 给文件瘦身,当超过指定大小是就删除文件内容使其只剩intLineNum行
        ///

        /// 文件完成路径(包括文件名)
        /// 文件大小(单位:K字节)
        ///
        private bool FileSlim(string filepath, long size)
        {
            try
            {
                if (GetFileSize(filepath) >= 1024 * size)
                {
                    string[] strArray = File.ReadAllLines(filepath, Encoding.GetEncoding("UTF-8"));
                    int lines = strArray.Length - intLineNum;
                    IList strlist = strArray.ToList();

                    //删除list中的指定行数
                    for (int i = 0; i < lines; i++)
                    {
                        strlist.RemoveAt(0);
                    }
                    strlist.Add("\r\n");
                    //把list剩余行写回文件
                    File.WriteAllText(filepath, string.Join("\r\n", strlist.ToArray()), Encoding.GetEncoding("UTF-8"));
                }
                return true;
            }
            catch (Exception e)
            {
                throw (e);
            }
        }

        private string logfilename;
        private const int intLineNum = 200;        //保留行数
    }
}

转载于:https://my.oschina.net/u/267603/blog/550117

你可能感兴趣的:(一个打印日志类)