You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
3.0 KiB
107 lines
3.0 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.IO;
|
|
|
|
namespace GummingCommon
|
|
{
|
|
/// <summary>
|
|
/// this is debug class,not used
|
|
/// </summary>
|
|
public class DebugLog
|
|
{
|
|
|
|
private static StringBuilder logContent = new StringBuilder();
|
|
/// <summary>
|
|
/// 防止并发对象
|
|
/// </summary>
|
|
private static Object lockobj = new Object();
|
|
|
|
private static DateTime lastTime = DateTime.Now;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="exception"></param>
|
|
/// <returns></returns>
|
|
public static bool WriteLog(string log)
|
|
{
|
|
bool returnValue;
|
|
double ticket = DateTime.Now.Subtract(lastTime).TotalMilliseconds;
|
|
logContent.AppendLine(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss.fff") + " - " + ticket + " " + log);
|
|
lastTime = DateTime.Now;
|
|
returnValue = WriteLog();
|
|
return returnValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 产生文件名
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private static string GenerateFileName()
|
|
{
|
|
string path = AppDomain.CurrentDomain.BaseDirectory + @"log\debug\";
|
|
if (!Directory.Exists(path))
|
|
{
|
|
Directory.CreateDirectory(path);
|
|
}
|
|
return path + "Debug" + DateTime.Now.ToString("yyyy-MM-dd hh-mm-ss") + " " + System.Guid.NewGuid().ToString() + ".txt";
|
|
}
|
|
|
|
public static int maxFlushSize = 1000000;
|
|
/// <summary>
|
|
/// 记录监控日志
|
|
/// </summary>
|
|
/// <param name="logContext">日志内容</param>
|
|
/// <returns></returns>
|
|
public static bool WriteLog()
|
|
{
|
|
bool returnValue = true;
|
|
|
|
lock (logContent) //防止并发
|
|
{
|
|
if (logContent.Length >= maxFlushSize)
|
|
{
|
|
GC.Collect();//强制垃圾回收
|
|
|
|
int writtenSize = FlushLog(); //将监控日志写入文件
|
|
if (writtenSize > 0)
|
|
{
|
|
logContent.Clear();
|
|
}
|
|
else if (writtenSize == -1) //写入日志失败
|
|
{
|
|
returnValue = false;
|
|
}
|
|
}
|
|
}
|
|
return returnValue;
|
|
}
|
|
|
|
|
|
public static int FlushLog()
|
|
{
|
|
int returnValue = -1;
|
|
|
|
try
|
|
{
|
|
if (logContent.Length > 0)
|
|
{
|
|
StreamWriter sw = File.CreateText(GenerateFileName());
|
|
returnValue = logContent.Length;
|
|
sw.Write(logContent.ToString(0, returnValue));
|
|
sw.Close();
|
|
logContent.Clear();
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
returnValue = -1;
|
|
}
|
|
|
|
return returnValue;
|
|
}
|
|
}
|
|
}
|