f4ad22af99deb9c97c2e40c1b2783d1432abfdc7
[mono.git] / mcs / class / System.Web / System.Web / TraceContext.cs
1 // \r
2 // System.Web.TraceContext\r
3 //\r
4 // Author:\r
5 //   Patrik Torstensson (Patrik.Torstensson@labs2.com)\r
6 //   Jackson Harper (jackson@ximian.com)\r
7 //\r
8 // (C) 2002 2003, Patrik Torstensson\r
9 // (C) 2003 Novell, Inc (http://www.novell.com) \r
10 //\r
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 \r
33 using System;\r
34 using System.Data;\r
35 using System.Web.UI;\r
36 using System.Web.UI.WebControls;\r
37 \r
38 namespace System.Web {\r
39    public sealed class TraceContext {\r
40       private HttpContext _Context;\r
41       private bool _Enabled;\r
42       private TraceMode _Mode;\r
43       private TraceData data;\r
44       private bool data_saved;\r
45       private bool _haveTrace;
46            \r
47       public TraceContext(HttpContext Context) {\r
48          _Context = Context;\r
49          _Enabled = false;\r
50       }\r
51
52
53         internal bool HaveTrace {
54                 get {
55                         return _haveTrace;
56                 }
57         }
58 \r
59       public bool IsEnabled {\r
60          get {\r
61             if (!_haveTrace)
62                 return HttpRuntime.TraceManager.Enabled;
63             return _Enabled;\r
64          }\r
65 \r
66          set {\r
67                  if (value && data == null)\r
68                          data = new TraceData ();\r
69              _haveTrace = true;
70             _Enabled = value;\r
71          }\r
72       }\r
73 \r
74       public TraceMode TraceMode {\r
75          get {\r
76             return _Mode;\r
77          }\r
78 \r
79          set {\r
80             _Mode = value;\r
81          }\r
82       }\r
83 \r
84       public void Warn(string msg) {\r
85          Write(String.Empty, msg, null, true);\r
86       }\r
87 \r
88       public void Warn(string category, string msg) {\r
89          Write(category, msg, null, true);\r
90       }\r
91 \r
92       public void Warn(string category, string msg, Exception error) {\r
93          Write(category, msg, error, true);\r
94       }\r
95 \r
96       public void Write(string msg) {\r
97          Write(String.Empty, msg, null, true);\r
98       }\r
99 \r
100       public void Write(string category, string msg) {\r
101          Write(category, msg, null, false);\r
102       }\r
103 \r
104       public void Write(string category, string msg, Exception error) {\r
105          Write(category, msg, error, false);\r
106       }\r
107 \r
108       private void Write(string category, string msg, Exception error, bool Warning) {\r
109               if (!IsEnabled)\r
110                       return;\r
111               if (data == null)\r
112                       data = new TraceData ();\r
113               data.Write (category, msg, error, Warning);\r
114       }\r
115 \r
116            internal void SaveData ()\r
117            {\r
118                    if (data == null)\r
119                            data = new TraceData ();\r
120                    SetRequestDetails ();\r
121                    data.AddControlTree ((Page) _Context.Handler);\r
122                    AddCookies ();\r
123                    AddHeaders ();\r
124                    AddServerVars ();\r
125                    HttpRuntime.TraceManager.AddTraceData (data);\r
126                    data_saved = true;\r
127            }\r
128            \r
129            internal void Render (HtmlTextWriter output)\r
130            {\r
131                    if (!data_saved)\r
132                            SaveData ();\r
133                    data.Render (output);\r
134            }\r
135 \r
136            private void SetRequestDetails ()\r
137            {\r
138                    data.RequestPath = _Context.Request.FilePath;\r
139                    data.SessionID = (_Context.Session != null ? _Context.Session.SessionID : String.Empty);\r
140                    data.RequestType = _Context.Request.RequestType;\r
141                    data.RequestTime = _Context.Timestamp;\r
142                    data.StatusCode = _Context.Response.StatusCode;\r
143                    data.RequestEncoding = _Context.Request.ContentEncoding;\r
144                    data.ResponseEncoding = _Context.Response.ContentEncoding;\r
145            }\r
146 \r
147            private void AddCookies ()\r
148            {\r
149                    foreach (string key in _Context.Request.Cookies.Keys)\r
150                            data.AddCookie (key, _Context.Request.Cookies [key].Value);\r
151            }\r
152            \r
153            private void AddHeaders ()\r
154            {\r
155                    foreach (string key in _Context.Request.Headers.Keys)\r
156                            data.AddHeader (key, _Context.Request.Headers [key]);\r
157            }\r
158 \r
159            private void AddServerVars ()\r
160            {\r
161                    foreach (string key in _Context.Request.ServerVariables)\r
162                            data.AddServerVar (key, _Context.Request.ServerVariables [key]);\r
163            }\r
164    }\r
165 }\r