using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Windows.Forms; namespace CommoditySaleDataFrom { public class LogHelper { public static string path = Application.StartupPath; public static void WriteSendLog(string LogData, string LogName = "", bool IsDeleteFile = false) { try { if (FilterContent(LogData)) { return; } if (IsDeleteFile) { DeleteFiles("日志_Send*.log", path, -10); } //LogData = DateTime.Now.ToLongTimeString() + ":" + LogData; LogName = (!string.IsNullOrEmpty(LogName) ? LogName : "日志_Send" + DateTime.Now.ToString("yyyyMMddHHmmss")) + ".log"; string filename = path + "\\" + LogName; FileStream fs = new FileStream(filename, FileMode.OpenOrCreate); fs.Seek(0, SeekOrigin.End); byte[] _data = new UTF8Encoding().GetBytes(DateTime.Now + ":" + LogData + "\r\n"); fs.Write(_data, 0, _data.Length); fs.Flush(); fs.Close(); } catch (Exception ex) { } } public static void WriteReceiveLog(string LogData, string LogName = "", bool IsDeleteFile = false) { try { if (FilterContent(LogData)) { return; } if (IsDeleteFile) { DeleteFiles("日志_Receive*.log", path, -10); } //LogData = DateTime.Now.ToLongTimeString() + ":" + LogData; LogName = (!string.IsNullOrEmpty(LogName) ? LogName : "日志_Receive" + DateTime.Now.ToString("yyyyMMddHHmmss")) + ".log"; string filename = path + "\\" + LogName; FileStream fs = new FileStream(filename, FileMode.OpenOrCreate); fs.Seek(0, SeekOrigin.End); byte[] _data = new UTF8Encoding().GetBytes(DateTime.Now + ":" + LogData + "\r\n"); fs.Write(_data, 0, _data.Length); fs.Flush(); fs.Close(); } catch { } } public static void DeleteFiles(string FileName, string FilePath = "", int DeleteDays = -2) { try { 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(); } } } catch { } } public static bool FilterContent(string Content) { if (Content.StartsWith("远程主机强迫关闭了一个现有的连接") || Content.ToUpper().Contains("ORA-01033") || Content.StartsWith("由于目标计算机积极拒绝,无法连接") || Content.ToUpper().Contains("ORA-01089") || Content.ToUpper().Contains("ORA-02067") || Content.ToUpper().Contains("ORA-03113") || Content.ToUpper().Contains("ORA-03114") || Content.ToUpper().Contains("ORA-03135") || Content.ToUpper().Contains("ORA-12514") || Content.ToUpper().Contains("ORA-12520") || Content.ToUpper().Contains("ORA-12528") || Content.ToUpper().Contains("ORA-12541") || Content.ToUpper().Contains("ORA-12547") || Content.ToUpper().Contains("ORA-12571") || Content.ToUpper().Contains("ORA-12170") || Content.ToUpper().Contains("监听程序无法分发客户机连接") || Content.StartsWith("由于连接方在一段时间后没有正确答复或连接的主机没有反应") || Content.StartsWith("在某个线程上创建的控件不能成为在另一个线程上创建的控件的父级") || Content.Contains("输入流是无效的二进制格式。开始内容(以字节为单位)是:") || Content.Contains("二进制流“0”不包含有效的 BinaryHeader。这可能是由于无效流,或由于在序列化和反序列化之间的对象版本更改。")) { return true; } else { return false; } } } }