using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace HXX.Scanner.Common
{
///
/// 日志类
///
public class LogManager_Lock
{
public static readonly string ServerLogPath = @"C:\HXX\";
public static readonly string ServerLogExtension = "-Log_Operation.txt";
public static readonly string ServerLogDateFormat = "yyyy-MM-dd";
private static DateTime CurrentDate = DateTime.Now.Date;
private static string ServerLogFileName = string.Empty;
private static readonly object lockObj = new object();
public static void WriteLog(string Message)
{
WriteLog(Message, null);
}
public static void WriteLog(Exception ee)
{
WriteLog(ee.Message, ee);
}
public static void WriteLog(string Message, Exception ee)
{
lock (lockObj)
{
try
{
CheckLogFile();
using (StreamWriter sw = new StreamWriter(ServerLogFileName, true))
{
string Content = string.Empty;
Content += "=======================================================================================================" + Environment.NewLine;
Content += "************** Time At:" + DateTime.Now.ToString(@"[yyyy-MM-dd HH:mm:ss]") + Environment.NewLine;
Content += "************** Description:" + Environment.NewLine + Message + Environment.NewLine;
if (ee != null)
{
Content += ee.Message + Environment.NewLine;
Content += "************** Error Stack Trace:" + Environment.NewLine + ee.StackTrace + Environment.NewLine;
Content += "************** Error Source:" + ee.Source + Environment.NewLine;
}
Content += "=======================================================================================================" + Environment.NewLine;
Content += Environment.NewLine;
Content += Environment.NewLine;
sw.Write(Content);
}
}
catch { }
}
}
private static void CheckLogFile()
{
if (CurrentDate != DateTime.Now.Date || ServerLogFileName == string.Empty)
{
CurrentDate = DateTime.Now.Date;
ServerLogFileName = DateTime.Now.ToString(ServerLogDateFormat) + ServerLogExtension;
ServerLogFileName = ServerLogPath + ServerLogFileName;
}
if (!Directory.Exists(ServerLogPath))
{
Directory.CreateDirectory(ServerLogPath);
}
}
public static void WriteLogCoco(string Title, List ExceptionList)
{
try
{
CheckLogFile();
using (StreamWriter sw = new StreamWriter(ServerLogFileName, true))
{
string Content = string.Empty;
Content += "=======================================================================================================" + Environment.NewLine;
Content += "************** Time At:" + DateTime.Now.ToString(@"[yyyy-MM-dd HH:mm:ss]") + Environment.NewLine;
Content += Title + Environment.NewLine;
foreach (Exception ee in ExceptionList)
{
Content += "************** Description:" + Environment.NewLine + ee.Message + Environment.NewLine;
if (ee != null)
{
Content += "************** Error Stack Trace:" + Environment.NewLine + ee.StackTrace + Environment.NewLine;
Content += "************** Error Source:" + ee.Source + Environment.NewLine + Environment.NewLine;
}
}
Content += "=======================================================================================================" + Environment.NewLine;
Content += Environment.NewLine;
Content += Environment.NewLine;
sw.Write(Content);
}
}
catch { }
}
}
}