123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- namespace Common
- {
- public class LogHelper
- {
- public static string LogInfo = AppDomain.CurrentDomain.BaseDirectory + "Logs\\";
- /// <summary>
- /// 获取日志存储路径
- /// </summary>
- /// <param name="logfilename"></param>
- /// <param name="type">0正常信息,1错误信息</param>
- /// <returns></returns>
- private static string GetLogFilePath(string logfilename, int type = 0)
- {
- if (!System.IO.Directory.Exists(LogInfo))
- {
- System.IO.Directory.CreateDirectory(LogInfo);
- }
- string log = LogInfo + DateTime.Now.ToString("yyyy-MM-dd") + logfilename + ".txt";
- if (type != 0)
- {
- log = LogInfo + logfilename + ".txt";
- }
- return log;
- }
- /// <summary>
- /// 保存的最多日志数量
- /// </summary>
- // private static int MaxLogCount = 30;
- /// <summary>
- /// 日志信息
- /// </summary>
- /// <param name="msg"></param>
- /// <param name="logfilename"></param>
- public static void Info(string msg, string logfilename = "",int type = 0)
- {
- try
- {
- DeleteLog();
- string datapath = GetLogFilePath(logfilename, type);
- if (type != 0)
- {
- using (FileStream fs = new FileStream(datapath, FileMode.Create))
- {
- using (StreamWriter sw = new StreamWriter(fs))
- {
- //开始写入
- sw.WriteLine("当前时间:" + DateTime.Now.ToString());
- sw.WriteLine("日志级别:Error");
- sw.WriteLine("偏移量:"+ logfilename);
- sw.WriteLine("日志信息:" + msg);
- //清空缓冲区
- sw.Flush();
- //关闭流
- sw.Close();
- fs.Close();
- }
- }
- return;
- }
- if (System.IO.File.Exists(datapath))//追加
- {
- using (FileStream fs = new FileStream(datapath, FileMode.Append))
- {
- using (StreamWriter sw = new StreamWriter(fs))
- {
- //开始写入
- sw.WriteLine("\n");
- sw.WriteLine("当前时间:" + DateTime.Now.ToString());
- sw.WriteLine("日志级别:Info");
- sw.WriteLine("日志信息:" + msg);
- //清空缓冲区
- sw.Flush();
- //关闭流
- sw.Close();
- fs.Close();
- }
- }
- }
- else//创建
- {
- using (FileStream fs = new FileStream(datapath, FileMode.Create))
- {
- using (StreamWriter sw = new StreamWriter(fs))
- {
- //开始写入
- sw.WriteLine("当前时间:" + DateTime.Now.ToString());
- sw.WriteLine("日志级别:Info");
- sw.WriteLine("日志信息:" + msg);
- //清空缓冲区
- sw.Flush();
- //关闭流
- sw.Close();
- fs.Close();
- }
- }
- }
- }
- catch
- {
- }
- }
- /// <summary>
- /// 错误日志
- /// </summary>
- /// <param name="ex"></param>
- /// <param name="logfilename"></param>
- public static void Error(Exception ex, string logfilename = "", int type = 0)
- {
- try
- {
- DeleteLog();
- string datapath = GetLogFilePath(logfilename, type);
- if (System.IO.File.Exists(datapath))//追加
- {
- using (FileStream fs = new FileStream(datapath, FileMode.Append))
- {
- using (StreamWriter sw = new StreamWriter(fs))
- { //开始写入
- sw.WriteLine("\n");
- sw.WriteLine("当前时间:" + DateTime.Now.ToString());
- sw.WriteLine("异常级别:Error");
- sw.WriteLine("异常信息:" + ex.Message);
- sw.WriteLine("异常对象:" + ex.Source);
- sw.WriteLine("异常位置:" + ex.StackTrace);
- //清空缓冲区
- sw.Flush();
- //关闭流
- sw.Close();
- fs.Close();
- }
- }
- }
- else//创建
- {
- using (FileStream fs = new FileStream(datapath, FileMode.Create))
- {
- using (StreamWriter sw = new StreamWriter(fs))
- {
- //开始写入
- sw.WriteLine("当前时间:" + DateTime.Now.ToString());
- sw.WriteLine("异常级别:Error");
- sw.WriteLine("异常信息:" + ex.Message);
- sw.WriteLine("异常对象:" + ex.Source);
- sw.WriteLine("异常位置:" + ex.StackTrace);
- //清空缓冲区
- sw.Flush();
- //关闭流
- sw.Close();
- fs.Close();
- }
- }
- }
- }
- catch
- {
- }
- }
- /// <summary>
- /// 删除多余日志
- /// </summary>
- private static void DeleteLog()
- {
- //不必每天都删除日志,整10天删除
- if (!(DateTime.Now.Day % 10 == 0))
- {
- return;
- }
- DirectoryInfo dic = new DirectoryInfo(LogInfo);
- if (dic.Exists)
- {
- try
- {
- //清理10天前的日志文件
- dic.GetFiles("*.txt", SearchOption.AllDirectories).Where(n => (DateTime.Now - n.LastWriteTime).TotalDays > 10).ToList().TrueForAll(n => { n.Delete(); return true; });
- }
- catch (Exception ex)
- {
- Error(ex);
- }
- }
- }
- }
- }
|