87 lines
2.8 KiB
C#
87 lines
2.8 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.IO;
|
|||
|
|
using System.Reflection;
|
|||
|
|
|
|||
|
|
namespace WMS.Common
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 写日志
|
|||
|
|
/// </summary>
|
|||
|
|
public class LogWriteText
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 清除 日志数据
|
|||
|
|
/// </summary>
|
|||
|
|
public static void ClearDataLog()
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
string appPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
|
|||
|
|
string path = appPath + "\\log";
|
|||
|
|
double logDate = 13;
|
|||
|
|
string filedate = System.DateTime.Now.AddDays(-logDate).ToString("yyyyMMdd");
|
|||
|
|
string[] filename = Directory.GetFiles(path);
|
|||
|
|
foreach (string fn in filename)
|
|||
|
|
{
|
|||
|
|
if (fn.Trim().Length > 8)
|
|||
|
|
{
|
|||
|
|
string date = fn.Substring(fn.IndexOf("2"), 8);
|
|||
|
|
if (int.Parse(date) < int.Parse(filedate))
|
|||
|
|
{
|
|||
|
|
File.Delete(fn);
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
private static object write = new object();
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 写日志
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="strLog"></param>
|
|||
|
|
public static void WriteLog(string strLog)
|
|||
|
|
{
|
|||
|
|
lock (write)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
strLog = "时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Environment.NewLine + strLog + Environment.NewLine;
|
|||
|
|
DirectoryInfo di = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + "\\Log");
|
|||
|
|
if (di.Exists == false)
|
|||
|
|
{
|
|||
|
|
di.Create();
|
|||
|
|
}
|
|||
|
|
string logFileName;
|
|||
|
|
logFileName = AppDomain.CurrentDomain.BaseDirectory + "\\Log\\" + DateTime.Now.ToString("yyyyMMdd") + ".log";
|
|||
|
|
if (File.Exists(logFileName))
|
|||
|
|
{
|
|||
|
|
FileInfo fi = new FileInfo(logFileName);
|
|||
|
|
if (fi.Length > 5024000)
|
|||
|
|
{
|
|||
|
|
fi.Delete();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
StreamWriter sw = null;
|
|||
|
|
FileStream fs = new FileStream(logFileName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
|
|||
|
|
sw = new StreamWriter(fs);
|
|||
|
|
sw.WriteLine(strLog);
|
|||
|
|
sw.Close();
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
SystemCommon.ShowErrorMessageBox(string.Empty + ex.Message.ToString());
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|