静态类,初始化调用InitLogApp()
其他任意位置调用LogWrite();
public class LogApp
{
private static string strLocalFilePath = "";
private static string strLocalFile = "logfile";
private static string strErrorFile = "logEror";
private static bool IsInited = false;
public static void (string strFilePath)
{
try
{
strLocalFilePath = strFilePath + "logs";
string strdir = strLocalFilePath; //strLocalFile.Substring(0, strLocalFile.LastIndexOf("\\"));
if (!System.IO.Directory.Exists(strdir))
System.IO.Directory.CreateDirectory(strdir);
IsInited = true;
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ": " + strLocalFilePath);
}
catch { }
}
public static void LogWrite(string strval)
{
if (!IsInited) { InitLogApp(System.Windows.Forms.Application.StartupPath + "\\"); };
try
{
if (!Directory.Exists(strLocalFilePath)) Directory.CreateDirectory(strLocalFilePath);
System.IO.StreamWriter sw = System.IO.File.AppendText(strLocalFilePath + "\\" + strLocalFile + DateTime.Now.ToString("yyyyMMddHH") + ".log");
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ": " + strval);
sw.Close();
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ": " + strval);
}
catch { }
}
public static void LogError(string strval)
{
if (!IsInited) { InitLogApp(System.Windows.Forms.Application.StartupPath + "\\"); };
try
{
if (!Directory.Exists(strLocalFilePath)) Directory.CreateDirectory(strLocalFilePath);
System.IO.StreamWriter sw = System.IO.File.AppendText(strLocalFilePath + "\\" + strErrorFile + DateTime.Now.ToString("yyyyMMddHH") + ".log");
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ": " + strval);
sw.Close();
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ": " + strval);
}
catch { }
}
public static void LogFileClear(int day)
{
if (!IsInited) { InitLogApp(System.Windows.Forms.Application.StartupPath + "\\"); };
try
{
System.Collections.Hashtable htFileInfos = DIR.GetAllFiles(strLocalFilePath);
//string[] files = Directory.GetFiles(strLocalFilePath);//得到文件
foreach (System.Collections.DictionaryEntry de in htFileInfos)//循环文件
{
FileInfo file = (FileInfo)de.Value;
string exname = file.Name.Substring(0, file.Name.LastIndexOf("."));//得到后缀名
//string exname = exname1.Substring(exname1.LastIndexOf(".") + 1);//得到后缀名
//string strfnDateTime = exname.Substring(7, 8).Insert(6, "-").Insert(4, "-");
DateTime timeFile = Convert.ToDateTime(file.LastWriteTime);
DateTime timeNow = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
if (timeFile.AddDays(day) < timeNow)
{
try
{
File.Delete(file.FullName);
}
catch { }
}
}
}
catch
{
}
}
public static void LogWrite(string strval, string strFlag)
{
if (!IsInited) { InitLogApp(System.Windows.Forms.Application.StartupPath + "\\"); };
try
{
string strFile = strLocalFilePath + "\\" + strLocalFile + DateTime.Now.ToString("yyyyMMddHH") + ".log";
if (strFlag != "")
{
string strPath = strLocalFilePath + "\\" + strFlag + "\\";
if (!Directory.Exists(strPath)) Directory.CreateDirectory(strPath);
strFile = strLocalFilePath + "\\" + strFlag + "\\" + strLocalFile + DateTime.Now.ToString("yyyyMMddHH") + ".log";
}
System.IO.StreamWriter sw = System.IO.File.AppendText(strFile);
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ": " + strval);
sw.Close();
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ": " + strval);
}
catch { }
}
public static void LogError(string strval, string strFlag)
{
if (!IsInited) { InitLogApp(System.Windows.Forms.Application.StartupPath + "\\"); };
try
{
string strFile = strLocalFilePath + "\\" + strErrorFile + DateTime.Now.ToString("yyyyMMddHH") + ".log";
if (strFlag != "")
{
string strPath = strLocalFilePath + "\\" + strFlag + "\\";
if (!Directory.Exists(strPath)) Directory.CreateDirectory(strPath);
strFile = strLocalFilePath + "\\" + strFlag + "\\" + strErrorFile + DateTime.Now.ToString("yyyyMMddHH") + ".log";
}
System.IO.StreamWriter sw = System.IO.File.AppendText(strFile);
// System.IO.StreamWriter sw = System.IO.File.AppendText(strLocalFilePath + "\\" + strErrorFile + DateTime.Now.ToString("yyyyMMddHH") + ".log");
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ": " + strval);
sw.Close();
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + ": " + strval);
}
catch { }
}
public static void WriteErrorLog(string class_name, string method_name, string message_info)
{
StringBuilder strInfo = new StringBuilder();
string strLogPath = strLocalFilePath + @"\LogDBInterface\";
strLogPath = strLogPath.TrimEnd('\\') + "\\";
if (!Directory.Exists(strLogPath)) Directory.CreateDirectory(strLogPath);
string strOutPutFile = strLogPath + @"ErrorLog" + DateTime.Now.ToString("yyyyMMdd") + @".csv";
//strOutPutFile = mstrLogPath + @"\ErrorLog" + DateTime.Now.ToString("yyyyMMdd") + @".csv";
StreamWriter swCsv = new StreamWriter(strOutPutFile, true, System.Text.Encoding.GetEncoding("GB2312"));
strInfo.Append(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + ",");
strInfo.Append(class_name + ",");
strInfo.Append(method_name + ",");
strInfo.Append("(" + message_info + ")");
swCsv.WriteLine(strInfo.ToString());
swCsv.Close();
}
public static void WriteSQLErrorLog(string class_name, string method_name, string strSql, string message_info, string strPath = "")
{
StringBuilder strInfo = new StringBuilder();
string strLogPath = "";
if (strPath == "")
strLogPath = strLocalFilePath + @"\LogDBInterface\";
else
strLogPath = strLocalFilePath + "\\" + strPath + @"\LogDBInterface\";
strLogPath = strLogPath.TrimEnd('\\') + "\\";
if (!Directory.Exists(strLogPath)) Directory.CreateDirectory(strLogPath);
string strOutPutFile = strLogPath + @"ErrorLog" + DateTime.Now.ToString("yyyyMMdd") + @".csv";
//string strOutPutFile = Application.StartupPath + @"\LogDBInterface\" + mstrLogPath + @"ErrorLog" + DateTime.Now.ToString("yyyyMMdd") + @".csv";
StreamWriter swCsv = new StreamWriter(strOutPutFile, true, System.Text.Encoding.GetEncoding("GB2312"));
strInfo.Append(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + ",");
strInfo.Append(class_name + ",");
strInfo.Append(method_name + ",");
strInfo.Append(strSql + ",");
strInfo.Append("(" + message_info + ")");
swCsv.WriteLine(strInfo.ToString());
swCsv.Close();
}
}