123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Reflection;
- using System.Text;
- namespace SnapshotWinFormsApp.Tools
- {
- /// <summary>
- /// 最基本的日志记录
- /// </summary>
- public static class Logs
- {
- /// <summary>
- /// 获取记录文件路径
- /// </summary>
- /// <returns>日志文件路径</returns>
- public static string Path()
- {
- var logPath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "logs");
- if (!Directory.Exists(logPath))
- {
- Directory.CreateDirectory(logPath);
- }
- return logPath;
- }
- /// <summary>
- /// 错误信息记录
- /// 请在所有的日志记录都无法使用的情况下使用下, 再使用本方法
- /// </summary>
- /// <param name="msg">记录的内容</param>
- public static void Err(string msg)
- {
- string logPath = System.IO.Path.Combine(Path(), "err.log");
- StackTrace trace = new StackTrace();
- MethodBase methodBase = trace.GetFrame(1).GetMethod();
- msg = $"方法 {methodBase.Name} 异常: {msg}";
- Note(logPath, msg, true);
- }
- /// <summary>
- /// 错误信息记录
- /// </summary>
- /// <param name="e"></param>
- public static void Err(Exception e)
- {
- var msg = string.Empty;
- try
- {
- msg = "Form: " + e?.InnerException?.Source + "\r\nMsg: " + e;
- }
- catch
- {
- msg = e.ToString();
- }
- Err(msg);
- }
- /// <summary>
- /// 记录警告信息
- /// </summary>
- /// <param name="msg">内容</param>
- public static void Warning(string msg)
- {
- Note(System.IO.Path.Combine(Path() , "Warning.log"),msg,true);
- }
- /// <summary>
- /// 错误信息记录
- /// </summary>
- /// <param name="msg">记录的内容</param>
- /// <param name="name">文件名字</param>
- public static void Err(string msg, string name)
- {
- string path = System.IO.Path.Combine(Path(), DateTime.Now.ToString("yyyyMMdd") + "[" + name + "][err].log");
- Note(path, msg, true);
- }
- /// <summary>
- /// 错误信息记录
- /// </summary>
- /// <param name="method">方法</param>
- /// <param name="param">参数</param>
- /// <param name="discription">描述</param>
- public static void Err(string method, string param ,string discription)
- {
- string message = "方法: " + method + "\r\n参数:" + param + "\r\n描述:" + discription;
- Err(message);
- }
- /// <summary>
- /// 日志记录
- /// </summary>
- /// <param name="msg">记录的内容</param>
- public static void Note(string msg)
- {
- var logPath = System.IO.Path.Combine(Path(), "note.log");
- Note(logPath, msg, true);
- }
- /// <summary>
- /// 日志记录
- /// 日志自动记录调用者的名字
- /// </summary>
- /// <param name="msg">记录的信息</param>
- public static void MethodNote(string msg)
- {
- StackTrace trace = new StackTrace();
- MethodBase metbodBase = trace.GetFrame(1).GetMethod();
- Note(System.IO.Path.Combine(Path(), "note.log"), metbodBase.Name + ":" + msg);
- }
- /// <summary>
- /// 日志记录
- /// </summary>
- /// <param name="path">日志文件路径</param>
- /// <param name="msg">内容</param>
- public static void Note(string path, string msg)
- {
- Note(path, msg, false);
- }
- /// <summary>
- /// 日志记录
- /// </summary>
- /// <param name="msg">记录的内容</param>
- /// <param name="path">日志文件路径</param>
- /// <param name="format">是否格式化信息</param>
- public static void Note(string path, string msg, bool format)
- {
- try
- {
- if (!File.Exists(path))
- {
- FileStream fs = File.Create(path);
- fs.Dispose();
- fs.Close();
- }
- using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.Write))
- {
- StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
- if (format) sw.WriteLine(DateTime.Now + " " + msg);
- else sw.WriteLine(msg);
- sw.Dispose();
- sw.Close();
- }
- }
- catch
- {
- }
- }
- }
- }
|