1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4:
5: namespace Ia.Cl.Model
6: {
7: ////////////////////////////////////////////////////////////////////////////
8:
9: /// <summary publish="true">
10: /// Trace function to try to identifiy a user using IP addresses, cookies, and session states.
11: /// </summary>
12: /// <value>
13: /// Put Ia.Cl.Model.Trace.Inspect(this.Request); in Session_Start()
14: /// Or use a service reference
15: /// </value>
16: ///
17: /// <remarks>
18: /// Copyright � 2001-2015 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
19: ///
20: /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
21: /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
22: ///
23: /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
24: /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
25: ///
26: /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
27: ///
28: /// Copyright notice: This notice may not be removed or altered from any source distribution.
29: /// </remarks>
30: public partial class Trace
31: {
32: private const string traceCookieName = "traceCookie";
33:
34: /// <summary/>
35: public Trace() { }
36:
37: /// <summary/>
38: public int Id { get; set; }
39:
40: /// <summary/>
41: public string Ip { get; set; }
42:
43: /// <summary/>
44: public string Host { get; set; }
45:
46: /// <summary/>
47: public string ServerVariables { get; set; }
48:
49: /// <summary/>
50: public System.Guid Guid { get; set; }
51:
52: /// <summary/>
53: public DateTime Created { get; set; }
54:
55: ////////////////////////////////////////////////////////////////////////////
56:
57: /// <summary>
58: ///
59: /// </summary>
60: public static bool Create(Trace newItem, out string result)
61: {
62: bool b;
63:
64: b = false;
65: result = "";
66:
67: using (var db = new Ia.Cl.Model.IaDbContext())
68: {
69: //db.Traces.Add(newItem);
70: //db.SaveChanges();
71:
72: b = false; // true;
73: }
74:
75: return b;
76: }
77:
78: ////////////////////////////////////////////////////////////////////////////
79:
80: /// <summary>
81: ///
82: /// </summary>
83: public static void Inspect(System.Web.HttpRequest request)
84: {
85: Guid guid;
86:
87: // read trace guid from cookie value (with ""). If does not exists (or is invalid) create a new one (guid & cookie).
88: //if (!Guid.TryParse(Cookie.Read(traceCookieName), out guid))
89: //{
90: guid = Guid.NewGuid();
91: // Cookie.Create(traceCookieName, guid.ToString());
92: //}
93:
94: guid = Guid.NewGuid();
95:
96: Insert(request.UserHostAddress, request.Url, guid, FormatServerVariables(request));
97: }
98:
99: ////////////////////////////////////////////////////////////////////////////
100:
101: /// <summary>
102: /// Initiates a trace by passing the API Trace function
103: /// </summary>
104: public static void Inspect(System.Web.HttpRequest request, Func<string, Uri, Guid, string, int> ApiTrace)
105: {
106: Guid guid;
107:
108: // read trace guid from cookie value (with ""). If does not exists (or is invalid) create a new one (guid & cookie).
109: //if (!Guid.TryParse(Cookie.Read(traceCookieName), out guid))
110: //{
111: guid = Guid.NewGuid();
112: // Cookie.Create(traceCookieName, guid.ToString());
113: //}
114:
115: ApiTrace(request.UserHostAddress, request.Url, guid, FormatServerVariables(request));
116: }
117:
118: ////////////////////////////////////////////////////////////////////////////
119:
120: /// <summary>
121: ///
122: /// </summary>
123: public static bool Insert(string userHostAddress, Uri url, Guid guid, string serverVariables)
124: {
125: bool newItemCreated;
126: string result;
127: Trace newItem;
128:
129: newItem = new Trace();
130:
131: // insert new record
132: newItem.Ip = userHostAddress;
133: newItem.Host = Ia.Cl.Model.Default.BasicHost(url);
134: newItem.Guid = guid;
135: newItem.ServerVariables = serverVariables;
136: newItem.Created = DateTime.UtcNow.AddHours(3);
137:
138: newItemCreated = Trace.Create(newItem, out result);
139:
140: return newItemCreated;
141: }
142:
143: ////////////////////////////////////////////////////////////////////////////
144:
145: /// <summary>
146: /// Read traced records
147: /// </summary>
148: public static List<Trace> Read()
149: {
150: List<Trace> list;
151:
152: using (var db = new Ia.Cl.Model.IaDbContext())
153: {
154: list = null; // (from q in db.Traces orderby q.Created descending select q).Take(100).ToList<Trace>();
155: }
156:
157: return list;
158: }
159:
160: ////////////////////////////////////////////////////////////////////////////
161:
162: /// <summary>
163: ///
164: /// </summary>
165: public static string FormatServerVariables(System.Web.HttpRequest request)
166: {
167: string serverVariables = null;
168:
169: try
170: {
171: serverVariables = "";
172:
173: foreach (string key in request.ServerVariables.AllKeys)
174: {
175: serverVariables += "[" + key + ": " + request.ServerVariables[key] + "]\r\n";
176: }
177: }
178: catch (Exception)
179: {
180: #if DEBUG
181: //line += "Error: " + ex.ToString();
182: #else
183: //line += "Error: " + ex.Message;
184: #endif
185: }
186: finally { }
187:
188: return serverVariables;
189: }
190:
191: ////////////////////////////////////////////////////////////////////////////
192: ////////////////////////////////////////////////////////////////////////////
193: }
194: }