Files
T-DAS/Epost.BLL/LogXmlBLL.cs
2023-01-13 15:30:20 +08:00

245 lines
9.0 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Epost.Common;
using Epost.DAL;
using Epost.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace Epost.BLL
{
public class LogXmlBLL
{
DB db = new DB();
List<Log4NetModel> list = new List<Log4NetModel>();
#region
public List<Log4NetModel> Log4NetData()
{
list.Clear();
Log4NetModel model = new Log4NetModel();
model.Name = "日志文件";
//【改变】
string directoryPath = System.AppDomain.CurrentDomain.BaseDirectory + "\\Log\\System_log";
//string directoryPath = db.path_log4;
long len = GetDirectoryLength(directoryPath);
string length = CountSize(len);
model.Space = length;
list.Add(model);
return list;
}
#endregion
#region
public static long GetDirectoryLength(string dirPath)
{
long len = 0;
//判断该路径是否存在(是否为文件夹)
if (!Directory.Exists(dirPath))
{
//查询文件的大小
len = FileSize(dirPath);
}
else
{
//定义一个DirectoryInfo对象
DirectoryInfo di = new DirectoryInfo(dirPath);
//通过GetFiles方法获取di目录中的所有文件的大小
foreach (FileInfo fi in di.GetFiles())
{
len += fi.Length;
}
//获取di中所有的文件夹并存到一个新的对象数组中以进行递归
DirectoryInfo[] dis = di.GetDirectories();
if (dis.Length > 0)
{
for (int i = 0; i < dis.Length; i++)
{
len += GetDirectoryLength(dis[i].FullName);
}
}
}
return len;
}
public static long FileSize(string filePath)
{
//定义一个FileInfo对象是指与filePath所指向的文件相关联以获取其大小
FileInfo fileInfo = new FileInfo(filePath);
return fileInfo.Length;
}
public static string CountSize(long Size)
{
string m_strSize = "";
long FactSize = 0;
FactSize = Size;
if (FactSize < 1024.00)
m_strSize = FactSize.ToString("F2") + " Byte";
else if (FactSize >= 1024.00 && FactSize < 1048576)
m_strSize = (FactSize / 1024.00).ToString("F2") + " K";
else if (FactSize >= 1048576 && FactSize < 1073741824)
m_strSize = (FactSize / 1024.00 / 1024.00).ToString("F2") + " M";
else if (FactSize >= 1073741824)
m_strSize = (FactSize / 1024.00 / 1024.00 / 1024.00).ToString("F2") + " G";
return m_strSize;
}
#endregion
#region
public bool DeleteLog4Net()
{
string directoryPath = System.AppDomain.CurrentDomain.BaseDirectory + "\\Log\\System_log";
DirectoryInfo dir = new DirectoryInfo(directoryPath);
FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回目录中所有文件和子目录
foreach (FileSystemInfo i in fileinfo)
{
if (i is DirectoryInfo) //判断是否文件夹
{
DirectoryInfo subdir = new DirectoryInfo(i.FullName);
subdir.Delete(true); //删除子目录和文件
}
else
{
File.Delete(i.FullName); //删除指定文件
}
//return true;
}
//删除日志记录
if (true == true)
{
//记录日志
ErrorLogBLL logBLL = new ErrorLogBLL();
ErrorLogModel error_model = new ErrorLogModel();
error_model.Type = "3";
error_model.Remark = "删除日志(包括系统设置自动删除):日志目录" + directoryPath;
logBLL.InsertErrorLog(error_model);
}
return true;
}
#endregion
#region xml()
public bool LoadLog4XML()
{
try
{
//【改变】
string sql_xml = System.AppDomain.CurrentDomain.BaseDirectory + "\\Log\\delog4.xml";
//string sql_xml = @db.path_delog4;
XmlDocument xmldoc = new XmlDocument();
string datatime = DateTime.Now.ToString();
XmlNode node = xmldoc.CreateXmlDeclaration("1.0", "utf-8", "");
xmldoc.AppendChild(node);
if (!File.Exists(sql_xml))
{
XmlElement root = xmldoc.CreateElement("Delete");
xmldoc.AppendChild(root);
//子节点
XmlElement delete = xmldoc.CreateElement("delete");
root.AppendChild(delete);
//一级属性
XmlAttribute day = xmldoc.CreateAttribute("days");
delete.Attributes.Append(day);
day.Value = Convert.ToString(30);
delete.InnerText = datatime;//当前时间
xmldoc.Save(sql_xml);
}
else
{
xmldoc.Load(sql_xml);
XmlNode orderid2 = xmldoc.SelectSingleNode("Delete");
var list = orderid2.ChildNodes;
for (int i = 0; i < list.Count; i++)
{
if (list[i].Attributes["days"].Name == "days")
{
string days = list[i].Attributes["days"].Value;
string innerday = list[i].Attributes["days"].Value;
DateTime dtt = Convert.ToDateTime(innerday);
dtt = dtt.AddDays(Convert.ToInt32(days));
DateTime dtt2 = Convert.ToDateTime(datatime);
if (dtt.CompareTo(dtt2) >= 0)
{
}
else if (dtt.CompareTo(dtt2) < 0)
{
//执行删除任务的操作
DeleteLog4Net();//删除数据中的加载日志历史数据
}
}
}
}
}
catch (Exception ex)
{
LogHelper.WriteLogInfo("加载日志文件定期删除异常:" + ex.ToString());
}
//对比数据
return true;
}
#endregion
#region xml
public bool Log4XML(int days)
{
//保存数据到xml格式中
string datatime = DateTime.Now.ToString();
//主目录
string sql_xml = System.AppDomain.CurrentDomain.BaseDirectory + "\\Log\\delog4.xml";
//string sql_xml = @db.path_delog4;
//public static string sqlcon = ConfigurationManager.ConnectionStrings["path"].ToString();
XmlDocument xmldoc = new XmlDocument();
XmlNode node = xmldoc.CreateXmlDeclaration("1.0", "utf-8", "");
xmldoc.AppendChild(node);
if (!File.Exists(sql_xml))
{
//根节点
XmlElement root = xmldoc.CreateElement("Delete");
xmldoc.AppendChild(root);
//子节点
XmlElement delete = xmldoc.CreateElement("delete");
root.AppendChild(delete);
//一级属性
XmlAttribute day = xmldoc.CreateAttribute("days");
delete.Attributes.Append(day);
day.Value = Convert.ToString(days);
delete.InnerText = datatime;//当前时间
xmldoc.Save(sql_xml);
}
else
{
xmldoc.Load(sql_xml);
XmlNode orderid2 = xmldoc.SelectSingleNode("Delete");
//获取属性
var list = orderid2.ChildNodes;
for (int i = 0; i < list.Count; i++)
{
if (list[i].Attributes["days"].Name == "days")
{
list[i].Attributes["days"].Value = Convert.ToString(days);
list[i].InnerText = datatime;
}
else
{
}
}
}
xmldoc.Save(sql_xml);
return true;
}
}
#endregion
}