using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Collections; using System.Security.Principal; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using CMS.CMSHelper; using CMS.DataEngine; using CMS.EventLog; using CMS.GlobalHelper; using CMS.SettingsProvider; using CMS.SiteProvider; using CMS.UIControls; public partial class CMSPages_GetTestingModeReport : CMSPage { private static int id = 0; /// /// Returns information about web.config. /// private string GetAppSettingsInfo() { StringBuilder sb = new StringBuilder(); foreach (string key in ConfigurationManager.AppSettings.AllKeys) { sb.AppendLine("
"); sb.AppendLine(GetAppSetting(key)); sb.AppendLine("
"); } return sb.ToString(); } /// /// Returns one web.config key info. /// /// Key name private string GetAppSetting(string key) { string setting = ""; return HTMLHelper.HTMLEncode(setting); } /// /// Returns information about web.config. /// private string GetConnectionStringsInfo() { StringBuilder sb = new StringBuilder(); foreach (ConnectionStringSettings key in ConfigurationManager.ConnectionStrings) { sb.AppendLine("
"); sb.AppendLine(GetConnStr(key)); sb.AppendLine("
"); } return sb.ToString(); } /// /// Returns one connection string info. /// /// Connection string name private string GetConnStr(ConnectionStringSettings connStr) { string setting = ""; return HTMLHelper.HTMLEncode(setting); } /// /// Returns CMS Version with build time. /// private string GetCMSVersion() { Version v = CMSContext.GetCMSVersion(); string result = v.ToString(4); DateTime date = new DateTime(2000, 1, 1); date = date.AddDays(v.Build).AddSeconds(v.MinorRevision * 2); return result + " (" + date.ToString() + ")"; } /// /// Returns basic testing information. /// private string GetBasicInfo() { StringBuilder sb = new StringBuilder(); sb.Append("", "", "", "", "", "", "
", ResHelper.GetString("cmstesting.livesiteurl"), "", URLHelper.GetApplicationUrl(), "
", ResHelper.GetString("cmstesting.buildnumber"), "", GetCMSVersion(), "
", ResHelper.GetString("cmstesting.currentuser"), "", CMSContext.CurrentUser.UserName, "
", ResHelper.GetString("cmstesting.useragent"), "", BrowserHelper.GetUserAgent(), "
", ResHelper.GetString("cmstesting.connectionstrings"), "", GetConnectionStringsInfo(), "
"); return sb.ToString(); } /// /// Returns last 20 events from EventLog. /// private string GetEventLog() { StringBuilder sb = new StringBuilder(); sb.Append("
"); EventLogProvider eventProvider = new EventLogProvider(); DataSet ds = eventProvider.GetAllEvents("EventType = 'E' OR EventType = 'W'", "EventTime DESC", 20, null); if (!DataHelper.DataSourceIsEmpty(ds)) { sb.Append(""); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); string evenStyle = " background-color: rgb(244, 244, 244);"; int i = 0; foreach (DataRow dr in ds.Tables[0].Rows) { string siteName = SiteInfoProvider.GetSiteName(ValidationHelper.GetInteger(dr["SiteID"], 0)); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); i++; } sb.Append("
" + ResHelper.GetString("cmstesting.eventlog.EventType") + "" + ResHelper.GetString("cmstesting.eventlog.EventCode") + "" + ResHelper.GetString("cmstesting.eventlog.UserName") + "" + ResHelper.GetString("cmstesting.eventlog.IPAddress") + "" + ResHelper.GetString("cmstesting.eventlog.DocumentName") + "" + ResHelper.GetString("cmstesting.eventlog.SiteName") + "" + ResHelper.GetString("cmstesting.eventlog.EventMachineName") + "
" + dr["EventType"] + "" + dr["EventCode"] + "" + dr["UserName"] + "" + dr["IPAddress"] + "" + dr["DocumentName"] + "" + siteName + "" + dr["EventMachineName"] + "
"); string item = ""; foreach (DataColumn col in ds.Tables[0].Columns) { item += GetEventLogItem(col.ColumnName, dr); } item += "
"; sb.AppendLine(item); sb.AppendLine("
"); } sb.Append("
"); return sb.ToString(); } /// /// Returns one event log event item (one column transformed to one row in a table). /// /// Column name /// DataRow private string GetEventLogItem(string columnName, DataRow dr) { string item = ""; string itemValue = ValidationHelper.GetString(dr[columnName], ""); if (!string.IsNullOrEmpty(itemValue)) { item += "" + ResHelper.GetString("cmstesting.eventlog." + columnName) + "" + itemValue + ""; } return item; } /// /// Returns system information (renderes control on Site Manager / System / General tab). /// private string GetDebug() { StringBuilder sb = new StringBuilder(); int id = 0; for (int i = RequestHelper.LastLogs.Count - 1; i >= 0; i--) { // Get the request log RequestLog log = (RequestLog)RequestHelper.LastLogs[i]; if (log != null) { List logs = AllLog.GetLogs(log); if (logs.Count > 1) { DataTable table = AllLog.MergeTables(logs, Page, false); string tdStyle = "style=\"border: 1px solid rgb(204, 204, 204);\""; string evenStyle = "style=\"background-color: rgb(244, 244, 244);\""; string headerStyle = "style=\"border: 1px solid rgb(204, 204, 204); white-space: nowrap;\" scope=\"col\""; string tableStyle = "style=\"background-color: White; border-color: rgb(204, 204, 204); border-style: solid; width: 100%; border-collapse: collapse;\""; sb.Append("
" + log.RequestURL + "" + " (" + log.RequestTime + ")
"); sb.Append( "", ""); bool even = false; foreach (DataRow dr in table.Rows) { sb.Append( ""); even = !even; } sb.Append("
 ", GetString("AllLog.DebugType"), "", GetString("AllLog.Information"), "", GetString("AllLog.Result"), "", GetString("AllLog.Context"), "", GetString("AllLog.TotalDuration"), "", GetString("AllLog.Duration"), "
", dr["Index"], "", dr["DebugType"], "", dr["Information"], "", dr["Result"], "", GetContext(dr["Context"].ToString(), id++), "", dr["TotalDuration"], "", dr["Duration"], "
"); } } } return sb.ToString(); } /// /// Changes the script for hiding/showing context in the debug. /// /// Original context HTML /// ID of the control private string GetContext(string original, int id) { string result = original.Replace("this.nextSibling", "document.getElementById('context_" + id + "')"); result = result.Replace("
", "
"); return result; } /// /// Returns info from System / General tab. /// private string GetSystemInfo() { StringBuilder sb = new StringBuilder(); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(GetEmptyRow()); sb.AppendLine(""); sb.AppendLine(GetEmptyRow()); sb.AppendLine("
"); // System information sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(GetRow("Administration-System.MachineName", HTTPHelper.MachineName)); sb.AppendLine(GetEmptyRow()); sb.AppendLine(GetRow("Administration-System.Account", WindowsIdentity.GetCurrent().Name)); sb.AppendLine(GetRow("Administration-System.Version", Environment.Version.ToString())); string pool = "N/A"; try { pool = SystemHelper.GetApplicationPoolName(); } catch { } sb.AppendLine(GetRow("Administration-System.Pool", pool)); sb.AppendLine(GetEmptyRow()); sb.AppendLine(GetRow("Administration-System.IP", HTTPHelper.UserHostAddress)); sb.AppendLine(GetRow("Administration-System.MachineName", HTTPHelper.MachineName)); sb.AppendLine("
"); sb.AppendLine(ResHelper.GetString("Administration-System.SystemInfo")); sb.AppendLine("
"); sb.AppendLine("
"); // Database information sb.AppendLine(""); sb.AppendLine(""); TableManager tm = new TableManager(null); sb.AppendLine(GetRow("Administration-System.ServerName", tm.DatabaseServerName)); sb.AppendLine(GetRow("Administration-System.ServerVersion", tm.DatabaseServerVersion)); sb.AppendLine(GetEmptyRow()); sb.AppendLine(GetRow("Administration-System.DatabaseName", tm.DatabaseName)); sb.AppendLine(GetRow("Administration-System.DatabaseSize", tm.DatabaseSize)); sb.AppendLine("
"); sb.AppendLine(ResHelper.GetString("Administration-System.DatabaseInfo")); sb.AppendLine("
"); sb.AppendLine("
"); // Memory statistics sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(GetRow("Administration-System.Memory", DataHelper.GetSizeString(GC.GetTotalMemory(false)))); sb.AppendLine(GetEmptyRow()); string virtualMemory = "N/A"; string physicalMemory = "N/A"; string peakMemory = "N/A"; try { virtualMemory = DataHelper.GetSizeString(SystemHelper.GetVirtualMemorySize()); physicalMemory = DataHelper.GetSizeString(SystemHelper.GetWorkingSetSize()); peakMemory = DataHelper.GetSizeString(SystemHelper.GetPeakWorkingSetSize()); } catch { } sb.AppendLine(GetRow("Administration-System.PeakMemory", peakMemory)); sb.AppendLine(GetRow("Administration-System.PhysicalMemory", physicalMemory)); sb.AppendLine(GetRow("Administration-System.VirtualMemory", virtualMemory)); sb.AppendLine("
"); sb.AppendLine(ResHelper.GetString("Administration-System.MemoryStatistics")); sb.AppendLine("
"); sb.AppendLine("
"); // GC information sb.AppendLine(""); sb.AppendLine(""); try { int generations = GC.MaxGeneration; for (int i = 0; i <= generations; i++) { int count = GC.CollectionCount(i); sb.AppendLine(""); } } catch { } sb.AppendLine("
"); sb.AppendLine(ResHelper.GetString("Administration-System.GC")); sb.AppendLine("
" + GetString("GC.Generation") + " " + i.ToString() + ":" + count.ToString() + "
"); sb.AppendLine("
"); // Page view statistics long pending = RequestHelper.PendingRequests.GetValue(null); if (pending > 1) { // Current request does not count as pending at the time of display pending--; } if (pending < 0) { pending = 0; } // Remove miliseconds from the end of the time string string timeSpan = (DateTime.Now - CMSAppBase.ApplicationStart).ToString(); int index = timeSpan.LastIndexOfCSafe('.'); if (index >= 0) { timeSpan = timeSpan.Remove(index); } sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(GetRow("Administration-System.Pages", RequestHelper.TotalPageRequests.GetValue(null).ToString())); sb.AppendLine(GetRow("Administration-System.GetFilePages", RequestHelper.TotalGetFileRequests.GetValue(null).ToString())); sb.AppendLine(GetRow("Administration-System.SystemPages", RequestHelper.TotalSystemPageRequests.GetValue(null).ToString())); sb.AppendLine(GetRow("Administration-System.NonPages", RequestHelper.TotalNonPageRequests.GetValue(null).ToString())); sb.AppendLine(GetRow("Administration-System.PagesNotFound", RequestHelper.TotalPageNotFoundRequests.GetValue(null).ToString())); sb.AppendLine(GetRow("Administration-System.Pending", pending.ToString())); sb.AppendLine(GetEmptyRow()); sb.AppendLine(GetRow("Administration.System.RunTime", timeSpan)); sb.AppendLine("
"); sb.AppendLine(ResHelper.GetString("Administration-System.PageViews")); sb.AppendLine(""); sb.AppendLine(ResHelper.GetString("Administration-System.PageViewsValues")); sb.AppendLine("
"); sb.AppendLine("
"); // Cache statistics sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(GetRow("Administration-System.CacheItems", Cache.Count.ToString())); sb.AppendLine(GetEmptyRow()); sb.AppendLine(GetRow("Administration-System.CacheExpired", CacheHelper.Expired.GetValue(null).ToString())); sb.AppendLine(GetRow("Administration-System.CacheRemoved", CacheHelper.Removed.GetValue(null).ToString())); sb.AppendLine(GetRow("Administration-System.CacheDependency", CacheHelper.DependencyChanged.GetValue(null).ToString())); sb.AppendLine(GetRow("Administration-System.CacheUnderused", CacheHelper.Underused.GetValue(null).ToString())); sb.AppendLine("
"); sb.AppendLine(ResHelper.GetString("Administration-System.CacheStatistics")); sb.AppendLine("
"); sb.AppendLine("
"); return sb.ToString(); } /// /// Creates one row of a table (tr tag). /// /// Left part /// Right part private string GetEmptyRow() { return " "; } /// /// Creates one row of a table (tr tag). /// /// Left part /// Right part private string GetRow(string title, string content) { return "" + ResHelper.GetString(title) + "" + content + ""; } /// /// Creates show hide section. /// /// Title of the section /// Content which can be dynamically shown/hidden /// If true content is shown by default /// If true, title is put into h2 tag private string MakeShowHideSection(string title, string content, bool showByDefault, bool isHeader) { string titleTag = (isHeader ? "

" + HTMLHelper.HTMLEncode(title) + "

" : HTMLHelper.HTMLEncode(title)); string result = "
" + titleTag + ""; result += "
" + content + "
"; id++; return result; } /// /// Generates report code. /// public string GetReportCode() { StringBuilder sb = new StringBuilder(); sb.Append( "", "", "CMS Testing Report", "

" + ResHelper.GetString("cmstesting.basicinformation") + "

", "
", GetBasicInfo(), MakeShowHideSection(ResHelper.GetString("cmstesting.system"), GetSystemInfo(), false, true), "
", "

" + ResHelper.GetString("cmstesting.advancedinformation") + "

", "
", MakeShowHideSection(ResHelper.GetString("cmstesting.webconfig"), GetAppSettingsInfo(), false, true), MakeShowHideSection(ResHelper.GetString("cmstesting.eventlog"), GetEventLog(), false, true), MakeShowHideSection(ResHelper.GetString("cmstesting.debug"), GetDebug(), false, true), "
"); return HTMLHelper.ReformatHTML(sb.ToString(), " "); } /// /// Creates the download report response. /// private void MakeResponse() { // Clear all generated HTML code Response.Clear(); // Generate sql script string responseText = GetReportCode(); if (responseText != null) { // Add header containing attachment Response.AddHeader("content-disposition", "attachment;filename=cmstesting_report.html"); // Ensure UTF-8 formating Response.ContentEncoding = Encoding.UTF8; // Set content type Response.ContentType = "text/html"; Response.Charset = string.Empty; // Disable view state EnableViewState = false; // Clear content of response Response.ClearContent(); // Start writing sql script to output stream Response.Output.Write(responseText); Response.Output.Close(); // End response RequestHelper.EndResponse(); } } protected void Page_Load(object sender, EventArgs e) { if (SettingsKeyProvider.TestingMode) { MakeResponse(); } } }