Professional Applications Programmers/Consultants برمجة واستشارات تطبيقات الإنترنت
Skip Navigation LinksHome » Code Library » Trace

Public general use code classes and xml files that we've compiled and used over the years:

Trace function to try to identifiy a user using IP addresses, cookies, and session states.

   1:  using System;
   2:  using System.Web;
   3:  using System.Xml;
   4:  using System.Xml.Linq;
   5:  using System.Linq;
   6:  using System.Text.RegularExpressions;
   7:  using System.Collections;
   8:  using System.Collections.Generic;
   9:   
  10:  namespace Ia.Cl.Model
  11:  {
  12:      ////////////////////////////////////////////////////////////////////////////
  13:   
  14:      /// <summary publish="true">
  15:      /// Trace function to try to identifiy a user using IP addresses, cookies, and session states.
  16:      /// </summary>
  17:      /// <value>
  18:      /// Put Ia.Cl.Model.Trace.Inspect(this.Request); in Session_Start()
  19:      /// Or use a service reference
  20:      /// </value>
  21:      /// 
  22:      /// <remarks> 
  23:      /// Copyright � 2001-2015 Jasem Y. Al-Shamlan (info@ia.com.kw), Internet Applications - Kuwait. All Rights Reserved.
  24:      ///
  25:      /// 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
  26:      /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  27:      ///
  28:      /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  29:      /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  30:      /// 
  31:      /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
  32:      /// 
  33:      /// Copyright notice: This notice may not be removed or altered from any source distribution.
  34:      /// </remarks> 
  35:      public partial class Trace
  36:      {
  37:          private const string traceCookieName = "traceCookie";
  38:   
  39:          /// <summary/>
  40:          public Trace() { }
  41:   
  42:          /// <summary/>
  43:          public int Id { get; set; }
  44:          /// <summary/>
  45:          public string Ip { get; set; }
  46:          /// <summary/>
  47:          public string Host { get; set; }
  48:          /// <summary/>
  49:          public string ServerVariables { get; set; }
  50:          /// <summary/>
  51:          public System.Guid Guid { get; set; }
  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 global::Ia.Cl.Model.Ia())
  68:              {
  69:                  db.Traces.Add(newItem);
  70:                  db.SaveChanges();
  71:   
  72:                  b = 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 = global::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 global::Ia.Cl.Model.Ia())
 153:              {
 154:                  list = (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:  }