56e556624bd82b06947274825dc70cdff50375b1
[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                 public event TraceContextEventHandler TraceFinished;
125 #endif
126                 internal void SaveData ()
127                 {
128                         if (data == null)
129                                 data = new TraceData ();
130                         SetRequestDetails ();
131                         if (_Context.Handler is Page)
132                                 data.AddControlTree ((Page) _Context.Handler, view_states, sizes);
133
134                         AddCookies ();
135                         AddHeaders ();
136                         AddServerVars ();
137                         HttpRuntime.TraceManager.AddTraceData (data);
138                         data_saved = true;
139                 }
140
141                 internal void SaveViewState (Control ctrl, object vs)
142                 {
143                         if (view_states == null)
144                                 view_states = new Hashtable ();
145
146                         view_states [ctrl] = vs;
147                 }
148
149                 internal void SaveSize (Control ctrl, int size)
150                 {
151                         if (sizes == null)
152                                 sizes = new Hashtable ();
153
154                         sizes [ctrl] = size;
155                 }
156
157                 internal void Render (HtmlTextWriter output)
158                 {
159                         if (!data_saved)
160                                 SaveData ();
161                         data.Render (output);
162                 }
163
164                 void SetRequestDetails ()
165                 {
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;
173                 }
174
175                 void AddCookies ()
176                 {
177                         foreach (string key in _Context.Request.Cookies.Keys)
178                                 data.AddCookie (key, _Context.Request.Cookies [key].Value);
179                 }
180
181                 void AddHeaders ()
182                 {
183                         foreach (string key in _Context.Request.Headers.Keys)
184                                 data.AddHeader (key, _Context.Request.Headers [key]);
185                 }
186
187                 void AddServerVars ()
188                 {
189                         foreach (string key in _Context.Request.ServerVariables)
190                                 data.AddServerVar (key, _Context.Request.ServerVariables [key]);
191                 }
192         }
193 }
194