using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Runtime.InteropServices; namespace libEShangPB { [ComVisible(false)] public class LogHelper { public static string path = System.AppDomain.CurrentDomain.BaseDirectory; public static void WriteSellDataLog(string sellData, string logFileName = "") { try { if (string.IsNullOrWhiteSpace(logFileName)) { logFileName = DateTime.Today.ToString("yyyy-MM-dd", System.Globalization.DateTimeFormatInfo.InvariantInfo) + ".log"; } if (!Directory.Exists(path + "\\selldata")) { Directory.CreateDirectory(path + "\\selldata"); } FileStream fileStream = new FileStream(path + "\\selldata\\" + logFileName, FileMode.OpenOrCreate); fileStream.Seek(0, SeekOrigin.End); byte[] _data = new UTF8Encoding().GetBytes(sellData + "\r\n"); fileStream.Write(_data, 0, _data.Length); fileStream.Flush(); fileStream.Close(); } catch { } } public static void WriteServiceLog(string LogData, string LogFileName = "") { try { LogData = DateTime.Now.ToLongTimeString() + ":" + LogData; string LogName = "错误日志_" + System.DateTime.Now.Year.ToString() + "-" + System.DateTime.Now.Month.ToString() + "-" + System.DateTime.Now.Day.ToString() + ".log"; if (LogFileName != "") { LogName = LogFileName; } if (Directory.Exists(path + "\\" + "log"))//判断是否存在 { } else { Directory.CreateDirectory(path + "\\" + "log");//创建新路径 } string filename = path + "\\" + "log" + "\\" + LogName; FileStream fs = new FileStream(filename, FileMode.OpenOrCreate); fs.Seek(0, SeekOrigin.End); byte[] _data = new UTF8Encoding().GetBytes(LogData + "\r\n\r\n"); fs.Write(_data, 0, _data.Length); fs.Flush(); fs.Close(); } catch { } } public static void WriteServiceLog2(string LogData) { LogData = DateTime.Now.ToLongTimeString() + ":" + LogData; string LogName = "日志_Success" + System.DateTime.Now.Year.ToString() + "-" + System.DateTime.Now.Month.ToString() + "-" + System.DateTime.Now.Day.ToString() + ".log"; string filename = path + "\\" + LogName; FileStream fs = new FileStream(filename, FileMode.OpenOrCreate); fs.Seek(0, SeekOrigin.End); byte[] _data = new UTF8Encoding().GetBytes(LogData + "\r\n"); fs.Write(_data, 0, _data.Length); fs.Flush(); fs.Close(); } } }