public delegate void DebugCallback(Int32 type, String str); // 委托 public class DebugMsg { public int Type { get; set; } public String Message { get; set; } public DebugMsg(Int32 type, String str) { this.Type = type; this.Message = str; } } // 调试消息 public class CallbackManager { #region 单例 private static CallbackManager instance = null; public static CallbackManager Instance // 只读属性 { get { if (instance == null) { instance = new CallbackManager(); instance.StartProcess(); // 启用处理线程 } return CallbackManager.instance; } } #endregion public static DebugCallback G_D = new DebugCallback(delegate(Int32 type, String str) { // System.Diagnostics.Debug.WriteLine("异常类型:" + type + "异常信息:" + str); }); private static int G_D_ClientCount = 0; private static int G_D_MAX = 10; // 最大接入客户端数目 private static object G_D_LOCK = new object(); // 同步锁 private static Queue<DebugMsg> QUEUE_BUFF = new Queue<DebugMsg>(); // 调试信息缓存 private static object QUEUE_BUFF_LOCK = new object(); // 同步锁 private static Boolean IS_PROCESS = true; public CallbackManager() { } #region 对外接口 // 输出调试信息 public void _D(Int32 type, String str) { lock (QUEUE_BUFF_LOCK) // 同步操做 { try { QUEUE_BUFF.Enqueue(new DebugMsg(type, str)); Monitor.Pulse(QUEUE_BUFF_LOCK); } catch { }; } } // 输出调试信息 public void _D(String str) { lock (QUEUE_BUFF_LOCK) // 同步操做 { try { QUEUE_BUFF.Enqueue(new DebugMsg(0, str)); Monitor.Pulse(QUEUE_BUFF_LOCK); } catch { }; } } // 添加客户端调试信息输出 public void AddClient(DebugCallback client) { lock (G_D_LOCK) // 同步操做 { try { if (G_D_ClientCount >= G_D_MAX) return; G_D += client; G_D_ClientCount++; _D("当前调试客户端个数:" + G_D_ClientCount); } catch { } } } // 删除客户端调试信息输出 public void RemoveClient(DebugCallback client) { lock (G_D_LOCK) // 同步操做 { try { G_D -= client; G_D_ClientCount--; _D("当前调试客户端个数:" + G_D_ClientCount); } catch { } } } #endregion // 处理缓存队列消息 private void StartProcess() { ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object o){ while (IS_PROCESS) { lock (QUEUE_BUFF_LOCK) // 同步操做 { if (QUEUE_BUFF.Count > 0) { lock (G_D_LOCK) { try { // 输出异常消息 DebugMsg deque = QUEUE_BUFF.Dequeue(); G_D(deque.Type, deque.Message); // 从队列中输出调试消息 } catch { } } } else { Monitor.Wait(QUEUE_BUFF_LOCK); } } } })); }
Boolean isOnline = true; // GET: /System/ public ActionResult Index() { #region 滚动条控制 Response.Write("<html onclick=\"clearInterval(i_1);\" ondblclick =\"reInterval()\"><head><title>服务器实时监控</title></head>"); Response.Write("<script type=\"text/javascript\">"); Response.Write("function scrollWindow() { document.body.scrollTop = document.body.scrollHeight; }"); Response.Write("function reInterval() { i_1 = setInterval('scrollWindow()', 50); }"); Response.Write("i_1 = setInterval('scrollWindow()', 50);"); Response.Write("scrollWindow();"); Response.Write("</script> "); Response.Flush(); #endregion Dictionary<int, string> dicColor = new Dictionary<int, string>() { {0,"#0000FF"}, // 蓝色 {1,"#FF3333"}, // 红色 {2,"#FFFF00"}, // 黄色 {3,"#FF3EFF"}, {4,"#0000FF"}, {5,"#0000FF"} }; DebugCallback callback = new DebugCallback(delegate(int type, string str) { if (dicColor.ContainsKey(type)) { Response.Write("<span style = 'color:" + dicColor[type] + ";'>" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "--" + str + "</span> <br />"); } Response.Flush(); }); Log.AddCallBack(callback); while (isOnline) { try { Response.Write("...<br>"); Response.Flush(); } catch { } System.Threading.Thread.Sleep(1000); if (!Response.IsClientConnected) // 链接关闭 { Log.RemoveCallBack(callback); Response.Write("</html>"); break; } } return null; }
长连接会涉及到Session阻塞问题,详细说明请见:http://www.cnblogs.com/fanqie-liuxiao/p/5702633.htmljavascript