You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
327 lines
12 KiB
327 lines
12 KiB
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace XS_DAL
|
|
{
|
|
/// <summary>
|
|
/// 日志记录类
|
|
/// </summary>
|
|
public static class LogHelper
|
|
{
|
|
/// <summary>
|
|
/// 系统异常日志
|
|
/// </summary>
|
|
/// <param name="ex">异常类</param>
|
|
public static void ErrorLog(Exception ex)
|
|
{
|
|
try
|
|
{
|
|
string path = System.Environment.CurrentDirectory;
|
|
string threadId = Thread.CurrentThread.ManagedThreadId.ToString();
|
|
string fName = path + "\\ErrorLog\\线程" + threadId + " " + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
|
|
|
|
string newPath = path + "\\ErrorLog\\";
|
|
|
|
if (!System.IO.Directory.Exists(newPath))
|
|
{
|
|
System.IO.Directory.CreateDirectory(newPath);
|
|
}
|
|
|
|
FileInfo fileInfo = new FileInfo(fName);
|
|
|
|
if (fileInfo.Exists == false)
|
|
{
|
|
using (StreamWriter sw = fileInfo.CreateText())
|
|
{
|
|
sw.WriteLine("/********************************************************************************/");
|
|
sw.WriteLine("时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff"));
|
|
sw.WriteLine("信息:" + ex.Message);
|
|
sw.WriteLine("实例:" + ex.InnerException);
|
|
sw.WriteLine("方法:" + ex.TargetSite);
|
|
sw.WriteLine("堆栈:" + ex.StackTrace);
|
|
sw.WriteLine();
|
|
sw.Flush();
|
|
sw.Close();
|
|
}
|
|
FilesDelete(fileInfo.DirectoryName);
|
|
}
|
|
else
|
|
{
|
|
using (StreamWriter sw = fileInfo.AppendText())
|
|
{
|
|
sw.WriteLine("/********************************************************************************/");
|
|
sw.WriteLine("时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff"));
|
|
sw.WriteLine("信息:" + ex.Message);
|
|
sw.WriteLine("实例:" + ex.InnerException);
|
|
sw.WriteLine("方法:" + ex.TargetSite);
|
|
sw.WriteLine("堆栈:" + ex.StackTrace);
|
|
sw.WriteLine();
|
|
sw.Flush();
|
|
sw.Close();
|
|
}
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 系统异常日志
|
|
/// </summary>
|
|
/// <param name="dName">目录名</param>
|
|
/// <param name="ex">异常类</param>
|
|
public static void ErrorLog(string dName, Exception ex)
|
|
{
|
|
try
|
|
{
|
|
string path = System.Environment.CurrentDirectory;
|
|
string newPath = path + "\\" + dName + "\\";
|
|
|
|
if (!System.IO.Directory.Exists(newPath))
|
|
{
|
|
System.IO.Directory.CreateDirectory(newPath);
|
|
}
|
|
|
|
string fName = newPath + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
|
|
|
|
FileInfo fileInfo = new FileInfo(fName);
|
|
|
|
if (fileInfo.Exists == false)
|
|
{
|
|
using (StreamWriter sw = fileInfo.CreateText())
|
|
{
|
|
sw.WriteLine("/********************************************************************************/");
|
|
sw.WriteLine("时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff"));
|
|
sw.WriteLine("信息:" + ex.Message);
|
|
sw.WriteLine("实例:" + ex.InnerException);
|
|
sw.WriteLine("方法:" + ex.TargetSite);
|
|
sw.WriteLine("堆栈:" + ex.StackTrace);
|
|
sw.WriteLine();
|
|
sw.Flush();
|
|
sw.Close();
|
|
}
|
|
FilesDelete(fileInfo.DirectoryName);
|
|
}
|
|
else
|
|
{
|
|
using (StreamWriter sw = fileInfo.AppendText())
|
|
{
|
|
sw.WriteLine("/********************************************************************************/");
|
|
sw.WriteLine("时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff"));
|
|
sw.WriteLine("信息:" + ex.Message);
|
|
sw.WriteLine("实例:" + ex.InnerException);
|
|
sw.WriteLine("方法:" + ex.TargetSite);
|
|
sw.WriteLine("堆栈:" + ex.StackTrace);
|
|
sw.WriteLine();
|
|
sw.Flush();
|
|
sw.Close();
|
|
}
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 接口收发日志
|
|
/// </summary>
|
|
/// <param name="strFunctionName">方法名</param>
|
|
/// <param name="strSend">发送</param>
|
|
/// <param name="strRecieve">接收</param>
|
|
public static void InterfaceLog(string strFunctionName, string strSend, string strRecieve)
|
|
{
|
|
try
|
|
{
|
|
string path = System.Environment.CurrentDirectory + "\\InterfaceLog\\";
|
|
string fName = DateTime.Now.ToString("yyyy-MM-dd") + ".log";
|
|
|
|
if (!System.IO.Directory.Exists(path))
|
|
{
|
|
System.IO.Directory.CreateDirectory(path);
|
|
}
|
|
|
|
FileInfo fileInfo = new FileInfo(path + fName);
|
|
|
|
if (fileInfo.Exists == false)
|
|
{
|
|
using (StreamWriter sw = fileInfo.CreateText())
|
|
{
|
|
sw.WriteLine("/********************************************************************************/");
|
|
sw.WriteLine("时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff"));
|
|
sw.WriteLine("方法:" + strFunctionName);
|
|
sw.WriteLine("发送:" + strSend);
|
|
sw.WriteLine("收到:" + strRecieve);
|
|
sw.WriteLine();
|
|
sw.Flush();
|
|
sw.Close();
|
|
}
|
|
FilesDelete(fileInfo.DirectoryName);
|
|
}
|
|
else
|
|
{
|
|
using (StreamWriter sw = fileInfo.AppendText())
|
|
{
|
|
sw.WriteLine("/********************************************************************************/");
|
|
sw.WriteLine("时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff"));
|
|
sw.WriteLine("方法:" + strFunctionName);
|
|
sw.WriteLine("发送:" + strSend);
|
|
sw.WriteLine("收到:" + strRecieve);
|
|
sw.WriteLine();
|
|
sw.Flush();
|
|
sw.Close();
|
|
}
|
|
}
|
|
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 操作日志
|
|
/// </summary>
|
|
/// <param name="messsage">日志信息</param>
|
|
public static void OperateLog(string dirName, string messsage)
|
|
{
|
|
try
|
|
{
|
|
string path = System.Environment.CurrentDirectory;
|
|
|
|
string newPath = path + "\\" + dirName + "\\";
|
|
|
|
if (!System.IO.Directory.Exists(newPath))
|
|
{
|
|
System.IO.Directory.CreateDirectory(newPath);
|
|
}
|
|
|
|
string fName = newPath + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
|
|
|
|
FileInfo fileInfo = new FileInfo(fName);
|
|
|
|
if (fileInfo.Exists == false)
|
|
{
|
|
using (StreamWriter sw = fileInfo.CreateText())
|
|
{
|
|
sw.WriteLine("时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff"));
|
|
sw.WriteLine("内容:" + messsage);
|
|
sw.WriteLine();
|
|
sw.Flush();
|
|
sw.Close();
|
|
}
|
|
FilesDelete(fileInfo.DirectoryName);
|
|
}
|
|
else
|
|
{
|
|
using (StreamWriter sw = fileInfo.AppendText())
|
|
{
|
|
sw.WriteLine("时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff"));
|
|
sw.WriteLine("内容:" + messsage);
|
|
sw.WriteLine();
|
|
sw.Flush();
|
|
sw.Close();
|
|
}
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建接口访问日志
|
|
/// </summary>
|
|
/// <param name="MethodName">方法名</param>
|
|
/// <param name="RequestJson">请求JSON</param>
|
|
/// <param name="ReturnJson">返回JSON</param>
|
|
public static void LogOperation(string methodName, string requestJson, string returnJson)
|
|
{
|
|
try
|
|
{
|
|
string path = System.Environment.CurrentDirectory;
|
|
|
|
string newPath = path + "\\" + methodName + "\\";
|
|
|
|
if (!System.IO.Directory.Exists(newPath))
|
|
{
|
|
System.IO.Directory.CreateDirectory(newPath);
|
|
}
|
|
|
|
string fName = newPath + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
|
|
|
|
FileInfo fileInfo = new FileInfo(fName);
|
|
if (fileInfo.Exists == false)
|
|
{
|
|
using (StreamWriter sw = fileInfo.CreateText())
|
|
{
|
|
sw.WriteLine("时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff"));
|
|
sw.WriteLine("请求:" + requestJson.Replace("\r\n", "").Replace("\n", ""));
|
|
sw.WriteLine("反馈:" + returnJson.Replace("\r\n", "").Replace("\n", ""));
|
|
sw.WriteLine();
|
|
sw.Flush();
|
|
sw.Close();
|
|
}
|
|
FilesDelete(fileInfo.DirectoryName);
|
|
}
|
|
else
|
|
{
|
|
using (StreamWriter sw = fileInfo.AppendText())
|
|
{
|
|
sw.WriteLine("时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ffff"));
|
|
sw.WriteLine("请求:" + requestJson.Replace("\r\n", "").Replace("\n", ""));
|
|
sw.WriteLine("反馈:" + returnJson.Replace("\r\n", "").Replace("\n", ""));
|
|
sw.WriteLine();
|
|
sw.Flush();
|
|
sw.Close();
|
|
}
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
}
|
|
}
|
|
private static void FilesDelete(string delURL, string suffix = ".log", int days = 30)
|
|
{
|
|
try
|
|
{
|
|
DirectoryInfo root = new DirectoryInfo(delURL);
|
|
FileInfo[] files = root.GetFiles();
|
|
if (files.Length <= 10)
|
|
{
|
|
return;
|
|
}
|
|
foreach (FileInfo fi in files)
|
|
{
|
|
if (fi.Extension.ToUpper() == suffix.ToUpper())
|
|
{
|
|
DateTime createTime = fi.CreationTime;
|
|
DateTime newTime = DateTime.Now.AddDays(-1);
|
|
if (Convert.ToDateTime(fi.CreationTime) <= DateTime.Now.AddDays(-days))
|
|
{
|
|
createTime = fi.CreationTime;
|
|
fi.Delete();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|