213 lines
6.0 KiB
C#
213 lines
6.0 KiB
C#
/*
|
|
* 日志记录
|
|
* 肖俊
|
|
* 2015-3-13
|
|
*/
|
|
|
|
using NaiBa.Util;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Threading;
|
|
using System.Windows.Forms;
|
|
|
|
namespace AllBase
|
|
{
|
|
public class LogSave
|
|
{
|
|
/// <summary>
|
|
/// 显示当前流程信息 key 1:拼团 2:拍单 3:售后
|
|
/// </summary>
|
|
public static Dictionary<int, List<string>> ShowMsg = new Dictionary<int, List<string>>();
|
|
|
|
/// <summary>
|
|
/// 消息日志
|
|
/// </summary>
|
|
public static Dictionary<string, List<string>> Messages = new Dictionary<string, List<string>>();
|
|
|
|
/// <summary>
|
|
/// 日志队列
|
|
/// </summary>
|
|
private static ConcurrentList<string> Logs = new ConcurrentList<string>();
|
|
|
|
/// <summary>
|
|
/// 准备写入文件
|
|
/// </summary>
|
|
public static List<string> ImageTemp = new List<string>();
|
|
|
|
/// <summary>
|
|
/// 初始化
|
|
/// </summary>
|
|
public LogSave()
|
|
{
|
|
FileBase.DirectoryCreate(Application.StartupPath + "\\log\\");
|
|
FileBase.DirectoryCreate(Application.StartupPath + "\\temp\\");
|
|
FileBase.DirectoryCreate(Application.StartupPath + "\\temp\\province\\");
|
|
|
|
string[] l = Directory.GetFiles(Application.StartupPath + "\\log\\");
|
|
for (int i = 0; i < l.Length; i++)
|
|
{
|
|
string name = Path.GetFileName(l[i]);
|
|
string date = BaseRegex.GetMatchRegexString(ref name, @"([\d]{4}-[\d]{1,2}-[\d]{1,2})");
|
|
DateTime d;
|
|
if (DateTime.TryParse(date, out d))
|
|
{
|
|
if (d < DateTime.Now.AddDays(-7))
|
|
{
|
|
FileBase.Delete(l[i]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 服务
|
|
/// </summary>
|
|
public void Get()
|
|
{
|
|
string temp;
|
|
while (true)
|
|
{
|
|
if (Logs.Count == 0)
|
|
{
|
|
Thread.Sleep(10000);
|
|
continue;
|
|
}
|
|
temp = "";
|
|
while (Logs.Count > 0)
|
|
{
|
|
try
|
|
{
|
|
if (string.IsNullOrEmpty(Logs[0]))
|
|
continue;
|
|
temp += Logs[0] + "\r\n";
|
|
Logs.RemoveAt(0);
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
if (string.IsNullOrEmpty(temp))
|
|
continue;
|
|
string patch = "log\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";
|
|
try
|
|
{
|
|
File.AppendAllText(patch, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n" + temp);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
if (e.Message.Contains("空间"))
|
|
return;
|
|
Thread.Sleep(500);//重试一次
|
|
try
|
|
{
|
|
File.AppendAllText(patch, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n" + temp);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
BaseClass.ViewCode += "写入IIS日志:" + patch + " 发生错误:" + e.Message + "\r\n";
|
|
}
|
|
}
|
|
Thread.Sleep(120 * 1000);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 添加日志
|
|
/// </summary>
|
|
/// <param name="message">日志</param>
|
|
public static void Add(string message)
|
|
{
|
|
BaseClass.ViewCode += message + "\r\n";
|
|
if (Logs.Count > 5000)
|
|
{
|
|
BaseClass.ViewCode += "日志队列超长,请检查日志处理\r\n";
|
|
Logs.Clear();
|
|
return;
|
|
}
|
|
Logs.Add(message);
|
|
}
|
|
/// <summary>
|
|
/// 添加日志
|
|
/// </summary>
|
|
/// <param name="Name">名称</param>
|
|
/// <param name="message">日志</param>
|
|
public static void Add(string Name, string message)
|
|
{
|
|
if (string.IsNullOrEmpty(Name))
|
|
{
|
|
Logs.Add(message);
|
|
return;
|
|
}
|
|
if (!Messages.ContainsKey(Name))
|
|
{
|
|
try
|
|
{
|
|
Messages.Add(Name, new List<string>());
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
}
|
|
try
|
|
{
|
|
Logs.Add(message);
|
|
Messages[Name].Add(message);
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
|
|
}
|
|
/// <summary>
|
|
/// 添加到显示
|
|
/// </summary>
|
|
/// <param name="message">日志</param>
|
|
public static void AddView(string message)
|
|
{
|
|
BaseClass.ViewCode += message + "\r\n";
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 设置为最大值,完成
|
|
/// </summary>
|
|
/// <param name="name"></param>
|
|
public static void SetProccessFinish(string name)
|
|
{
|
|
BaseClass.ProcessList[name].Value = BaseClass.ProcessList[name].Max;
|
|
}
|
|
|
|
public static void Save(string content, LogType logType)
|
|
{
|
|
try
|
|
{
|
|
|
|
if (logType == LogType.Operation)
|
|
{
|
|
string fileName = DateTime.Now.ToString("yyyy-MM-dd");
|
|
FileBase.WriteTxt("\\temp\\OperationLog", fileName, content, false);
|
|
}
|
|
if (logType == LogType.Error)
|
|
{
|
|
string fileName = DateTime.Now.ToString("yyyy-MM-dd");
|
|
FileBase.WriteTxt("\\temp\\ErrorLog", fileName, content, false);
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
//throw;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public enum LogType
|
|
{
|
|
Operation,
|
|
Error
|
|
}
|
|
}
|