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.
390 lines
16 KiB
C#
390 lines
16 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.IO;
|
|
using HuizhongLibrary.Data;
|
|
using System.Data.Common;
|
|
using System.Data;
|
|
using System.Diagnostics;
|
|
|
|
namespace HuizhongLibrary.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") + "EventLog.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"));
|
|
if (string.IsNullOrEmpty(StackTrace) == false) sw.WriteLine(StackTrace);
|
|
sw.WriteLine(ErrorMsg);
|
|
sw.Close();
|
|
}
|
|
if (ErrorMsg.Contains("OutOfMemoryException") == true)
|
|
{
|
|
string file2 = AppDomain.CurrentDomain.BaseDirectory + "自动启动服务脚本/hz.bat";
|
|
CustomIO.ReStartService(file2);
|
|
}
|
|
}
|
|
catch { }
|
|
}
|
|
#endregion
|
|
#region 输出错误信息
|
|
/// <summary>
|
|
/// 输出错误信息
|
|
/// </summary>
|
|
public static void TraceWrite2(string FileName,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 + "/" + FileName + 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 + "/" + FileName + DateTime.Now.ToString("yyyyMMdd_HHmmss") + "EventLog.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"));
|
|
if (string.IsNullOrEmpty(StackTrace) == false) sw.WriteLine(StackTrace);
|
|
sw.WriteLine(ErrorMsg);
|
|
sw.Close();
|
|
}
|
|
if (ErrorMsg.Contains("OutOfMemoryException") == true)
|
|
{
|
|
string file2 = AppDomain.CurrentDomain.BaseDirectory + "自动启动服务脚本/hz.bat";
|
|
CustomIO.ReStartService(file2);
|
|
}
|
|
}
|
|
catch { }
|
|
}
|
|
#endregion
|
|
#region 输出错误信息_并且保存到数据库
|
|
/// <summary>
|
|
/// 输出错误信息_并且保存到数据库
|
|
/// </summary>
|
|
public static void TraceWriteError(string TypeName, Exception ex, string DataMsg)
|
|
{
|
|
if (string.IsNullOrEmpty(DataMsg) == true && ex == null) DataMsg = "消息为空字符串";
|
|
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") + "EventLog.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"));
|
|
if (ex != null)
|
|
{
|
|
if (string.IsNullOrEmpty(ex.StackTrace) == false) sw.WriteLine(ex.StackTrace);
|
|
sw.WriteLine(ex.Message);
|
|
}
|
|
if (string.IsNullOrEmpty(DataMsg) == false) sw.WriteLine(DataMsg);
|
|
sw.Close();
|
|
}
|
|
string tsql = @"
|
|
if (select count(0) from PD_ErrorLog where TypeName=@TypeName and ErrorMsg=@ErrorMsg and DATEDIFF(s,InDate,getdate())<300)=0
|
|
begin
|
|
INSERT INTO [PD_ErrorLog]([TypeName],[StackTrace],[ErrorMsg],[DataMsg],[InDate])values(@TypeName,@StackTrace,@ErrorMsg,@DataMsg,getdate())
|
|
end
|
|
";
|
|
Database db = DatabaseFactory.CreateDatabase();
|
|
DbCommand cmd = db.GetSqlStringCommand(tsql);
|
|
db.AddInParameter(cmd, "@TypeName", DbType.String, TypeName);
|
|
string StackTrace = "";
|
|
string ErrorMsg = "";
|
|
if (ex != null)
|
|
{
|
|
StackTrace = ex.StackTrace;
|
|
ErrorMsg = ex.Message;
|
|
}
|
|
if (ErrorMsg == "") ErrorMsg = DataMsg;
|
|
db.AddInParameter(cmd, "@StackTrace", DbType.String, StackTrace);
|
|
db.AddInParameter(cmd, "@ErrorMsg", DbType.String, ErrorMsg);
|
|
db.AddInParameter(cmd, "@DataMsg", DbType.String, DataMsg);
|
|
try
|
|
{
|
|
db.ExecuteNonQuery(cmd);
|
|
}
|
|
catch
|
|
{ }
|
|
}
|
|
catch { }
|
|
}
|
|
#endregion
|
|
#region 输出10.4挂号收费信息
|
|
/// <summary>
|
|
/// 输出错误信息
|
|
/// </summary>
|
|
public static void TraceWrite_GH(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") + "挂号收费日志.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") + "EventLog.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"));
|
|
if (string.IsNullOrEmpty(StackTrace) == false) 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();
|
|
}
|
|
if (ErrorMsg.Contains("OutOfMemoryException") == true)
|
|
{
|
|
string file2 = AppDomain.CurrentDomain.BaseDirectory + "自动启动服务脚本/hz.bat";
|
|
CustomIO.ReStartService(file2);
|
|
}
|
|
}
|
|
catch { }
|
|
}
|
|
#endregion
|
|
|
|
#region 清理错误日志
|
|
public static void DeleteLog(int Day)
|
|
{
|
|
Action<int> act = new Action<int>(DeleteTempFile);
|
|
act.BeginInvoke(Day, null, null);
|
|
}
|
|
public static void DeleteTempFile(int Day)
|
|
{
|
|
try
|
|
{
|
|
string DirectoryName = AppDomain.CurrentDomain.BaseDirectory + "ServerCookies/";
|
|
if (Directory.Exists(DirectoryName) == false) return;
|
|
//string[] files = Directory.GetFiles(DirectoryName, "*.*");
|
|
//foreach (string filePath in files)
|
|
//{
|
|
// DateTime dt = File.GetCreationTime(filePath);
|
|
// if (dt.AddDays(Day) > DateTime.Now) continue;
|
|
// File.Delete(filePath);
|
|
//}
|
|
DeleteFile(DirectoryName, Day);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorFollow.TraceWrite(ex.TargetSite.Name, ex.StackTrace, ex.Message);
|
|
}
|
|
}
|
|
#region 删除文件夹及下面所有文件
|
|
public static void DeleteFile(string Directory,int Day)
|
|
{
|
|
ErrorFollow obj = new ErrorFollow();
|
|
obj.DeleteFileTree(Directory, true, 0, Day);
|
|
foreach (string s in obj.files)
|
|
{
|
|
System.IO.Directory.Delete(s);
|
|
TraceWrite("删除临时日志文件", "", s);
|
|
}
|
|
}
|
|
|
|
List<string> files = new List<string>();
|
|
void DeleteFileTree(string dir, bool showFiles, int level, int Day)
|
|
{
|
|
try
|
|
{
|
|
if (showFiles == true)
|
|
{
|
|
foreach (string fname in Directory.GetFiles(dir))
|
|
{
|
|
DateTime dt = File.GetLastWriteTime(fname);
|
|
if (dt.AddDays(Day) > DateTime.Now) continue;
|
|
System.IO.File.Delete(fname);
|
|
TraceWrite("删除临时日志文件", "", fname);
|
|
}
|
|
}
|
|
|
|
foreach (string subdir in Directory.GetDirectories(dir))
|
|
{
|
|
DeleteFileTree(subdir, showFiles, level + 1,Day);
|
|
string[] files2 = Directory.GetFiles(subdir, "*.*");
|
|
if (files2.Length==0) files.Add(subdir);
|
|
}
|
|
|
|
}
|
|
catch
|
|
{}
|
|
}
|
|
#endregion
|
|
#endregion
|
|
|
|
#region 输出接口信息
|
|
/// <summary>
|
|
/// 输出接口信息
|
|
/// </summary>
|
|
public static void TraceInfoWrite(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") + "EventInfoLog.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") + "EventInfoLog.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"));
|
|
if (string.IsNullOrEmpty(StackTrace) == false) sw.WriteLine(StackTrace);
|
|
sw.WriteLine(ErrorMsg);
|
|
sw.Close();
|
|
}
|
|
}
|
|
catch { }
|
|
}
|
|
#endregion
|
|
#region 日志文件保存数据库
|
|
public static void SaveEventLog(string DeviceNo, DateTime InDate)
|
|
{
|
|
string InDate2 = InDate.ToString("yyyy-MM-dd");
|
|
string dir = AppDomain.CurrentDomain.BaseDirectory + "ServerCookies";
|
|
string[] files = Directory.GetFiles(dir, "*.log");
|
|
foreach (string filePath in files)
|
|
{
|
|
DateTime dt = File.GetCreationTime(filePath);
|
|
string dt2 = dt.ToString("yyyy-MM-dd");
|
|
if (dt2 != InDate2) continue;
|
|
byte[] bytes = CustomIO.GetOffice(filePath);
|
|
SaveEventLogData(DeviceNo, dt, bytes);
|
|
}
|
|
}
|
|
public static void SaveEventLogData(string DeviceNo,DateTime CreateTime,byte[] bytes)
|
|
{
|
|
string tsql = @"
|
|
select id from PD_EventLog where DeviceNo=@DeviceNo and InDate=@InDate
|
|
";
|
|
Database db = DatabaseFactory.CreateDatabase();
|
|
DbCommand cmd = db.GetSqlStringCommand(tsql);
|
|
db.AddInParameter(cmd, "@DeviceNo", DbType.String, DeviceNo);
|
|
db.AddInParameter(cmd, "@InDate", DbType.DateTime, CreateTime);
|
|
string a =Convert.ToString(db.ExecuteScalar(cmd));
|
|
if (string.IsNullOrEmpty(a) == true)
|
|
{
|
|
tsql = @"
|
|
insert PD_EventLog(DeviceNo,Filelog,InDate)values(@DeviceNo,@Filelog,@InDate)
|
|
";
|
|
cmd = db.GetSqlStringCommand(tsql);
|
|
db.AddInParameter(cmd, "@DeviceNo", DbType.String, DeviceNo);
|
|
db.AddInParameter(cmd, "@InDate", DbType.DateTime, CreateTime);
|
|
db.AddInParameter(cmd, "@Filelog", DbType.Binary, bytes);
|
|
db.ExecuteNonQuery(cmd);
|
|
}
|
|
else
|
|
{
|
|
tsql = @"
|
|
update PD_EventLog set Filelog=@Filelog where id=@id
|
|
";
|
|
cmd = db.GetSqlStringCommand(tsql);
|
|
db.AddInParameter(cmd, "@id", DbType.Int32, a);
|
|
db.AddInParameter(cmd, "@Filelog", DbType.Binary, bytes);
|
|
db.ExecuteNonQuery(cmd);
|
|
}
|
|
|
|
}
|
|
#endregion
|
|
|
|
|
|
}
|
|
}
|