Same thing as in HEAD.
[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 = false;
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             return _Enabled;\r
62          }\r
63 \r
64          set {\r
65                  if (value && data == null)\r
66                          data = new TraceData ();\r
67              _haveTrace = true;
68             _Enabled = value;\r
69          }\r
70       }\r
71 \r
72       public TraceMode TraceMode {\r
73          get {\r
74             return _Mode;\r
75          }\r
76 \r
77          set {\r
78             _Mode = value;\r
79          }\r
80       }\r
81 \r
82       public void Warn(string msg) {\r
83          Write(String.Empty, msg, null, true);\r
84       }\r
85 \r
86       public void Warn(string category, string msg) {\r
87          Write(category, msg, null, true);\r
88       }\r
89 \r
90       public void Warn(string category, string msg, Exception error) {\r
91          Write(category, msg, error, true);\r
92       }\r
93 \r
94       public void Write(string msg) {\r
95          Write(String.Empty, msg, null, true);\r
96       }\r
97 \r
98       public void Write(string category, string msg) {\r
99          Write(category, msg, null, false);\r
100       }\r
101 \r
102       public void Write(string category, string msg, Exception error) {\r
103          Write(category, msg, error, false);\r
104       }\r
105 \r
106       private void Write(string category, string msg, Exception error, bool Warning) {\r
107               if (!_Enabled)\r
108                       return;\r
109               if (data == null)\r
110                       data = new TraceData ();\r
111               data.Write (category, msg, error, Warning);\r
112       }\r
113 \r
114            internal void SaveData ()\r
115            {\r
116                    if (data == null)\r
117                            data = new TraceData ();\r
118                    SetRequestDetails ();\r
119                    data.AddControlTree ((Page) _Context.Handler);\r
120                    AddCookies ();\r
121                    AddHeaders ();\r
122                    AddServerVars ();\r
123                    HttpRuntime.TraceManager.AddTraceData (data);\r
124                    data_saved = true;\r
125            }\r
126            \r
127            internal void Render (HtmlTextWriter output)\r
128            {\r
129                    if (!data_saved)\r
130                            SaveData ();\r
131                    data.Render (output);\r
132            }\r
133 \r
134            private void SetRequestDetails ()\r
135            {\r
136                    data.RequestPath = _Context.Request.FilePath;\r
137                    data.SessionID = (_Context.Session != null ? _Context.Session.SessionID : String.Empty);\r
138                    data.RequestType = _Context.Request.RequestType;\r
139                    data.RequestTime = _Context.Timestamp;\r
140                    data.StatusCode = _Context.Response.StatusCode;\r
141                    data.RequestEncoding = _Context.Request.ContentEncoding;\r
142                    data.ResponseEncoding = _Context.Response.ContentEncoding;\r
143            }\r
144 \r
145            private void AddCookies ()\r
146            {\r
147                    foreach (string key in _Context.Request.Cookies.Keys)\r
148                            data.AddCookie (key, _Context.Request.Cookies [key].Value);\r
149            }\r
150            \r
151            private void AddHeaders ()\r
152            {\r
153                    foreach (string key in _Context.Request.Headers.Keys)\r
154                            data.AddHeader (key, _Context.Request.Headers [key]);\r
155            }\r
156 \r
157            private void AddServerVars ()\r
158            {\r
159                    foreach (string key in _Context.Request.ServerVariables)\r
160                            data.AddServerVar (key, _Context.Request.ServerVariables [key]);\r
161            }\r
162    }\r
163 }\r