相信很多小伙伴一遇见日志输出就会头疼,其实它也没那么难的。
很多人会选择日志输出的第三方插件使用,可是往往会发现它并没预料的那么简单。
其实不管用什么样的方式来实现它,你只要明白一点,日志输出你就可以理解为简单的文件写入,它实质上就是把你程序运行的过程信息记录到一个日志文件里,方便我们的查看,从而易于找到问题。
下面就是我自己实现的一个简单的日志输出:
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();
}
}
}
下面看一下如何使用它:
图中划红线的部分就是调用日志输出的方法。
下面是输出结果:
总结一下:
日志输出的实质问题就是日志文件的写入,至于要写入什么内容,那就是你自己决定的啦。
把复杂的问题简单化,你就会把这个问题看的更加透彻,从而知道如何下手去做、去实现它。
希望给你带来帮助!!!谢谢大家。