using Newtonsoft.Json;
|
using System;
|
using System.IO;
|
using System.Collections;
|
using System.Collections.Generic;
|
|
namespace iWareCommon.Utils
|
{
|
public class LogTextHelper
|
{
|
/// <summary>
|
/// 批次删除日志文件
|
/// </summary>
|
public static void BatchDeleteLog()
|
{
|
List<string> deleteFolder = new System.Collections.Generic.List<string>();
|
string folderPath = @"d:\\Log";
|
FolderHelper.ListDirectory(folderPath, 0, ref deleteFolder);
|
|
foreach (var item in deleteFolder)
|
{
|
FileHelper.DeleteOldFiles(item, 30);//删除一个月的数据
|
}
|
}
|
|
/// <summary>
|
/// 批次删除日志文件
|
/// </summary>
|
public static void BatchDeleteLog(string folderPath)
|
{
|
List<string> deleteFolder = new System.Collections.Generic.List<string>();
|
FolderHelper.ListDirectory(folderPath, 0, ref deleteFolder);
|
|
foreach (var item in deleteFolder)
|
{
|
FileHelper.DeleteOldFiles(item, 30);//删除一个月的数据
|
}
|
}
|
|
/// <summary>
|
/// 往日志里追加内容
|
/// </summary>
|
/// <param name="folder">日志目录</param>
|
/// <param name="logContent">内容</param>
|
private static void DoWriteLogContent(string folder, string logContent, Exception ex)
|
{
|
try
|
{
|
if (!Directory.Exists(folder))
|
{
|
Directory.CreateDirectory(folder);
|
}
|
string message = string.Format("【{0}】{1}\r\n", DateTime.Now.ToString("HH:mm:ss"), logContent);
|
if (ex != null)
|
{
|
message += ",异常堆栈:" + JsonConvert.SerializeObject(ex);
|
}
|
File.AppendAllText(Path.Combine(folder, DateTime.Now.ToString("yyyyMMdd") + ".txt"), message);
|
}
|
catch (Exception)
|
{
|
}
|
}
|
|
/// <summary>
|
/// 往日志里追加内容
|
/// </summary>
|
/// <param name="folder">日志目录</param>
|
/// <param name="format">内容格式</param>
|
/// <param name="args">内容参数</param>
|
public static void WriteLine(string folder, string format, params object[] args)
|
{
|
try
|
{
|
if (!Directory.Exists(folder))
|
{
|
Directory.CreateDirectory(folder);
|
}
|
string message = string.Format("【{0}】{1}\r\n", DateTime.Now.ToString("HH:mm:ss"), string.Format(format, args));
|
File.AppendAllText(Path.Combine(folder, DateTime.Now.ToString("yyyyMMdd") + ".txt"), message);
|
}
|
catch (Exception)
|
{
|
}
|
}
|
|
/// <summary>
|
/// 添加日志
|
/// </summary>
|
/// <param name="folder">日志目录</param>
|
/// <param name="className">类名</param>
|
/// <param name="methodName">方法名</param>
|
/// <param name="format">内容格式</param>
|
/// <param name="args">内容参数</param>
|
public static void WriteLog(string folder, string className, string methodName, string format, params object[] args)
|
{
|
WriteLine(folder, "在类{0}的{1}方法中:{2}", className, methodName, string.Format(format, args));
|
|
}
|
/// <summary>
|
/// 写入日志
|
/// </summary>
|
/// <param name="folder">日志目录</param>
|
/// <param name="className">类名</param>
|
/// <param name="methodName">方法名</param>
|
/// <param name="format">内容格式</param>
|
/// <param name="args">内容参数</param>
|
public static void WriteLogContent(string folder, string className, string methodName, string logContent, Exception ex)
|
{
|
DoWriteLogContent(folder, string.Format("在类{0}的{1}方法中:{2}", className, methodName, logContent), ex);
|
}
|
}
|
}
|