C# 自定义Log文件写入类

C# 自定义Log文件写入类

本文档整合了一个Log类可以方便程序运行时Log的记录,并且可以精确记录到运行文件的文件名,行号,函数名等以及其他自定义信息

1.Log类 主要的静态类,在程序最开始利用Initialize()函数配合一个LogSetting完成Log类的初始化

(1)File()内置函数,配合初始化函数完成初始化

(2)Close()释放函数,完成整个类的释放后后期的写入内容

(3)Write()写入函数,根据初始化参数不同可以即时写入和最后写入

using System;
using System.Linq;
using System.IO;
namespace dllLogRecord
{
    static public class Log
    {
        static private bool IsInitialize = false;  //是否初始化
        static private string FileName;  //保存文件名字(一般为载入时间)
        static private string FilePath;  //保存文件地址
        static private SaveStyle FileStyle;  //保存文件方式
        static private SaveTime FileTime;  //保存类型
        static private SaveLog[] SaveLogs;  //储存的logs
        static private int SaveNumber = 0;
        static private StreamWriter LogWriter;  //用于写入写出的流
        static public void Initialize(LogSetting ls)   //LogSetting为初始化的一个结构体
        {
            if (IsInitialize == true) return;
            if (ls.Detect() == false) return;
            if (ls.Get(ref FilePath, ref FileStyle, ref FileTime) == false) return;
            SaveLogs = new SaveLog[1024];
            FileName = DateTime.Now.ToLongTimeString();
            File();
            IsInitialize = true;
            return;
        }
        static private void File()
        {
            switch(FileStyle)
            {
                case SaveStyle.Solitary:
                    {
                        LogWriter = new StreamWriter(FilePath + "\\" + FileName, false);
                    }break;
                case SaveStyle.Overwritten:
                    {
                        FileName = "LogRecord";
                        FilePath = @"C:\Windows\PointStudio";
                        LogWriter = new StreamWriter(FilePath + "\\" + FileName, false);
                    }break;
                case SaveStyle.Additional:
                    {
                        FileName = "LogRecord";
                        FilePath = @"C:\Windows\PointStudio";
                        LogWriter = new StreamWriter(FilePath + "\\" + FileName, true);
                    }break;
            }
            LogWriter.WriteLine(DateTime.Now.ToString());
            LogWriter.WriteLine("----------------------------------------------------------------Start----------------------------------------------------------------");
            return;
        }
        static public void Close()
        {
            if(FileTime==SaveTime.Eventual)
            {
                for(int i=0;i<=SaveLogs.Count()-1; ++i)
                {
                    LogWriter.WriteLine(SaveLogs[i].InputStr);
                }
            }
            LogWriter.WriteLine("-----------------------------------------------------------------End-----------------------------------------------------------------");
            LogWriter.Close();
        }
        static public void Write(SaveLog sl)   //利用一个SaveLog写入
        {
            if (IsInitialize == false) return;
            switch(FileTime)
            {
                case SaveTime.Instant:
                    {
                       LogWriter.WriteLine(sl.InputStr);
                    }break;
                case SaveTime.Eventual:
                   {
                        SaveLogs[SaveNumber++] = sl;
                   }break;
            }

        }
        static public void Write(string msg)   //直接调用该函数写入
        {
            string[] temp = new string[4];
            System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(1, true);
            temp[0] = msg;
            temp[1] = st.GetFrame(0).GetFileName();
            temp[1] = System.IO.Path.GetFileName(temp[1]);
            temp[2] = st.GetFrame(0).GetFileLineNumber().ToString();
            temp[3] = st.GetFrame(0).GetMethod().ToString();
            SaveLog sl = new SaveLog(temp);
            if (IsInitialize == false) return;
            switch (FileTime)
            {
                case SaveTime.Instant:
                    {
                        LogWriter.WriteLine(sl.InputStr);
                    }
                    break;
                case SaveTime.Eventual:
                    {
                        SaveLogs[SaveNumber++] = sl;
                    }
                    break;
            }
        }
    }
}

 2.LogSetting结构体 初始化Log使用,包括了文件地址,写入方式,保存方式等

(1)Detect() 检验函数,判断给出路径是否合法(存在)

(2)Get() 设置函数,设置初始化值

using System.IO;
namespace dllLogRecord
{
    public struct LogSetting
    {
        private string LogPath; //保存地址
        private SaveStyle LogStyle;  //保存类型
        private SaveTime LogTime;  //保存方式
        public bool Detect()  //验证路径是否存在
        {
            if (LogPath == null) return false;
            if (Directory.Exists(LogPath) == false) return false;
            return true;
        }
        public bool Get(ref string LogPath, ref SaveStyle SaveStyle, ref SaveTime SaveTime)
        {
            if (Detect() == false) return false;
            LogPath = this.LogPath;
            SaveStyle = this.LogStyle;
            SaveTime = this.LogTime;
            return true;
        }
        public LogSetting(string LogPath, SaveStyle SaveStyle, SaveTime SaveTime)
        {
            this.LogPath = LogPath;
            this.LogStyle = SaveStyle;
            this.LogTime = SaveTime;
            return;
        }
    }
}

3.SaveLog结构体:每一条Log的最小保存单元,通过new SaveLog(SaveLog.SetMessage(msg))来设置Log

(1)SaveLog()初始化函数 从一个string[4]里面获取Message,Name,Line,Way四个变量的值

(2)SetMessage()获取Log的基本设置,行数,文件名,函数名

PS:直接调用Log中的Write(string)也可以直接实现,而如果用SaveLog必须要在记录行数里面调用SetMessage()

using System;
namespace dllLogRecord
{
    public struct SaveLog
    {
        public string InputStr;
        DateTime Time;
        string Message;
        string Name;
        int Line;
        string Way;
        public SaveLog(string[] msg)
        {
            Time = DateTime.Now;
            Message = msg[0];
            Name = msg[1];
            Line = Convert.ToInt32(msg[2], 10);
            Way = msg[3];
            InputStr = null;
            InputStr = "[" + Time.ToString() + "]" + Way + " from" + "[" + Name + "]" + ":where" + "[" + Line.ToString() + "]" + Message;
            return;
        }
        static public string[] SetMessage(string msg)
        {
            string[] temp = new string[4];
            System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace(1, true);
            temp[0] = msg;
            temp[1] = st.GetFrame(0).GetFileName();
            temp[1] = System.IO.Path.GetFileName(temp[1]);
            temp[2] = st.GetFrame(0).GetFileLineNumber().ToString();
            temp[3] = st.GetFrame(0).GetMethod().ToString();
            return temp;
        }
    }
}

4.所有枚举类型

namespace dllLogRecord
{
    public enum SaveStyle
    {
        /// <summary>
        /// 以保存时间单独存在
        /// </summary>
        Solitary,
        /// <summary>
        /// 以总文件追加的
        /// </summary>
        Additional,
        /// <summary>
        /// 覆盖写入一个文件的
        /// </summary>
        Overwritten
    }
    public enum SaveTime
    {
        /// <summary>
        /// 即时的写入
        /// </summary>
        Instant,
        /// <summary>
        /// 最后的写入
        /// </summary>
        Eventual
    }
}

新人第一次发学习教程,请大家多多包涵

  • 13
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

纸墨青鸢

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值