2 // System.Web.TraceContext
5 // Patrik Torstensson (Patrik.Torstensson@labs2.com)
6 // Jackson Harper (jackson@ximian.com)
8 // (C) 2002 2003, Patrik Torstensson
9 // Copyright (C) 2003,2005 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
32 using System.Security.Permissions;
35 namespace System.Web {
37 // CAS - no InheritanceDemand here as the class is sealed
38 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39 public sealed class TraceContext {
46 Hashtable view_states;
49 public TraceContext (HttpContext Context)
52 _Mode = TraceMode.SortByTime;
55 internal bool HaveTrace {
61 public bool IsEnabled {
64 return HttpRuntime.TraceManager.Enabled;
69 if (value && data == null)
70 data = new TraceData ();
76 public TraceMode TraceMode {
85 public void Warn(string msg)
87 Write (String.Empty, msg, null, true);
90 public void Warn(string category, string msg)
92 Write (category, msg, null, true);
95 public void Warn (string category, string msg, Exception error)
97 Write (category, msg, error, true);
100 public void Write (string msg)
102 Write (String.Empty, msg, null, false);
105 public void Write (string category, string msg)
107 Write (category, msg, null, false);
110 public void Write (string category, string msg, Exception error)
112 Write (category, msg, error, false);
115 void Write (string category, string msg, Exception error, bool Warning)
120 data = new TraceData ();
121 data.Write (category, msg, error, Warning);
124 public event TraceContextEventHandler TraceFinished;
126 internal void SaveData ()
129 data = new TraceData ();
130 SetRequestDetails ();
131 if (_Context.Handler is Page)
132 data.AddControlTree ((Page) _Context.Handler, view_states, sizes);
137 HttpRuntime.TraceManager.AddTraceData (data);
141 internal void SaveViewState (Control ctrl, object vs)
143 if (view_states == null)
144 view_states = new Hashtable ();
146 view_states [ctrl] = vs;
149 internal void SaveSize (Control ctrl, int size)
152 sizes = new Hashtable ();
157 internal void Render (HtmlTextWriter output)
161 data.Render (output);
164 void SetRequestDetails ()
166 data.RequestPath = _Context.Request.FilePath;
167 data.SessionID = (_Context.Session != null ? _Context.Session.SessionID : String.Empty);
168 data.RequestType = _Context.Request.RequestType;
169 data.RequestTime = _Context.Timestamp;
170 data.StatusCode = _Context.Response.StatusCode;
171 data.RequestEncoding = _Context.Request.ContentEncoding;
172 data.ResponseEncoding = _Context.Response.ContentEncoding;
177 foreach (string key in _Context.Request.Cookies.Keys)
178 data.AddCookie (key, _Context.Request.Cookies [key].Value);
183 foreach (string key in _Context.Request.Headers.Keys)
184 data.AddHeader (key, _Context.Request.Headers [key]);
187 void AddServerVars ()
189 foreach (string key in _Context.Request.ServerVariables)
190 data.AddServerVar (key, _Context.Request.ServerVariables [key]);