LogView.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // **********************************************************************************
  2. // CassiniDev - http://cassinidev.codeplex.com
  3. //
  4. // Copyright (c) Sky Sanders. All rights reserved.
  5. //
  6. // This source code is subject to terms and conditions of the Microsoft Public
  7. // License (Ms-PL). A copy of the license can be found in the license.htm file
  8. // included in this distribution.
  9. //
  10. // You must not remove this notice, or any other, from this software.
  11. //
  12. // **********************************************************************************
  13. #region
  14. using System;
  15. using System.Collections.Generic;
  16. using System.ComponentModel;
  17. using System.Windows.Forms;
  18. #endregion
  19. namespace CassiniDev.ServerLog
  20. {
  21. /// <summary>
  22. /// If log can persist, then only stack values in the list and tag the items with the RowId for querying the db for heavy values.
  23. /// If not, tag the item with the loginfo instance. Memory usage will be much greater but is better than nothing.
  24. /// </summary>
  25. public partial class LogView : Form
  26. {
  27. private Server _server;
  28. public LogView(Server server)
  29. {
  30. InitializeComponent();
  31. _server = server;
  32. _server.RequestComplete += RequestComplete;
  33. InitializeList();
  34. base.Text = SR.GetString(SR.WebdevLogViewerNameWithPort, _server.Port);
  35. }
  36. /// <summary>
  37. /// Not sure if these qualify for disposable
  38. /// </summary>
  39. /// <param name="e"></param>
  40. protected override void OnClosing(CancelEventArgs e)
  41. {
  42. _server.RequestComplete -= RequestComplete;
  43. _server = null;
  44. }
  45. private void AddLogRows(IEnumerable<LogInfo> items)
  46. {
  47. listView1.SuspendLayout();
  48. foreach (LogInfo item in items)
  49. {
  50. ListViewItem a = new ListViewItem(new[]
  51. {
  52. item.RowType == 0 ? "" : item.RowType == 1 ? "Request" : "Response",
  53. item.Created.ToString(),
  54. item.StatusCode.ToString(),
  55. item.Url,
  56. item.PathTranslated,
  57. item.Identity
  58. })
  59. {
  60. Tag = item
  61. };
  62. listView1.Items.Add(a);
  63. }
  64. if (listView1.Items.Count > 0 && scrollLogToolStripMenuItem.Checked)
  65. {
  66. int lastRow = listView1.Items.Count - 1;
  67. listView1.EnsureVisible(lastRow);
  68. }
  69. listView1.ResumeLayout();
  70. }
  71. private void clearToolStripMenuItem_Click(object sender, EventArgs e)
  72. {
  73. InitializeList();
  74. }
  75. private void exitToolStripMenuItem_Click(object sender, EventArgs e)
  76. {
  77. Close();
  78. }
  79. private LogInfo GetSelectedLogItem()
  80. {
  81. object tag = listView1.Items[listView1.SelectedIndices[0]].Tag;
  82. LogInfo returnValue = (LogInfo)tag;
  83. return returnValue;
  84. }
  85. private void InitializeList()
  86. {
  87. listView1.SuspendLayout();
  88. listView1.Items.Clear();
  89. listView1.ResumeLayout();
  90. }
  91. private void listView1_SelectedIndexChanged(object sender, EventArgs e)
  92. {
  93. if (listView1.SelectedIndices.Count > 0)
  94. {
  95. LogInfo log = GetSelectedLogItem();
  96. exceptionRichTextBox.Text = log.Exception;
  97. headersRichTextBox.Text = log.Headers;
  98. bodyBodyView.Value = log.Body;
  99. }
  100. else
  101. {
  102. exceptionRichTextBox.Text = "";
  103. headersRichTextBox.Text = "";
  104. bodyBodyView.Value = null;
  105. }
  106. }
  107. private void RequestComplete(object sender, RequestEventArgs e)
  108. {
  109. Invoke(new MethodInvoker(() => AddLogRows(new[] { e.RequestLog, e.ResponseLog })));
  110. }
  111. }
  112. }