New test.
[mono.git] / mcs / class / System.Web / System.Web / TraceContext.cs
1 // 
2 // System.Web.TraceContext
3 //
4 // Author:
5 //   Patrik Torstensson (Patrik.Torstensson@labs2.com)
6 //   Jackson Harper (jackson@ximian.com)
7 //
8 // (C) 2002 2003, Patrik Torstensson
9 // Copyright (C) 2003,2005 Novell, Inc (http://www.novell.com)
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 using System.Collections;
32 using System.Security.Permissions;
33 using System.Web.UI;
34
35 namespace System.Web {
36
37         // CAS - no InheritanceDemand here as the class is sealed
38         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
39         public sealed class TraceContext {
40                 HttpContext _Context;
41                 bool _Enabled;
42                 TraceMode _Mode;
43                 TraceData data;
44                 bool data_saved;
45                 bool _haveTrace;
46                 Hashtable view_states;
47                 Hashtable sizes;
48
49                 public TraceContext (HttpContext Context)
50                 {
51                         _Context = Context;
52                         _Mode = TraceMode.SortByTime;
53                 }
54
55                 internal bool HaveTrace {
56                         get {
57                                 return _haveTrace;
58                         }
59                 }
60
61                 public bool IsEnabled {
62                         get {
63                                 if (!_haveTrace)
64                                         return HttpRuntime.TraceManager.Enabled;
65                                 return _Enabled;
66                         }
67
68                         set {
69                                 if (value && data == null)
70                                         data = new TraceData ();
71                                 _haveTrace = true;
72                                 _Enabled = value;
73                         }
74                 }
75
76                 public TraceMode TraceMode {
77                         get {
78                                 return _Mode;
79                         }
80                         set {
81                                 _Mode = value;
82                         }
83                 }
84
85                 public void Warn(string msg)
86                 {
87                         Write (String.Empty, msg, null, true);
88                 }
89
90                 public void Warn(string category, string msg)
91                 {
92                         Write (category, msg, null, true);
93                 }
94
95                 public void Warn (string category, string msg, Exception error)
96                 {
97                         Write (category, msg, error, true);
98                 }
99
100                 public void Write (string msg)
101                 {
102                         Write (String.Empty, msg, null, false);
103                 }
104
105                 public void Write (string category, string msg)
106                 {
107                         Write (category, msg, null, false);
108                 }
109
110                 public void Write (string category, string msg, Exception error)
111                 {
112                         Write (category, msg, error, false);
113                 }
114
115                 void Write (string category, string msg, Exception error, bool Warning)
116                 {
117                         if (!IsEnabled)
118                                 return;
119                         if (data == null)
120                                 data = new TraceData ();
121                         data.Write (category, msg, error, Warning);
122                 }
123 #if NET_2_0
124                 [MonoTODO]
125                 public event TraceContextEventHandler TraceFinished;
126 #endif
127                 internal void SaveData ()
128                 {
129                         if (data == null)
130                                 data = new TraceData ();
131                         SetRequestDetails ();
132                         if (_Context.Handler is Page)
133                                 data.AddControlTree ((Page) _Context.Handler, view_states, sizes);
134
135                         AddCookies ();
136                         AddHeaders ();
137                         AddServerVars ();
138                         HttpRuntime.TraceManager.AddTraceData (data);
139                         data_saved = true;
140                 }
141
142                 internal void SaveViewState (Control ctrl, object vs)
143                 {
144                         if (view_states == null)
145                                 view_states = new Hashtable ();
146
147                         view_states [ctrl] = vs;
148                 }
149
150                 internal void SaveSize (Control ctrl, int size)
151                 {
152                         if (sizes == null)
153                                 sizes = new Hashtable ();
154
155                         sizes [ctrl] = size;
156                 }
157
158                 internal void Render (HtmlTextWriter output)
159                 {
160                         if (!data_saved)
161                                 SaveData ();
162                         data.Render (output);
163                 }
164
165                 void SetRequestDetails ()
166                 {
167                         data.RequestPath = _Context.Request.FilePath;
168                         data.SessionID = (_Context.Session != null ? _Context.Session.SessionID : String.Empty);
169                         data.RequestType = _Context.Request.RequestType;
170                         data.RequestTime = _Context.Timestamp;
171                         data.StatusCode = _Context.Response.StatusCode;
172                         data.RequestEncoding = _Context.Request.ContentEncoding;
173                         data.ResponseEncoding = _Context.Response.ContentEncoding;
174                 }
175
176                 void AddCookies ()
177                 {
178                         foreach (string key in _Context.Request.Cookies.Keys)
179                                 data.AddCookie (key, _Context.Request.Cookies [key].Value);
180                 }
181
182                 void AddHeaders ()
183                 {
184                         foreach (string key in _Context.Request.Headers.Keys)
185                                 data.AddHeader (key, _Context.Request.Headers [key]);
186                 }
187
188                 void AddServerVars ()
189                 {
190                         foreach (string key in _Context.Request.ServerVariables)
191                                 data.AddServerVar (key, _Context.Request.ServerVariables [key]);
192                 }
193         }
194 }
195