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