2007-08-23 Marek Habersack <mhabersack@novell.com>
[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                 TraceManager _traceManager;
42                 bool _Enabled;
43                 TraceMode _Mode;
44                 TraceData data;
45                 bool data_saved;
46                 bool _haveTrace;
47                 Hashtable view_states;
48                 Hashtable sizes;
49
50                 public TraceContext (HttpContext Context)
51                 {
52                         _Context = Context;
53                         _Mode = TraceMode.SortByTime;
54                 }
55
56                 internal bool HaveTrace {
57                         get {
58                                 return _haveTrace;
59                         }
60                 }
61
62                 public bool IsEnabled {
63                         get {
64                                 if (!_haveTrace)
65                                         return TraceManager.Enabled;
66                                 return _Enabled;
67                         }
68
69                         set {
70                                 if (value && data == null)
71                                         data = new TraceData ();
72                                 _haveTrace = true;
73                                 _Enabled = value;
74                         }
75                 }
76
77                 TraceManager TraceManager
78                 {
79                         get
80                         {
81                                 if (_traceManager == null)
82                                         _traceManager = HttpRuntime.TraceManager;
83
84                                 return _traceManager;
85                         }
86                 }
87
88                 public TraceMode TraceMode {
89                         get {
90                                 return _Mode;
91                         }
92                         set {
93                                 _Mode = value;
94                         }
95                 }
96
97                 public void Warn(string msg)
98                 {
99                         Write (String.Empty, msg, null, true);
100                 }
101
102                 public void Warn(string category, string msg)
103                 {
104                         Write (category, msg, null, true);
105                 }
106
107                 public void Warn (string category, string msg, Exception error)
108                 {
109                         Write (category, msg, error, true);
110                 }
111
112                 public void Write (string msg)
113                 {
114                         Write (String.Empty, msg, null, false);
115                 }
116
117                 public void Write (string category, string msg)
118                 {
119                         Write (category, msg, null, false);
120                 }
121
122                 public void Write (string category, string msg, Exception error)
123                 {
124                         Write (category, msg, error, false);
125                 }
126
127                 void Write (string category, string msg, Exception error, bool Warning)
128                 {
129                         if (!IsEnabled)
130                                 return;
131                         if (data == null)
132                                 data = new TraceData ();
133                         data.Write (category, msg, error, Warning);
134                 }
135 #if NET_2_0
136                 public event TraceContextEventHandler TraceFinished;
137 #endif
138                 internal void SaveData ()
139                 {
140                         if (data == null)
141                                 data = new TraceData ();
142                         SetRequestDetails ();
143                         if (_Context.Handler is Page)
144                                 data.AddControlTree ((Page) _Context.Handler, view_states, sizes);
145
146                         AddCookies ();
147                         AddHeaders ();
148                         AddServerVars ();
149                         TraceManager.AddTraceData (data);
150                         data_saved = true;
151                 }
152
153                 internal void SaveViewState (Control ctrl, object vs)
154                 {
155                         if (view_states == null)
156                                 view_states = new Hashtable ();
157
158                         view_states [ctrl] = vs;
159                 }
160
161                 internal void SaveSize (Control ctrl, int size)
162                 {
163                         if (sizes == null)
164                                 sizes = new Hashtable ();
165
166                         sizes [ctrl] = size;
167                 }
168
169                 internal void Render (HtmlTextWriter output)
170                 {
171                         if (!data_saved)
172                                 SaveData ();
173                         data.Render (output);
174                 }
175
176                 void SetRequestDetails ()
177                 {
178                         data.RequestPath = _Context.Request.FilePath;
179                         data.SessionID = (_Context.Session != null ? _Context.Session.SessionID : String.Empty);
180                         data.RequestType = _Context.Request.RequestType;
181                         data.RequestTime = _Context.Timestamp;
182                         data.StatusCode = _Context.Response.StatusCode;
183                         data.RequestEncoding = _Context.Request.ContentEncoding;
184                         data.ResponseEncoding = _Context.Response.ContentEncoding;
185                 }
186
187                 void AddCookies ()
188                 {
189                         foreach (string key in _Context.Request.Cookies.Keys)
190                                 data.AddCookie (key, _Context.Request.Cookies [key].Value);
191                 }
192
193                 void AddHeaders ()
194                 {
195                         foreach (string key in _Context.Request.Headers.Keys)
196                                 data.AddHeader (key, _Context.Request.Headers [key]);
197                 }
198
199                 void AddServerVars ()
200                 {
201                         foreach (string key in _Context.Request.ServerVariables)
202                                 data.AddServerVar (key, _Context.Request.ServerVariables [key]);
203                 }
204         }
205 }
206