202 lines
8.0 KiB
C#
202 lines
8.0 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Text;
|
||
|
||
namespace Transmission.SDK
|
||
{
|
||
public class LogHelper
|
||
{
|
||
public static string path = AppDomain.CurrentDomain.BaseDirectory;
|
||
private static Dictionary<long, long> lockDic = new Dictionary<long, long>();
|
||
|
||
#region 方法 -> 记录接收的内容到指定目录文本文件
|
||
/// <summary>
|
||
/// 记录接收的内容到指定目录文本文件
|
||
/// </summary>
|
||
/// <param name="LogData">记录内容</param>
|
||
public static void WriteServiceLog(string LogData)
|
||
{
|
||
LogData = DateTime.Now.ToLongTimeString() + ":" + LogData;
|
||
string LogName = "日志_Send" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
|
||
string filename = Path.Combine(path, "log", LogName);
|
||
if (!Directory.Exists(Path.Combine(path, "log")))
|
||
Directory.CreateDirectory(Path.Combine(path, "log"));
|
||
try
|
||
{
|
||
using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate,
|
||
FileAccess.ReadWrite, FileShare.ReadWrite, 8, FileOptions.Asynchronous))
|
||
{
|
||
Byte[] _data = new UTF8Encoding().GetBytes(LogData + Environment.NewLine);
|
||
bool flag = true;
|
||
long slen = _data.Length;
|
||
long len = 0;
|
||
while (flag)
|
||
{
|
||
try
|
||
{
|
||
if (len >= fs.Length)
|
||
{
|
||
fs.Lock(len, slen);
|
||
lockDic[len] = slen;
|
||
flag = false;
|
||
}
|
||
else
|
||
{
|
||
len = fs.Length;
|
||
}
|
||
}
|
||
catch
|
||
{
|
||
while (!lockDic.ContainsKey(len))
|
||
{
|
||
len += lockDic[len];
|
||
}
|
||
}
|
||
}
|
||
fs.Seek(0, SeekOrigin.End);
|
||
fs.Write(_data, 0, _data.Length);
|
||
fs.Flush();
|
||
fs.Close();
|
||
}
|
||
}
|
||
catch { }
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 删除指定目录下的指定后缀名的文件
|
||
/// <summary>
|
||
/// 删除指定目录下的指定后缀名的文件
|
||
/// </summary>
|
||
/// <param name="directory">要删除的文件所在的目录,是绝对目录,如d:\temp</param>
|
||
/// <param name="masks">要删除的文件的后缀名的一个数组,比如masks中包含了.cs,.vb,.c这三个元素</param>
|
||
/// <param name="searchSubdirectories">表示是否需要递归删除,即是否也要删除子目录中相应的文件</param>
|
||
/// <param name="ignoreHidden">表示是否忽略隐藏文件</param>
|
||
/// /// <param name="deletedFileCount">表示总共删除的文件数</param>
|
||
public static void DeleteFiles(string directory, string[] masks, bool searchSubdirectories, bool ignoreHidden, ref int deletedFileCount)
|
||
{
|
||
//先删除当前目录下指定后缀名的所有文件
|
||
foreach (string file in Directory.GetFiles(directory, "*.*"))
|
||
{
|
||
if (!(ignoreHidden && (File.GetAttributes(file) & FileAttributes.Hidden) == FileAttributes.Hidden))
|
||
{
|
||
foreach (string mask in masks)
|
||
{
|
||
if (Path.GetExtension(file) == mask)
|
||
{
|
||
FileInfo _FileInfo = new FileInfo(file);
|
||
if (_FileInfo.LastWriteTime < DateTime.Now.Date.AddDays(-10))
|
||
{
|
||
_FileInfo.Delete();
|
||
deletedFileCount++;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
//如果需要对子目录进行处理,则对子目录也进行递归操作
|
||
if (searchSubdirectories)
|
||
{
|
||
string[] childDirectories = Directory.GetDirectories(directory);
|
||
foreach (string dir in childDirectories)
|
||
{
|
||
if (!(ignoreHidden && (File.GetAttributes(dir) & FileAttributes.Hidden) == FileAttributes.Hidden))
|
||
{
|
||
DeleteFiles(dir, masks, searchSubdirectories, ignoreHidden, ref deletedFileCount);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 删除指定目录下的所有文件及文件夹
|
||
/// <summary>
|
||
/// 删除指定目录下的所有文件及文件夹
|
||
/// </summary>
|
||
/// <param name="strPath">待删除目录</param>
|
||
/// <param name="isReservedDir">是否保留目录</param>
|
||
/// <returns>删除结果</returns>
|
||
public static bool DeleteDirectory(string strPath, bool isReservedDir = true)
|
||
{
|
||
try
|
||
{
|
||
// 清除空格
|
||
strPath = @strPath.Trim().ToString();
|
||
// 判断文件夹是否存在
|
||
if (Directory.Exists(strPath))
|
||
{
|
||
// 获得文件夹数组
|
||
string[] strDirs = Directory.GetDirectories(strPath);
|
||
// 获得文件数组
|
||
string[] strFiles = Directory.GetFiles(strPath);
|
||
// 遍历所有子文件夹
|
||
foreach (string strFile in strFiles)
|
||
{
|
||
// 删除文件
|
||
File.Delete(strFile);
|
||
}
|
||
// 遍历所有文件
|
||
foreach (string strdir in strDirs)
|
||
{
|
||
// 删除文件夹
|
||
Directory.Delete(strdir, true);
|
||
}
|
||
if (!isReservedDir)
|
||
{
|
||
Directory.Delete(strPath);
|
||
}
|
||
}
|
||
// 成功
|
||
return true;
|
||
}
|
||
catch (Exception) // 异常处理
|
||
{
|
||
// 失败
|
||
return false;
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 记录日志文件
|
||
/// <summary>
|
||
/// 记录日志文件
|
||
/// </summary>
|
||
/// <param name="LogMsg">日志内容</param>
|
||
/// <param name="FileName">日志文件名称</param>
|
||
/// <param name="DirectoryName">日志文件路径</param>
|
||
public static void WriteLog(string LogMsg, string FileName = "", string DirectoryName = "")
|
||
{
|
||
string str = "";
|
||
try
|
||
{
|
||
if (string.IsNullOrEmpty(FileName))
|
||
{
|
||
FileName = DateTime.Now.ToString("yyyyMMdd");
|
||
}
|
||
if (string.IsNullOrWhiteSpace(DirectoryName))
|
||
{
|
||
DirectoryName = AppDomain.CurrentDomain.BaseDirectory + "log";
|
||
}
|
||
str += string.Format(@"{0}", DateTime.Now.ToString());
|
||
|
||
if (!string.IsNullOrWhiteSpace(LogMsg))
|
||
{
|
||
str += string.Format(" {0}", LogMsg);
|
||
}
|
||
|
||
if (!Directory.Exists(DirectoryName))//判断文件夹是否存在
|
||
{
|
||
Directory.CreateDirectory(DirectoryName);//不存在则创建文件夹
|
||
}
|
||
string filePath = DirectoryName + "\\" + FileName + ".log";
|
||
StreamWriter sw = new StreamWriter(filePath, true);
|
||
sw.WriteLine(str);
|
||
sw.Close();
|
||
}
|
||
catch { }
|
||
}
|
||
#endregion
|
||
}
|
||
}
|