You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
3.5 KiB
C#
95 lines
3.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.IO;
|
|
|
|
namespace NetLibrary.Log
|
|
{
|
|
public class ErrorFollow
|
|
{
|
|
public static bool IsDebug = true;
|
|
#region 输出错误信息
|
|
/// <summary>
|
|
/// 输出错误信息
|
|
/// </summary>
|
|
public static void TraceWrite(string TypeName, string StackTrace, string ErrorMsg)
|
|
{
|
|
if (ErrorFollow.IsDebug == false) return;
|
|
if (string.IsNullOrEmpty(ErrorMsg) == true) ErrorMsg="消息为空字符串";
|
|
try
|
|
{
|
|
string DirectoryName = AppDomain.CurrentDomain.BaseDirectory + "ServerCookies";
|
|
if (Directory.Exists(DirectoryName) == false) Directory.CreateDirectory(DirectoryName);
|
|
string filePath = DirectoryName + "/" + DateTime.Today.ToString("yyyy-MM-dd") + "EventLog.log";
|
|
bool append = true;
|
|
if (File.Exists(filePath) == false) File.Create(filePath).Close();
|
|
FileInfo f = new FileInfo(filePath);
|
|
if (f.Length / 1024 / 1024 > 1)
|
|
{
|
|
append = false;
|
|
string newFile=DirectoryName+"/"+DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".log";
|
|
f.CopyTo(newFile, true);
|
|
}
|
|
f = null;
|
|
using (StreamWriter sw = new StreamWriter(filePath, append, System.Text.Encoding.Default))
|
|
{
|
|
sw.WriteLine("------------");
|
|
sw.WriteLine(TypeName);
|
|
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
|
|
sw.WriteLine(StackTrace);
|
|
sw.WriteLine(ErrorMsg);
|
|
sw.Close();
|
|
}
|
|
}
|
|
catch { }
|
|
}
|
|
#endregion
|
|
|
|
#region 输出错误信息
|
|
/// <summary>
|
|
/// 输出错误信息
|
|
/// </summary>
|
|
public static void TraceWrite(string filePath, string TypeName, string StackTrace, string ErrorMsg)
|
|
{
|
|
if (ErrorFollow.IsDebug == false) return;
|
|
if (string.IsNullOrEmpty(ErrorMsg) == true) ErrorMsg = "未知错误";
|
|
try
|
|
{
|
|
bool append = true;
|
|
if (File.Exists(filePath) == false) File.Create(filePath).Close();
|
|
FileInfo f = new FileInfo(filePath);
|
|
if (f.Length / 1024 / 32 > 10) append = false;
|
|
f = null;
|
|
using (StreamWriter sw = new StreamWriter(filePath, append, System.Text.Encoding.Default))
|
|
{
|
|
sw.WriteLine("------------");
|
|
sw.WriteLine(TypeName);
|
|
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
|
|
sw.WriteLine(StackTrace);
|
|
sw.WriteLine(ErrorMsg);
|
|
sw.Close();
|
|
}
|
|
}
|
|
catch { }
|
|
}
|
|
#endregion
|
|
|
|
#region 清理错误日志
|
|
public static void DeleteLog(int Day)
|
|
{
|
|
string DirectoryName = AppDomain.CurrentDomain.BaseDirectory + "ServerCookies/";
|
|
if (Directory.Exists(DirectoryName) == false) return;
|
|
string[] files = Directory.GetFiles(DirectoryName, "*.log");
|
|
foreach (string filePath in files)
|
|
{
|
|
DateTime dt = File.GetCreationTime(filePath);
|
|
if (dt.AddDays(Day) > DateTime.Now) continue;
|
|
File.Delete(filePath);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
|
|
}
|
|
}
|