技术开发 频道

消息捕获类


【IT168技术文档】

  在日常编码当中,我们经常要处理一些错误,为了更好的方便得到错误信息,我仿一些代码写了如下的类。
using System; using System.Collections; namespace JFrameWork { public class MessageStack { static Message _message = null; static System.Collections.Stack _stack = new System.Collections.Stack(); static int _totalofpush = 0; // public MessageStack(Message message) // { // Push(message); // } public static string ToMessageString() { try { string error =""; while(_stack.Count > 0) { Pop(); error += "\r\n[Time]:"+_message.time.ToString("yyyy-MM-dd hh:mm:ss"); error += "\r\n[Body]:"+_message.body; error += "\r\n[State]:"+_message.state.ToString(); } return error; } catch(System.Exception e) { MessageStack.Push(new Message("[Source]:"+e.Source + "\r\n[Message]:" +e.Message +"\r\n[StackTrace]:"+e.StackTrace +"\r\n[HelpLink]:"+e.HelpLink)); return "Error!"; } } public static void WriteLog() { string xml = System.Environment.CurrentDirectory + @"/Big.Mouth.Z." + System.DateTime.Now.ToString("yyyyMMddhhmmss") + ".Log"; WriteLog(xml); } public static void WriteLog(string xml) { try { // string xml = @"c:/Message" + System.DateTime.Now.ToString("yyyyMMddhhmmss") + ".log"; System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(xml,System.Text.Encoding.UTF8); writer.Formatting = System.Xml.Formatting.Indented; writer.IndentChar = '\t'; writer.WriteStartDocument(); writer.WriteComment("Message Error Log." + System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")); writer.WriteStartElement("MessageInfo"); System.Collections.Stack tmp = (System.Collections.Stack)_stack.Clone(); int i = 0; while(tmp.Count > 0) { i++; Message tmpmessage = (Message)tmp.Pop(); writer.WriteStartElement("Message"); writer.WriteElementString("Row",i.ToString()); writer.WriteStartElement("Body"); writer.WriteCData("\r\n"+tmpmessage.body+"\r\n"); writer.WriteEndElement(); writer.WriteElementString("Time",tmpmessage.time.ToString("yyyy-MM-dd hh:mm:ss")); writer.WriteElementString("State",tmpmessage.state.ToString()); writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteEndDocument(); writer.WriteComment("Author: Bigmouthz@GMail.Com"); writer.Flush(); writer.Close(); tmp.Clear(); } catch(System.Exception e) { MessageStack.Push(new Message("[Source]:"+e.Source + "\r\n[Message]:" +e.Message +"\r\n[StackTrace]:"+e.StackTrace +"\r\n[HelpLink]:"+e.HelpLink)); } } public static void Push(Message message) { _message = message; _stack.Push(_message); _totalofpush++; } public static Message Pop() { if (_stack.Count > 0) { _message = (Message)_stack.Pop(); } else { _message = null; } return _message; } public static void Clear() { _stack.Clear(); } public static object Clone() { return _stack.Clone(); } public static string Body { get { if (_message != null) return _message.body; else return "Message is Null!"; } } public static System.DateTime Time { get { if (_message != null) return _message.time; else return System.DateTime.Now; } } public static bool State { get { if (_message != null) return _message.state; else return true; } } public static int Count { get { return _stack.Count; } } public static int TotalOfPush { get { return _totalofpush;} } } public sealed class Message { private string _body; private System.DateTime _time ; private bool _state; public Message(string body, bool state) { _body = body; _time = System.DateTime.Now; _state = state; } public Message(string body) { _body = body; _time = System.DateTime.Now; _state = false; } public string body { get { return _body;} } public System.DateTime time { get { return _time;} } public bool state { get { return _state;} } } }
0
相关文章