svn path=/branches/mono-1-1-9/mono/; revision=51217
[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 // (C) 2003 Novell, Inc (http://www.novell.com) 
10 //
11
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System;
34 using System.Collections;
35 using System.Data;
36 using System.Web.UI;
37 using System.Web.UI.WebControls;
38
39 namespace System.Web {
40         public sealed class TraceContext {
41                 HttpContext _Context;
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                 }
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
124                 internal void SaveData ()
125                 {
126                         if (data == null)
127                                 data = new TraceData ();
128                         SetRequestDetails ();
129                         if (_Context.Handler is Page)
130                                 data.AddControlTree ((Page) _Context.Handler, view_states, sizes);
131
132                         AddCookies ();
133                         AddHeaders ();
134                         AddServerVars ();
135                         HttpRuntime.TraceManager.AddTraceData (data);
136                         data_saved = true;
137                 }
138
139                 internal void SaveViewState (Control ctrl, object vs)
140                 {
141                         if (view_states == null)
142                                 view_states = new Hashtable ();
143
144                         view_states [ctrl] = vs;
145                 }
146
147                 internal void SaveSize (Control ctrl, int size)
148                 {
149                         if (sizes == null)
150                                 sizes = new Hashtable ();
151
152                         sizes [ctrl] = size;
153                 }
154
155                 internal void Render (HtmlTextWriter output)
156                 {
157                         if (!data_saved)
158                                 SaveData ();
159                         data.Render (output);
160                 }
161
162                 void SetRequestDetails ()
163                 {
164                         data.RequestPath = _Context.Request.FilePath;
165                         data.SessionID = (_Context.Session != null ? _Context.Session.SessionID : String.Empty);
166                         data.RequestType = _Context.Request.RequestType;
167                         data.RequestTime = _Context.Timestamp;
168                         data.StatusCode = _Context.Response.StatusCode;
169                         data.RequestEncoding = _Context.Request.ContentEncoding;
170                         data.ResponseEncoding = _Context.Response.ContentEncoding;
171                 }
172
173                 void AddCookies ()
174                 {
175                         foreach (string key in _Context.Request.Cookies.Keys)
176                                 data.AddCookie (key, _Context.Request.Cookies [key].Value);
177                 }
178
179                 void AddHeaders ()
180                 {
181                         foreach (string key in _Context.Request.Headers.Keys)
182                                 data.AddHeader (key, _Context.Request.Headers [key]);
183                 }
184
185                 void AddServerVars ()
186                 {
187                         foreach (string key in _Context.Request.ServerVariables)
188                                 data.AddServerVar (key, _Context.Request.ServerVariables [key]);
189                 }
190         }
191 }
192