C#中实现日志记录输出

相信很多小伙伴一遇见日志输出就会头疼,其实它也没那么难的。

很多人会选择日志输出的第三方插件使用,可是往往会发现它并没预料的那么简单。

其实不管用什么样的方式来实现它,你只要明白一点,日志输出你就可以理解为简单的文件写入,它实质上就是把你程序运行的过程信息记录到一个日志文件里,方便我们的查看,从而易于找到问题。

下面就是我自己实现的一个简单的日志输出:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace JR_Common
{
    /// <summary>
    /// 日志类
    /// </summary>
    public class Log
    {


        /// <summary>
        /// 输出日志记录(参数:记录消息)
        /// </summary>
        /// <param name="message"></param>
        public static void WriteLog(string message)
        {
            string folder = AppDomain.CurrentDomain.BaseDirectory + "Log";
            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }
            string date = DateTime.Now.ToString("yyyy-MM-dd");
            string file = "Log" + date + ".log";
            string path = folder + "\\" + file;

            FileStream fs = new FileStream(path, FileMode.Append);
            StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);

            string NowTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffff");
            sw.WriteLine();

            sw.WriteLine("- - " + NowTime + " - - - - - - - - - - -");
            sw.WriteLine("-");
            StackTrace trace = new StackTrace();
            StackFrame frame = trace.GetFrame(1); //1代表上级,2代表上上级,以此类推
            MethodBase method = frame.GetMethod();
            string methodName = method.Name;
            string className = method.ReflectedType.FullName;
            sw.WriteLine(className +"."+ methodName + "() 方法被执行。");
            sw.WriteLine("-");
            sw.WriteLine("记录消息:");
            sw.WriteLine(message);
            sw.WriteLine("-");
            sw.WriteLine("- - - - - - - - - - - - - - - - - - - - - - - - - -");

            sw.WriteLine();
            sw.Close();
            sw.Dispose();
            fs.Close();
            fs.Dispose();
        }


        /// <summary>
        /// 输出日志记录(参数:记录消息,错误异常信息)
        /// </summary>
        /// <param name="message"></param>
        /// <param name="error"></param>
        public static void WriteLog(string message, string error)
        {
            string folder = AppDomain.CurrentDomain.BaseDirectory + "Log";
            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }
            string date = DateTime.Now.ToString("yyyy-MM-dd");
            string file = "Log" + date + ".log";
            string path = folder + "\\" + file;

            FileStream fs = new FileStream(path, FileMode.Append);
            StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);

            string NowTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffff");
            sw.WriteLine();

            sw.WriteLine("- - " + NowTime + " - - - - - - - - - - -");
            sw.WriteLine("-");
            StackTrace trace = new StackTrace();
            StackFrame frame = trace.GetFrame(1); //1代表上级,2代表上上级,以此类推
            MethodBase method = frame.GetMethod();
            string methodName = method.Name;
            string className = method.ReflectedType.FullName;
            sw.WriteLine(className + "." + methodName + "() 方法被执行。");
            sw.WriteLine("-");
            sw.WriteLine("记录消息:");
            sw.WriteLine(message);
            sw.WriteLine("-");
            sw.WriteLine("错误异常:");
            sw.WriteLine(error);
            sw.WriteLine("-");
            sw.WriteLine("- - - - - - - - - - - - - - - - - - - - - - - - - -");

            sw.WriteLine();
            sw.Close();
            sw.Dispose();
            fs.Close();
            fs.Dispose();
        }


        /// <summary>
        /// 输出日志记录(参数:记录消息,错误异常信息,执行的SQL语句)
        /// </summary>
        /// <param name="message"></param>
        /// <param name="error"></param>
        /// <param name="strSql"></param>
        public static void WriteLog(string message, string error, string strSql)
        {
            string folder = AppDomain.CurrentDomain.BaseDirectory + "Log";
            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }
            string date = DateTime.Now.ToString("yyyy-MM-dd");
            string file = "Log" + date + ".log";
            string path = folder + "\\" + file;

            FileStream fs = new FileStream(path, FileMode.Append);
            StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);

            string NowTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fffff");
            sw.WriteLine();

            sw.WriteLine("- - " + NowTime + " - - - - - - - - - - -");
            sw.WriteLine("-");
            StackTrace trace = new StackTrace();
            StackFrame frame = trace.GetFrame(1); //1代表上级,2代表上上级,以此类推
            MethodBase method = frame.GetMethod();
            string methodName = method.Name;
            string className = method.ReflectedType.FullName;
            sw.WriteLine(className + "." + methodName + "() 方法被执行。");
            sw.WriteLine("-");
            sw.WriteLine("记录消息:");
            sw.WriteLine(message);
            sw.WriteLine("-");
            sw.WriteLine("错误异常:");
            sw.WriteLine(error);
            sw.WriteLine("-");
            sw.WriteLine("执行SQL:");
            sw.WriteLine(strSql);
            sw.WriteLine("-");
            sw.WriteLine("- - - - - - - - - - - - - - - - - - - - - - - - - -");

            sw.WriteLine();
            sw.Close();
            sw.Dispose();
            fs.Close();
            fs.Dispose();
        }



    }
}

下面看一下如何使用它:


图中划红线的部分就是调用日志输出的方法。

下面是输出结果:


总结一下:

日志输出的实质问题就是日志文件的写入,至于要写入什么内容,那就是你自己决定的啦。

把复杂的问题简单化,你就会把这个问题看的更加透彻,从而知道如何下手去做、去实现它。


希望给你带来帮助!!!谢谢大家。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值