Files
2026-07-10 15:25:05 +08:00

1145 lines
44 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
namespace LSUtils
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net.Sockets;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Xml;
public static class LSWriteLog
{
private static string[] AssemblyNames = new string[50];
private static int AssemblyNamesCount = 0;
private static bool bCreateNew = false;
private static bool bFileLog = false;
private static bool bShareCounter = false;
private static bool bStopThread = false;
private static bool bUseBackgroudThread = true;
private static List<CommonFileStreamClass> FixedUsedFileNames = new List<CommonFileStreamClass>();
private static SortedDictionary<string, string> ForbidFilesList = new SortedDictionary<string, string>();
private static Queue<GlLogClass> GlLogClassQueue = new Queue<GlLogClass>();
private static long MaxFileSize = 0L;
private static AutoResetEvent OpenFile_Event = new AutoResetEvent(true);
private static AutoResetEvent Rev_Event = new AutoResetEvent(true);
private static int SingleRecordMaxLength = 0;
private static string sLogsPath = "";
private static List<Thread> ThreadList = new List<Thread>();
private static long UniformCounter = 0L;
private static SortedList<string, CommonFileStreamClass> UsedFileNames = new SortedList<string, CommonFileStreamClass>();
public static void AnalyseParams(ref string WriteMsg, params object[] args)
{
try
{
int num;
MatchCollection matchs = new Regex(@"(?<={)\w*?(?=})").Matches(WriteMsg);
if (matchs.Count != args.Length)
{
string str2 = args.GetType().ToString();
if ((str2 != null) && (str2 == "System.String[]"))
{
string str = "\r\n ";
for (num = 0; num < args.Length; num++)
{
str = str + args[num] + "\r\n ";
}
args = new object[] { str };
return;
}
int num2 = matchs.Count - args.Length;
if (num2 > 0)
{
object[] array = new object[matchs.Count];
args.CopyTo(array, 0);
for (num = 0; num < num2; num++)
{
array[args.Length + num] = " [No Value] ";
}
args = array;
}
else
{
for (num = num2; num < 0; num++)
{
int num3 = args.Length + num;
object obj2 = WriteMsg;
WriteMsg = string.Concat(new object[] { obj2, " [UnKnownParam]={", num3, "} " });
}
}
}
for (num = 0; num < args.Length; num++)
{
arrayParamConvert(4, 0, ref args[num]);
}
}
catch (Exception exception)
{
Trace.WriteLine("AnalyseParams分析出错,ex is " + exception);
}
}
private static void arrayParamConvert(int LeftSpaceNum, int MaxFileNamelen, ref object paramobj)
{
string str = "";
string str2 = "";
int totalWidth = LeftSpaceNum;
LeftSpaceNum += MaxFileNamelen;
str = str.PadLeft(LeftSpaceNum);
str2 = str2.PadLeft(totalWidth);
Type type = paramobj.GetType();
if (paramobj == null)
{
paramobj = str + "= null\r\n";
}
else
{
int num4;
object obj3;
switch (type.ToString())
{
case "System.Byte[]":
{
string str3 = "\r\n" + str;
int num2 = 0;
foreach (byte num3 in (byte[]) paramobj)
{
str3 = str3 + num3.ToString("X2") + " ";
num2++;
if ((num2 % 8) == 0)
{
str3 = str3 + "\r\n" + str;
}
}
str3 = str3 + "\r\n";
paramobj = str3;
break;
}
case "System.Char[]":
paramobj = str + new string((char[]) paramobj).Replace("\0", "");
break;
case "System.String[]":
{
string[] strArray = (string[]) paramobj;
paramobj = "\r\n";
num4 = 0;
while (num4 < strArray.Length)
{
obj3 = paramobj;
paramobj = string.Concat(new object[] { obj3, str, strArray[num4], "\r\n" });
num4++;
}
break;
}
case "System.Int32[]":
{
int[] numArray = (int[]) paramobj;
paramobj = "\r\n";
num4 = 0;
while (num4 < numArray.Length)
{
obj3 = paramobj;
paramobj = string.Concat(new object[] { obj3, str, numArray[num4].ToString(), "\r\n" });
num4++;
}
break;
}
case "System.Int64[]":
{
long[] numArray2 = (long[]) paramobj;
paramobj = "\r\n";
num4 = 0;
while (num4 < numArray2.Length)
{
obj3 = paramobj;
paramobj = string.Concat(new object[] { obj3, str, numArray2[num4].ToString(), "\r\n" });
num4++;
}
break;
}
case "System.Single[]":
{
float[] numArray3 = (float[]) paramobj;
paramobj = "\r\n";
num4 = 0;
while (num4 < numArray3.Length)
{
obj3 = paramobj;
paramobj = string.Concat(new object[] { obj3, str, numArray3[num4].ToString(), "\r\n" });
num4++;
}
break;
}
case "System.Double[]":
{
double[] numArray4 = (double[]) paramobj;
paramobj = "\r\n";
num4 = 0;
while (num4 < numArray4.Length)
{
obj3 = paramobj;
paramobj = string.Concat(new object[] { obj3, str, numArray4[num4].ToString(), "\r\n" });
num4++;
}
break;
}
case "System.Xml.XmlDocument":
paramobj = FormatXmlDoc((XmlDocument) paramobj);
break;
default:
{
string str4 = "";
if (type.IsArray)
{
Array array = (Array) paramobj;
for (num4 = 0; num4 < array.Length; num4++)
{
str4 = str4 + "[" + Convert.ToString(num4) + "]";
}
str4 = str4 + "\r\n ";
for (num4 = 0; num4 < array.Length; num4++)
{
object obj2 = array.GetValue(num4);
if (obj2 == null)
{
str4 = str4 + str2 + "= null\r\n";
}
else
{
string str5 = obj2.GetType().Name + "[" + Convert.ToString(num4) + "]\r\n";
str4 = str4 + str2 + str5;
if (IsStruct(obj2))
{
str4 = str4 + str2 + ParseStructObj(totalWidth + 4, obj2) + "\r\n ";
}
else
{
str4 = str4 + str + obj2.ToString() + "\r\n ";
}
}
}
}
else if (IsStruct(paramobj))
{
string str7 = str4;
str4 = (str7 + "\r\n" + str2 + type.Name + "\r\n") + str2 + ParseStructObj(totalWidth - 4, paramobj);
}
else
{
str4 = str4 + paramobj.ToString();
}
paramobj = str4;
break;
}
}
if ((SingleRecordMaxLength > 0) && (((string) paramobj).Length > SingleRecordMaxLength))
{
paramobj = ((string) paramobj).Substring(0, SingleRecordMaxLength) + "......";
}
}
}
private static void CheckFileSize(CommonFileStreamClass CommonFileStream)
{
if (((CommonFileStream != null) && (CommonFileStream.fsFile != null)) && (CommonFileStream.fsFile.Length > MaxFileSize))
{
ReOpenFile(CommonFileStream, true);
}
}
public static void ClearLogFiles(string sDirectory)
{
if (Directory.Exists(sDirectory))
{
string[] files = Directory.GetFiles(sDirectory, "*.log");
foreach (string str in files)
{
try
{
File.Delete(str);
}
catch (Exception)
{
}
}
}
}
public static void CloseAllFile()
{
int num;
CommonFileStreamClass class2;
bStopThread = true;
for (num = 0; num < 10; num++)
{
Rev_Event.Set();
}
for (num = 0; num < UsedFileNames.Count; num++)
{
class2 = UsedFileNames.Values[num];
if ((class2 != null) && (class2.swFileName != null))
{
class2.swFileName.Close();
class2.swFileName = null;
}
}
for (num = 0; num < FixedUsedFileNames.Count; num++)
{
class2 = FixedUsedFileNames[num];
if ((class2 != null) && (class2.swFileName != null))
{
class2.swFileName.Close();
class2.swFileName = null;
}
}
for (num = 0; num < ThreadList.Count; num++)
{
ThreadList[num].Interrupt();
}
Thread.Sleep(100);
}
private static void CreateListener(CommonFileStreamClass CommonFileStream)
{
if ((CommonFileStream != null) && (CommonFileStream.swFileName != null))
{
try
{
TextWriterTraceListener listener = new TextWriterTraceListener(CommonFileStream.swFileName) {
TraceOutputOptions = TraceOptions.DateTime,
TraceOutputOptions = TraceOptions.Callstack
};
Debug.Listeners.Add(listener);
Trace.IndentLevel = 0;
Trace.AutoFlush = true;
Debug.IndentLevel = 0;
Debug.AutoFlush = true;
}
catch (Exception)
{
}
}
}
private static string FormatException(Exception ex)
{
object obj2 = ("\r\n" + "Exception.Message = " + ex.Message + "\r\n") + "Exception.Source = " + ex.Source + "\r\n";
return (string.Concat(new object[] { obj2, "Exception.TargetSite = ", ex.TargetSite, "\r\n" }) + "Exception.StackTrace = " + ex.StackTrace + "\r\n");
}
private static string FormatException(SocketException ex)
{
object obj2 = FormatException((Exception) ex);
obj2 = string.Concat(new object[] { obj2, "Exception.ErrorCode = ", ex.ErrorCode, "\r\n" });
return string.Concat(new object[] { obj2, "Exception.SocketErrorCode = ", ex.SocketErrorCode, "\r\n" });
}
private static string FormatXmlDoc(XmlDocument xmlDoc)
{
StringBuilder sb = new StringBuilder();
XmlTextWriter w = null;
try
{
w = new XmlTextWriter(new StringWriter(sb)) {
Formatting = Formatting.Indented
};
xmlDoc.WriteTo(w);
}
finally
{
if (w != null)
{
w.Close();
}
}
return sb.ToString();
}
private static string FormatXmlString(string source)
{
StringBuilder sb = new StringBuilder();
XmlTextWriter w = null;
try
{
XmlDocument document = new XmlDocument();
document.LoadXml(source);
w = new XmlTextWriter(new StringWriter(sb)) {
Formatting = Formatting.Indented
};
document.WriteTo(w);
document = null;
}
catch (Exception)
{
return source;
}
finally
{
if (w != null)
{
w.Close();
}
}
return sb.ToString();
}
private static XmlNode GetElementNode(XmlNode xn)
{
while ((xn != null) && (xn.NodeType != XmlNodeType.Element))
{
xn = xn.NextSibling;
}
return xn;
}
private static GlLogClass GetGlLogClass()
{
GlLogClass glLogClass = null;
if (GlLogClassQueue.Count > 0)
{
lock (GlLogClassQueue)
{
glLogClass = GlLogClassQueue.Dequeue();
}
}
else
{
Rev_Event.WaitOne();
}
if (bStopThread)
{
return null;
}
if (glLogClass == null)
{
glLogClass = GetGlLogClass();
}
return glLogClass;
}
private static CommonFileStreamClass GetStreamWrite(string FileName, bool bAddCount)
{
CommonFileStreamClass class2 = null;
if (!(bFileLog && !(FileName == "")))
{
return class2;
}
if (ForbidFilesList.ContainsKey(FileName))
{
return class2;
}
int num = 0;
do
{
if (UsedFileNames.TryGetValue(FileName, out class2))
{
goto Label_0082;
}
class2 = OpenLogFile(FileName);
num++;
if (num > 1)
{
return null;
}
}
while (class2 == null);
UsedFileNames.Add(FileName, class2);
Label_0082:
if ((class2 != null) && bAddCount)
{
Interlocked.Increment(ref class2.Cur_Counter);
}
return class2;
}
private static string GetThreadName()
{
int managedThreadId = Thread.CurrentThread.ManagedThreadId;
string name = "MainThread";
if (Thread.CurrentThread.Name != null)
{
name = Thread.CurrentThread.Name;
}
return ("-------Thread【 " + managedThreadId.ToString() + " 】【" + name + "】");
}
public static string getTick(string FileName, long Cur_Counter)
{
if (bShareCounter)
{
Interlocked.Increment(ref UniformCounter);
return (DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "-------【 " + UniformCounter.ToString() + " 】" + GetThreadName());
}
return (DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "-------【 " + Cur_Counter.ToString() + " 】" + GetThreadName());
}
private static string GetXmlNodeValue(ref XmlNode xn)
{
while ((xn != null) && (xn.NodeType != XmlNodeType.Element))
{
xn = xn.NextSibling;
}
string innerText = "";
if (xn != null)
{
innerText = xn.InnerText;
xn = xn.NextSibling;
}
return innerText;
}
public static void Init(string LogsPath, string TraceName)
{
string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
if (!baseDirectory.EndsWith(@"\"))
{
baseDirectory = baseDirectory + @"\";
}
XmlDocument document = new XmlDocument();
try
{
sLogsPath = LogsPath.Trim();
document.Load(baseDirectory + "LSWriteLogConfig.xml");
XmlNode elementNode = GetElementNode(document.DocumentElement.FirstChild);
XmlNode firstChild = elementNode.FirstChild;
if (sLogsPath == "")
{
sLogsPath = GetXmlNodeValue(ref firstChild);
}
else
{
GetXmlNodeValue(ref firstChild);
}
char[] chArray = sLogsPath.ToCharArray();
if (((((chArray[0] < 'a') || (chArray[0] > 'z')) && ((chArray[0] < 'A') || (chArray[0] > 'Z'))) || (chArray[1] != ':')) || (chArray[2] != '\\'))
{
sLogsPath = baseDirectory + sLogsPath;
}
if (!Directory.Exists(sLogsPath))
{
Directory.CreateDirectory(sLogsPath);
}
sLogsPath = sLogsPath + @"\";
bool flag = bool.Parse(GetXmlNodeValue(ref firstChild));
bFileLog = bool.Parse(GetXmlNodeValue(ref firstChild));
bShareCounter = bool.Parse(GetXmlNodeValue(ref firstChild));
bCreateNew = bool.Parse(GetXmlNodeValue(ref firstChild));
MaxFileSize = long.Parse(GetXmlNodeValue(ref firstChild));
bUseBackgroudThread = bool.Parse(GetXmlNodeValue(ref firstChild));
SingleRecordMaxLength = int.Parse(GetXmlNodeValue(ref firstChild));
elementNode = GetElementNode(elementNode.NextSibling);
if (elementNode.HasChildNodes)
{
for (firstChild = elementNode.FirstChild; firstChild != null; firstChild = firstChild.NextSibling)
{
AssemblyNames[AssemblyNamesCount] = baseDirectory + firstChild.InnerText;
if (File.Exists(AssemblyNames[AssemblyNamesCount]))
{
AssemblyNamesCount++;
}
}
}
elementNode = GetElementNode(elementNode.NextSibling);
if (elementNode.HasChildNodes)
{
for (firstChild = elementNode.FirstChild; firstChild != null; firstChild = firstChild.NextSibling)
{
string key = firstChild.InnerText.Trim();
if (key != "")
{
ForbidFilesList.Add(key, key);
}
}
}
Random random = new Random();
string fileName = "lsCounterSavedFiles";
CommonFileStreamClass item = OpenLogFile(fileName);
FixedUsedFileNames.Add(item);
TraceName = TraceName.Trim();
if (flag && (TraceName != ""))
{
item = OpenLogFile(TraceName);
FixedUsedFileNames.Add(item);
CreateListener(item);
}
else
{
FixedUsedFileNames.Add(null);
}
if (bUseBackgroudThread)
{
new Thread(new ThreadStart(LSWriteLog.ProcessGlLogClassQueueThread)) { Priority = ThreadPriority.BelowNormal }.Start();
}
}
finally
{
document = null;
}
}
private static void InsertGlLogClass(int WriteType, CommonFileStreamClass CommonFileStream, bool bWriteStack, string LogMsg, params object[] args)
{
try
{
if ((CommonFileStream == null) || (CommonFileStream.swFileName == null))
{
return;
}
CommonFileStream.Cur_Counter += 1L;
string str = getTick(CommonFileStream.filename, CommonFileStream.Cur_Counter);
GlLogClass item = new GlLogClass {
WriteType = WriteType,
CommonFileStream = CommonFileStream,
bWriteStack = bWriteStack,
TickInfo = str,
LogMsg = LogMsg,
args = args
};
if (bWriteStack)
{
item.CommonFileStream.stacktrace = new StackTrace(1, true);
}
if (bUseBackgroudThread)
{
lock (GlLogClassQueue)
{
GlLogClassQueue.Enqueue(item);
}
}
else
{
ProcessGlLogClassQueue(item);
}
}
catch (Exception exception)
{
Trace.WriteLine("记录日志功能中插入队列出错,LSWriteLog.InsertGlLogClass,错误是:" + exception);
}
Rev_Event.Set();
}
private static bool IsStruct(object obj)
{
return ((((obj != null) && obj.GetType().IsValueType) && !obj.GetType().IsEnum) && !obj.GetType().IsPrimitive);
}
private static CommonFileStreamClass OpenLogFile(string FileName)
{
OpenFile_Event.WaitOne();
try
{
CommonFileStreamClass class2 = null;
UsedFileNames.TryGetValue(FileName, out class2);
if (class2 != null)
{
return null;
}
string path = sLogsPath + FileName;
FileStream stream = null;
if (bCreateNew)
{
stream = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.Read);
}
else
{
stream = File.Open(path, FileMode.Append, FileAccess.Write, FileShare.Read);
}
StreamWriter writer = new StreamWriter(stream, Encoding.Default) {
AutoFlush = true
};
return new CommonFileStreamClass { fsFile = stream, swFileName = writer, filename = FileName };
}
catch (Exception exception)
{
Trace.WriteLine("记录日志功能中创建文件出错,LSWriteLog.OpenLogFile,错误是:" + exception.Message.ToString());
}
finally
{
OpenFile_Event.Set();
}
return null;
}
private static string ParseStructObj(int LeftSpaceNum, object ObjStruct)
{
Type type = ObjStruct.GetType();
string str = "";
int totalWidth = 0;
FieldInfo[] fields = type.GetFields();
foreach (FieldInfo info in fields)
{
if (info.Name.Length > totalWidth)
{
totalWidth = info.Name.Length;
}
}
foreach (FieldInfo info in fields)
{
object objStruct = info.GetValue(ObjStruct);
type = objStruct.GetType();
string str2 = info.Name.PadRight(totalWidth).PadLeft(totalWidth + LeftSpaceNum);
if (!((!type.IsValueType || type.IsEnum) || type.IsPrimitive))
{
str = (str + str2 + "\r\n ") + ParseStructObj(LeftSpaceNum + 4, objStruct);
}
else
{
arrayParamConvert(LeftSpaceNum + 4, totalWidth, ref objStruct);
object obj3 = str;
str = string.Concat(new object[] { obj3, str2, " = ", objStruct, "\r\n " });
}
}
return str;
}
private static void PrivateWriteLog(GlLogClass LogClass)
{
Exception exception;
StreamWriter swFileName = LogClass.CommonFileStream.swFileName;
string tickInfo = LogClass.TickInfo;
AutoResetEvent event2 = LogClass.CommonFileStream.Write_Event;
string logMsg = LogClass.LogMsg;
object[] args = LogClass.args;
try
{
if (swFileName == null)
{
return;
}
if ((args != null) && (args.Length > 0))
{
AnalyseParams(ref logMsg, args);
}
}
catch (Exception exception1)
{
exception = exception1;
return;
}
event2.WaitOne();
try
{
swFileName.WriteLine("----------------" + tickInfo + "-------------------------------------");
if ((args != null) && (args.Length > 0))
{
swFileName.WriteLine("/// Log " + logMsg, args);
}
else
{
swFileName.WriteLine(logMsg);
}
if (LogClass.bWriteStack)
{
StackTrace stacktrace = LogClass.CommonFileStream.stacktrace;
int num = 1;
foreach (StackFrame frame in stacktrace.GetFrames())
{
string name = frame.GetMethod().DeclaringType.Name;
if (ValidityClassName(name))
{
swFileName.WriteLine("/// Stack Run Order Desc={0} , {1}.{2} , FileLineNumber={3} , FileColumnNumber={4}", new object[] { num, name, frame.GetMethod().Name, frame.GetFileLineNumber(), frame.GetFileColumnNumber() });
}
num++;
}
}
swFileName.WriteLine("");
}
catch (Exception exception2)
{
exception = exception2;
Trace.Indent();
Trace.WriteLine("-----------------writelog fun can't write log------------------------------------------------------");
Trace.WriteLine(" error is : " + exception);
Trace.WriteLine(" ErrMsg is " + logMsg);
Trace.WriteLine("/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////");
Trace.Unindent();
}
CheckFileSize(LogClass.CommonFileStream);
event2.Set();
LogClass.CommonFileStream = null;
LogClass = null;
}
private static void PrivateWriteLog(CommonFileStreamClass CommonFileStream, string LogMsg)
{
if (CommonFileStream != null)
{
StreamWriter swFileName = CommonFileStream.swFileName;
AutoResetEvent event2 = CommonFileStream.Write_Event;
if (swFileName != null)
{
event2.WaitOne();
try
{
swFileName.WriteLine(LogMsg);
}
catch (Exception exception)
{
Trace.Indent();
Trace.WriteLine("-----------------writelog fun can't write log------------------------------------------------------");
Trace.WriteLine(" Exception is " + exception);
Trace.WriteLine(" ErrMsg is " + LogMsg);
Trace.WriteLine("/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////");
Trace.Unindent();
}
CheckFileSize(CommonFileStream);
event2.Set();
CommonFileStream = null;
}
}
}
private static void ProcessGlLogClassQueue(GlLogClass LogClass)
{
int index = 0;
try
{
if ((LogClass.WriteType == 10) && bFileLog)
{
ReOpenFile(LogClass.CommonFileStream, false);
}
else
{
string logMsg = LogClass.LogMsg;
object[] args = LogClass.args;
switch (LogClass.WriteType)
{
case 1:
AnalyseParams(ref logMsg, args);
WriteTrace(string.Format(logMsg, args), LogClass.TickInfo);
return;
case 2:
AnalyseParams(ref logMsg, args);
index = args.Length - 1;
if (args[index].ToString().Trim() != "")
{
args[index] = FormatXmlString((string) args[index]);
}
WriteTrace(string.Format(logMsg, args), LogClass.TickInfo);
return;
case 3:
PrivateWriteLog(LogClass);
return;
case 4:
PrivateWriteLog(LogClass.CommonFileStream, LogClass.LogMsg);
return;
case 5:
AnalyseParams(ref logMsg, args);
index = args.Length - 1;
if (args[index].ToString().Trim() != "")
{
args[index] = FormatXmlString((string) args[index]);
}
LogClass.LogMsg = string.Format(logMsg, args);
LogClass.args = null;
PrivateWriteLog(LogClass);
return;
}
}
}
catch (Exception exception)
{
Trace.WriteLine("记录日志功能中线程处理出错,LSWriteLog.ProcessGlLogClassQueue,错误是:" + exception);
}
finally
{
if (LogClass != null)
{
LogClass = null;
}
}
}
private static void ProcessGlLogClassQueueThread()
{
Thread.CurrentThread.IsBackground = true;
GlLogClass logClass = null;
int index = 0;
while (!bStopThread)
{
try
{
logClass = GetGlLogClass();
if (bStopThread)
{
break;
}
if (logClass.CommonFileStream == null)
{
continue;
}
if ((logClass.WriteType == 10) && bFileLog)
{
ReOpenFile(logClass.CommonFileStream, false);
continue;
}
if (logClass.CommonFileStream.swFileName == null)
{
continue;
}
string logMsg = logClass.LogMsg;
object[] args = logClass.args;
switch (logClass.WriteType)
{
case 1:
{
AnalyseParams(ref logMsg, args);
WriteTrace(string.Format(logMsg, args), logClass.TickInfo);
continue;
}
case 2:
{
AnalyseParams(ref logMsg, args);
index = args.Length - 1;
if (args[index].ToString().Trim() != "")
{
args[index] = FormatXmlString((string) args[index]);
}
WriteTrace(string.Format(logMsg, args), logClass.TickInfo);
continue;
}
case 3:
{
PrivateWriteLog(logClass);
continue;
}
case 4:
{
PrivateWriteLog(logClass.CommonFileStream, logClass.LogMsg);
continue;
}
case 5:
{
AnalyseParams(ref logMsg, args);
index = args.Length - 1;
if (args[index].ToString().Trim() != "")
{
args[index] = FormatXmlString((string) args[index]);
}
logClass.LogMsg = string.Format(logMsg, args);
logClass.args = null;
PrivateWriteLog(logClass);
continue;
}
}
}
catch (ThreadAbortException exception)
{
Trace.WriteLine("记录日志功能中线程处理出错,LSWriteLog.ProcessGlLogClassQueueThread ThreadAbortException: " + exception);
break;
}
catch (ThreadInterruptedException exception2)
{
Trace.WriteLine("记录日志功能中线程处理出错,LSWriteLog.ProcessGlLogClassQueueThread ThreadInterruptedException: " + exception2);
break;
}
catch (Exception exception3)
{
Trace.WriteLine("记录日志功能中线程处理出错,LSWriteLog.ProcessGlLogClassQueueThread,错误是:" + exception3);
}
finally
{
if (logClass != null)
{
logClass = null;
}
}
}
}
public static void ReCreateFile(string FileName)
{
CommonFileStreamClass class2 = new CommonFileStreamClass {
filename = FileName
};
GlLogClass item = new GlLogClass {
WriteType = 10,
CommonFileStream = class2
};
lock (GlLogClassQueue)
{
GlLogClassQueue.Enqueue(item);
}
}
private static void ReOpenFile(CommonFileStreamClass CommonFileStream, bool bSaveOldFile)
{
if ((CommonFileStream != null) && UsedFileNames.TryGetValue(CommonFileStream.filename, out CommonFileStream))
{
try
{
if (CommonFileStream.swFileName != null)
{
CommonFileStream.swFileName.Close();
}
if (CommonFileStream.fsFile != null)
{
CommonFileStream.fsFile.Close();
}
string filename = CommonFileStream.filename;
string sourceFileName = sLogsPath + CommonFileStream.filename;
if (bSaveOldFile)
{
string destFileName = sLogsPath + filename.Substring(0, filename.Length - 4) + DateTime.Now.ToString("dd#HH-mm-ss") + ".log";
File.Move(sourceFileName, destFileName);
}
if (bCreateNew)
{
CommonFileStream.fsFile = File.Open(sourceFileName, FileMode.Create, FileAccess.Write, FileShare.Read);
}
else
{
CommonFileStream.fsFile = File.Open(sourceFileName, FileMode.Append, FileAccess.Write, FileShare.Read);
}
CommonFileStream.swFileName = new StreamWriter(CommonFileStream.fsFile, Encoding.Default);
CommonFileStream.swFileName.AutoFlush = true;
}
catch (Exception exception)
{
Trace.WriteLine("记录日志功能中重新创建文件出错,LSWriteLog.ReOpenFile,错误是:" + exception.Message.ToString());
}
}
}
private static bool ValidityClassName(string ClassName)
{
if (AssemblyNamesCount == 0)
{
return true;
}
for (int i = 0; i < AssemblyNamesCount; i++)
{
string assemblyFile = AssemblyNames[i];
Assembly assembly = Assembly.LoadFrom(assemblyFile);
if (assembly != null)
{
Type[] types = assembly.GetTypes();
foreach (Type type in types)
{
if (type.Name == ClassName)
{
return true;
}
}
}
}
return false;
}
private static void WriteCounter(string filename)
{
CommonFileStreamClass class2 = FixedUsedFileNames[0];
if ((class2 != null) && (class2.swFileName != null))
{
lock (class2.swFileName)
{
class2.swFileName.WriteLine(" " + UniformCounter.ToString() + " " + filename);
}
}
}
public static void WriteLog(string FileName, string LogMsg, params object[] args)
{
CommonFileStreamClass streamWrite = GetStreamWrite(FileName, true);
InsertGlLogClass(3, streamWrite, true, LogMsg, args);
}
public static void WriteLog(string FileName, bool bWriteStack, string LogMsg, params object[] args)
{
CommonFileStreamClass streamWrite = GetStreamWrite(FileName, true);
InsertGlLogClass(3, streamWrite, bWriteStack, LogMsg, args);
}
public static void WriteLogXml(string FileName, bool bWriteStack, string LogMsg, params object[] args)
{
CommonFileStreamClass streamWrite = GetStreamWrite(FileName, true);
InsertGlLogClass(5, streamWrite, bWriteStack, LogMsg, args);
}
public static void WriteString(string FileName, string LogMsg)
{
LogMsg = DateTime.Now.ToString("yy-MM-dd HH:mm:ss.fff") + " " + LogMsg;
CommonFileStreamClass streamWrite = GetStreamWrite(FileName, true);
InsertGlLogClass(4, streamWrite, false, LogMsg, null);
}
public static void WriteString(string FileName, string LogMsg, params object[] args)
{
LogMsg = "【 " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " 】" + string.Format(LogMsg, args);
CommonFileStreamClass streamWrite = GetStreamWrite(FileName, true);
InsertGlLogClass(4, streamWrite, true, LogMsg, null);
}
private static void WriteTrace(object value)
{
WriteTrace(value.ToString());
}
private static void WriteTrace(string message)
{
WriteTrace(message, "");
}
private static void WriteTrace(object value, string category)
{
WriteTrace(value.ToString(), category);
}
public static void WriteTrace(string message, string TickInfo)
{
try
{
Trace.WriteLine("----------------" + TickInfo + "-------------------------------------");
Trace.Indent();
Trace.WriteLine(message);
Trace.WriteLine("");
Trace.Unindent();
}
catch (Exception)
{
}
}
public static void WriteTrace(string LogMsg, params object[] args)
{
InsertGlLogClass(1, FixedUsedFileNames[1], true, LogMsg, args);
}
public static void WriteTraceXml(string LogMsg, params object[] args)
{
InsertGlLogClass(2, FixedUsedFileNames[1], true, LogMsg, args);
}
private class CommonFileStreamClass
{
public long Cur_Counter = 0L;
public string filename = "";
public FileStream fsFile = null;
public StackTrace stacktrace = null;
public StreamWriter swFileName = null;
public AutoResetEvent Write_Event = new AutoResetEvent(true);
}
private class GlLogClass
{
public object[] args;
public bool bWriteStack = false;
public LSWriteLog.CommonFileStreamClass CommonFileStream = null;
public string LogMsg = "";
public string TickInfo = "";
public int WriteType = 0;
}
}
}