| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.IO;
- namespace YTSoft.Common
- {
- /// <summary>
- /// 记录系统错误日志类
- /// </summary>
- public class SysLog
- {
- #region 错误记录
- private static Exception EX = null;//错误消息
- private static string Leve = null;//错误级别
- private static string Position = null;//错误位置
- private static string SavePath = null; //保存位置
- /// <summary>
- /// 向文件中写错误日志,只写错误信息
- /// </summary>
- /// <param name="ex">错误信息</param>
- public static void WriteLog(Exception ex)
- {
- EX = ex;
- WriteDisk();//文件写入硬盘
- }
- /// <summary>
- /// 向文件中写错误日志,只写错误信息
- /// </summary>
- /// <param name="ex">错误信息</param>
- public static void WriteLog(Exception ex, string path)
- {
- SavePath = path;
- EX = ex;
- WriteDisk();//文件写入硬盘
- }
- /// <summary>
- /// 向文件中写错误日志,记录错误信息,发生错误位置
- /// </summary>
- /// <param name="ex">错误信息</param>
- /// <param name="position">发生错误位置</param>
- public static void WriteLog(Exception ex, string position, string path)
- {
- SavePath = path;
- EX = ex;
- Position = position;
- WriteDisk();//文件写入硬盘
- }
- /// <summary>
- /// 向文件中写错误日志,记录错误信息,发生错误位置,错误级别
- /// </summary>
- /// <param name="ex">错误信息</param>
- /// <param name="position">发生错误位置</param>
- /// <param name="type">错误级别</param>
- public static void WriteLog(Exception ex, string position, string leve, string path)
- {
- SavePath = path;
- EX = ex;
- Leve = leve;
- Position = position;
- WriteDisk();//文件写入硬盘
- }
- /// <summary>
- /// 文件写入硬盘
- /// </summary>
- /// <param name="str"></param>
- private static void WriteDisk()
- {
- FileStream fs = null;
- try
- {
- string str = "时间:" + System.DateTime.Now.ToString() + "\t";
- if (EX != null)
- {
- str += "错误消息:" + EX.Message + "\t";
- if (EX.HelpLink != null)
- {
- str += "与此关联的文件:" + EX.HelpLink + "\t";
- }
- str += "错误消息字符串表示:" + EX.StackTrace + "\t";
- }
- if (Position != null)
- {
- str += "错误位置:" + Position + "\t";
- }
- if (Leve != null)
- {
- str += "错误级别:" + Leve + "\t";
- }
- str += "\r\n\r\n\r\n";
- string path = SavePath + "/Logs/ErrorLogs/" + DateTime.Now.ToString("yyyyMM") + "/" + DateTime.Now.ToString("yyyyMMdd") + "Log.txt";
- if (!File.Exists(path))
- {
- Directory.CreateDirectory(SavePath + "/Logs/ErrorLogs/" + DateTime.Now.ToString("yyyyMM") + "/");
- File.Create(path).Close();
- }
- //字符串写入硬盘
- byte[] bytData = System.Text.Encoding.Default.GetBytes(str);
- fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite, 2048);
- fs.Write(bytData, 0, bytData.Length);
- }
- finally
- {
- if (fs != null)
- {
- fs.Close();
- }
- EX = null;
- Position = null;
- Leve = null;
- }
- }
- /// <summary>
- /// 文件写入硬盘 操作日志
- /// </summary>
- /// <param name="str"></param>
- public static void WriteOptDisk(string info, string paths)
- {
- SavePath = paths;
- FileStream fs = null;
- try
- {
- string str = "时间:" + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ms") + "\t";
- str += "操作信息:" + info + "\t";
- str += "\r\n\r\n\r\n";
- string path = SavePath + "/Logs/OptLogs/" + DateTime.Now.ToString("yyyyMM") + "/" + DateTime.Now.ToString("yyyyMMdd") + "-" + DateTime.Now.Hour + "-" + DateTime.Now.Minute + "Log.txt";
- if (!File.Exists(path))
- {
- Directory.CreateDirectory(SavePath + "/Logs/OptLogs/" + DateTime.Now.ToString("yyyyMM")+ "/");
- File.Create(path).Close();
- }
- //字符串写入硬盘
- byte[] bytData = System.Text.Encoding.Default.GetBytes(str);
- fs = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite, 2048);
- fs.Write(bytData, 0, bytData.Length);
- }
- finally
- {
- if (fs != null)
- {
- fs.Close();
- }
- }
- }
- #endregion
- }
- }
|