c#实现记录每个线程的log日志
来源:互联网 发布:天猫超市满99减50知乎 编辑:程序博客网 时间:2024/06/14 22:31
这是从网上找来的源码,学习参考,在这里也首先谢谢提供者。
using System;using System.Reflection;using System.Collections.Generic;using System.Xml;using System.Net;using System.Text;using System.Data;using System.IO;using System.Threading;using System.Web;namespace Sample{ public enum VariableType { Text = -1, FunctionName = 0, Parameter = 1 } public static class LogManagement { public static Dictionary<int, ThreadLog> _threadLogDictionary = new Dictionary<int, ThreadLog>(); private static ThreadLog ChooseThreadLog() { if (_threadLogDictionary.ContainsKey(Thread.CurrentThread.ManagedThreadId)) { return _threadLogDictionary[Thread.CurrentThread.ManagedThreadId] as ThreadLog; } else { AddThreadLog(); return _threadLogDictionary[Thread.CurrentThread.ManagedThreadId] as ThreadLog; } } private static void AddThreadLog() { if (!_threadLogDictionary.ContainsKey(Thread.CurrentThread.ManagedThreadId)) { _threadLogDictionary.Add(Thread.CurrentThread.ManagedThreadId, new ThreadLog()); } } private static void RemoveThreadLog() { if (_threadLogDictionary.ContainsKey(Thread.CurrentThread.ManagedThreadId)) { _threadLogDictionary.Remove(Thread.CurrentThread.ManagedThreadId); } } public static string ParkFuncParm(VariableType VbType, string name, string value) { return "<" + VbType.ToString() + " name = /"" + name + "/">" + value + "</" + VbType.ToString() + ">"; } public static void CheckException(Exception exception) { if (_threadLogDictionary.ContainsKey(Thread.CurrentThread.ManagedThreadId)) { ThreadLog TDLog = _threadLogDictionary[Thread.CurrentThread.ManagedThreadId]; TDLog.CheckException(exception); } else { AddThreadLog(); _threadLogDictionary[Thread.CurrentThread.ManagedThreadId].CheckException(exception); } } public static void MakeLog(string description, string data) { ThreadLog TDLog = ChooseThreadLog(); if (TDLog != null) { TDLog.MakeLog(description, data); } } public static void BeginLog(string functionName, string variableInfo,string note) { ThreadLog TDLog = ChooseThreadLog(); if (TDLog != null) { TDLog.BeginLog(functionName, variableInfo, note); } } public static void EndLog(string Data) { ThreadLog TDLog = ChooseThreadLog(); if (TDLog != null) { TDLog.EndLog(Data); } } public static string GetResult() { string ResultData = "<EmptyData></EmptyData>"; ThreadLog TDLog = ChooseThreadLog(); if (TDLog != null && TDLog.XmlDOM != null && TDLog.XmlDOM.DocumentElement != null) { ResultData = TDLog.XmlDOM.DocumentElement.OuterXml; TDLog.XmlDOM = null; _threadLogDictionary.Remove(Thread.CurrentThread.ManagedThreadId); } return ResultData; } } public class ThreadLog { private UInt32 _logId = 1; private Boolean _hasInitialized = false; private Stack<LogInformation> _logInfoStack = null; private XmlDocument _logInfoDataDOM = null; public XmlDocument XmlDOM { set { _logInfoDataDOM = value; } get { return _logInfoDataDOM; } } public ThreadLog() { _logInfoStack = new Stack<LogInformation>(); _logInfoDataDOM = new XmlDocument(); } private void InitializeQuicktakeLog() { _logInfoDataDOM.LoadXml("<LogEntry></LogEntry>"); XmlAttribute attr = _logInfoDataDOM.CreateAttribute("RequestURL"); attr.Value = (HttpContext.Current != null && HttpContext.Current.Request != null) ? string.Empty : HttpContext.Current.Request.RawUrl; _logInfoDataDOM.DocumentElement.Attributes.Append(attr); attr = _logInfoDataDOM.CreateAttribute("BeginTime"); attr.Value = DateTime.Now.ToString(); _logInfoDataDOM.DocumentElement.Attributes.Append(attr); attr = _logInfoDataDOM.CreateAttribute("TotalTime"); attr.Value = DateTime.Now.ToString(); _logInfoDataDOM.DocumentElement.Attributes.Append(attr); attr = _logInfoDataDOM.CreateAttribute("ThreadId"); attr.Value = Thread.CurrentThread.ManagedThreadId.ToString(); _logInfoDataDOM.DocumentElement.Attributes.Append(attr); _hasInitialized = true; } public void MakeLog(string description, string data) { if (!_hasInitialized) { InitializeThreadLog(); } XmlElement XE = _logInfoDataDOM.CreateElement("Log"); _logInfoDataDOM.DocumentElement.AppendChild(XE); XmlAttribute attr = _logInfoDataDOM.CreateAttribute("Id"); attr.Value = _logId.ToString(); _logId++; XE.Attributes.Append(attr); XmlElement XESub = _logInfoDataDOM.CreateElement("LogStep"); XESub.InnerText = data; attr = _logInfoDataDOM.CreateAttribute("Description"); attr.Value = description; XESub.Attributes.Append(attr); XE.AppendChild(XESub); } public void BeginLog(string functionName, string variableInfo,string note) { LogInformation LogInfo = new LogInformation(_logId, note, functionName, variableInfo); if (!_hasInitialized) { InitializeThreadLog(); } _logInfoStack.Push(new LogInformation(_logId, note, functionName, variableInfo)); _logId++; } public void EndLog(string data) { if (_logInfoStack.Count != 0) { LogInformation logInfo = _logInfoStack.Pop(); logInfo.EndTime = DateTime.Now; logInfo.TotalTime = logInfo.EndTime - logInfo.BeginTime; XmlNode NodeXml = _logInfoDataDOM.DocumentElement as XmlNode; if (NodeXml != null) { XmlElement LogXml = _logInfoDataDOM.CreateElement("Log"); NodeXml.AppendChild(LogXml); XmlAttribute logidAttr = _logInfoDataDOM.CreateAttribute("Id"); logidAttr.Value = logInfo.Id.ToString(); LogXml.Attributes.Append(logidAttr); XmlAttribute attr = _logInfoDataDOM.CreateAttribute("TotalTime"); attr.Value = (logInfo.EndTime - logInfo.BeginTime).TotalMilliseconds.ToString(); LogXml.Attributes.Append(attr); attr = _logInfoDataDOM.CreateAttribute("BeginTime"); attr.Value = logInfo.BeginTime.ToString(); LogXml.Attributes.Append(attr); LogXml.InnerXml = logInfo.FunctionInfo; LogXml.InnerXml += logInfo.VariableInfo; XmlElement LogXml2 = _logInfoDataDOM.CreateElement("Data"); LogXml.AppendChild(LogXml2); LogXml2.InnerXml = data; } } if (_logInfoStack.Count == 0) { if (_logInfoDataDOM.DocumentElement.Attributes["TotalTime"] != null) { _logInfoDataDOM.DocumentElement.Attributes["TotalTime"].Value = ( DateTime.Now - DateTime.Parse(_logInfoDataDOM.DocumentElement.Attributes["TotalTime"].Value)).TotalMilliseconds.ToString(); } } } public void CheckException(Exception exception) { if (_logInfoStack.Count > 0) { LogInformation LogNode = _logInfoStack.Pop(); XmlElement ExcpElem = _logInfoDataDOM.CreateElement("ExceptionDescription"); ExcpElem.InnerXml = "<Exception>" + exception.Message + Environment.NewLine +exception.Source + Environment.NewLine + exception.StackTrace + Environment.NewLine + "</Exception>" + LogNode.FunctionInfo + LogNode.VariableInfo; _logInfoDataDOM.DocumentElement.AppendChild(ExcpElem); _logInfoDataDOM.DocumentElement.Attributes["TotalTime"].Value = (DateTime.Now - DateTime.Parse(_logInfoDataDOM.DocumentElement.Attributes["BeginTime"].Value)).TotalMilliseconds.ToString(); _logInfoStack = null; } } } public class LogInformation { private uint _Id; private TimeSpan _totalTime; private DateTime _beginTime; private DateTime _endTime; private string _data = null; private string _functionInfo = null; private string _variableInfo = null; private string _note = null; public uint Id { set { _Id = value; } get { return _Id; } } public TimeSpan TotalTime { set{ _totalTime = value; } get { return _totalTime; } } public DateTime BeginTime { set { _beginTime = value; } get { return _beginTime; } } public DateTime EndTime { set { _endTime = value; } get { return _endTime; } } public string FunctionInfo { set { _functionInfo = value; } get { return _functionInfo; } } public string VariableInfo { set { _variableInfo = value; } get { return _variableInfo; } } public string Note { set { _note = value; } get { return _note; } } public string Data { set { _data = value; } get { return _data; } } public LogInformation(uint Id, string note, string functionNameInfo, string variableInfo) { Initialize(); _Id = Id; _note = note; _functionInfo = functionNameInfo; _variableInfo = variableInfo; } private void Initialize() { _totalTime = TimeSpan.Zero; _beginTime = DateTime.Now; _endTime = _beginTime; } }}