128 lines
5.1 KiB
C#
128 lines
5.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.IO;
|
|
|
|
namespace WebService.SDK
|
|
{
|
|
public class LogHelper
|
|
{
|
|
//设置存储日志文件的根目录
|
|
public static string path = System.AppDomain.CurrentDomain.BaseDirectory;
|
|
|
|
/// <summary>
|
|
/// 记录内容至文本文件
|
|
/// </summary>
|
|
/// <param name="LogData">日志内容</param>
|
|
/// <param name="LogPath">文件存储目录</param>
|
|
/// <param name="LogName">文件名称,默认命名为当前日期</param>
|
|
public static void WriteLog(string LogData, string LogPath, string LogName = "")
|
|
{
|
|
try
|
|
{
|
|
LogData = DateTime.Now.ToLongTimeString() + ":" + LogData;
|
|
//如果文件名称为空,则默认命名为当前日期
|
|
if (string.IsNullOrWhiteSpace(LogName))
|
|
{
|
|
LogName = DateTime.Now.ToString("yyyyMMdd") + ".log";
|
|
}
|
|
if (!Directory.Exists(path + LogPath))
|
|
{
|
|
Directory.CreateDirectory(path + LogPath);
|
|
}
|
|
//设置文件存储路径:
|
|
string filename = path + LogPath + "\\" + LogName;
|
|
//将文件内容写入流
|
|
FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
|
|
fs.Seek(0, SeekOrigin.End);
|
|
byte[] _data = new UTF8Encoding().GetBytes(LogData + "\r\n");
|
|
fs.Write(_data, 0, _data.Length);
|
|
fs.Flush();
|
|
fs.Close();
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 记录发送的内容至文本文件
|
|
/// </summary>
|
|
/// <param name="LogData">日志内容</param>
|
|
public static void WriteSendLog(string LogData)
|
|
{
|
|
try
|
|
{
|
|
LogData = DateTime.Now.ToLongTimeString() + ":" + LogData;
|
|
string LogName = "日志_Send" + System.DateTime.Now.Year.ToString() + "-" + System.DateTime.Now.Month.ToString() +
|
|
"-" + System.DateTime.Now.Day.ToString() + ".log";
|
|
string filename = path + "\\" + LogName;
|
|
FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
|
|
fs.Seek(0, SeekOrigin.End);
|
|
byte[] _data = new UTF8Encoding().GetBytes(LogData + "\r\n");
|
|
fs.Write(_data, 0, _data.Length);
|
|
fs.Flush();
|
|
fs.Close();
|
|
MoveFiles("日志_Send*", "Log", -5);
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 记录接收的内容至文本文件
|
|
/// </summary>
|
|
/// <param name="LogData"></param>
|
|
public static void WriteReceiveLog(string LogData)
|
|
{
|
|
try
|
|
{
|
|
LogData = DateTime.Now.ToLongTimeString() + ":" + LogData;
|
|
string LogName = "日志_Receive" + System.DateTime.Now.Year.ToString() + "-" + System.DateTime.Now.Month.ToString() +
|
|
"-" + System.DateTime.Now.Day.ToString() + ".log";
|
|
string filename = path + "\\" + LogName;
|
|
FileStream fs = new FileStream(filename, FileMode.OpenOrCreate);
|
|
fs.Seek(0, SeekOrigin.End);
|
|
byte[] _data = new UTF8Encoding().GetBytes(LogData + "\r\n");
|
|
fs.Write(_data, 0, _data.Length);
|
|
fs.Flush();
|
|
fs.Close();
|
|
MoveFiles("日志_Receive*", "Log", -5);
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
public static void DeleteFiles(string FileName, string FilePath = "", int DeleteDays = -2)
|
|
{
|
|
FilePath = FilePath == "" ? path : FilePath;
|
|
DirectoryInfo _DirectoryInfo = new DirectoryInfo(FilePath);
|
|
foreach (FileInfo _FileInfo in _DirectoryInfo.GetFiles(FileName, SearchOption.TopDirectoryOnly))
|
|
{
|
|
if (_FileInfo.LastWriteTime < DateTime.Now.Date.AddDays(DeleteDays))
|
|
{
|
|
_FileInfo.Delete();
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void MoveFiles(string FileName, string FilePath = "Log", int DeleteDays = -30, string MovePath = "")
|
|
{
|
|
MovePath = MovePath == "" ? path : MovePath;
|
|
if (!Directory.Exists(MovePath + "\\" + FilePath))
|
|
{
|
|
Directory.CreateDirectory(MovePath + "\\" + FilePath);
|
|
}
|
|
DirectoryInfo _DirectoryInfo = new DirectoryInfo(MovePath);
|
|
foreach (FileInfo _FileInfo in _DirectoryInfo.GetFiles(FileName, SearchOption.TopDirectoryOnly))
|
|
{
|
|
if (_FileInfo.LastWriteTime < DateTime.Now.Date.AddDays(DeleteDays))
|
|
{
|
|
if (File.Exists(MovePath + "\\" + FilePath + "\\" + _FileInfo.Name))
|
|
{
|
|
File.Delete(MovePath + "\\" + FilePath + "\\" + _FileInfo.Name);
|
|
}
|
|
_FileInfo.MoveTo(MovePath + "\\" + FilePath + "\\" + _FileInfo.Name);
|
|
//_FileInfo.Delete();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|