شركة التطبيقات المتكاملة لتصميم النظم البرمجية الخاصة

Integrated Applications Programming Company

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.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:          /// <summary/>
  40:          public string Ip { get; set; }
  41:          /// <summary/>
  42:          public string Host { get; set; }
  43:          /// <summary/>
  44:          public string ServerVariables { get; set; }
  45:          /// <summary/>
  46:          public System.Guid Guid { get; set; }
  47:          /// <summary/>
  48:          public DateTime Created { get; set; }
  49:   
  50:          ////////////////////////////////////////////////////////////////////////////
  51:   
  52:          /// <summary>
  53:          ///
  54:          /// </summary>
  55:          public static bool Create(Trace newItem, out string result)
  56:          {
  57:              bool b;
  58:   
  59:              b = false;
  60:              result = "";
  61:   
  62:              using (var db = new global::Ia.Cl.Model.Ia())
  63:              {
  64:                  db.Traces.Add(newItem);
  65:                  db.SaveChanges();
  66:   
  67:                  b = true;
  68:              }
  69:   
  70:              return b;
  71:          }
  72:   
  73:          ////////////////////////////////////////////////////////////////////////////
  74:   
  75:          /// <summary>
  76:          ///
  77:          /// </summary>
  78:          public static void Inspect(System.Web.HttpRequest request)
  79:          {
  80:              Guid guid;
  81:   
  82:              // read trace guid from cookie value (with ""). If does not exists (or is invalid) create a new one (guid & cookie).
  83:              //if (!Guid.TryParse(Cookie.Read(traceCookieName), out guid))
  84:              //{
  85:              guid = Guid.NewGuid();
  86:              //   Cookie.Create(traceCookieName, guid.ToString());
  87:              //}
  88:   
  89:              guid = Guid.NewGuid();
  90:   
  91:              Insert(request.UserHostAddress, request.Url, guid, FormatServerVariables(request));
  92:          }
  93:   
  94:          ////////////////////////////////////////////////////////////////////////////
  95:   
  96:          /// <summary>
  97:          /// Initiates a trace by passing the API Trace function
  98:          /// </summary>
  99:          public static void Inspect(System.Web.HttpRequest request, Func<string, Uri, Guid, string, int> ApiTrace)
 100:          {
 101:              Guid guid;
 102:   
 103:              // read trace guid from cookie value (with ""). If does not exists (or is invalid) create a new one (guid & cookie).
 104:              //if (!Guid.TryParse(Cookie.Read(traceCookieName), out guid))
 105:              //{
 106:              guid = Guid.NewGuid();
 107:              //   Cookie.Create(traceCookieName, guid.ToString());
 108:              //}
 109:   
 110:              ApiTrace(request.UserHostAddress, request.Url, guid, FormatServerVariables(request));
 111:          }
 112:   
 113:          ////////////////////////////////////////////////////////////////////////////
 114:   
 115:          /// <summary>
 116:          ///
 117:          /// </summary>
 118:          public static bool Insert(string userHostAddress, Uri url, Guid guid, string serverVariables)
 119:          {
 120:              bool newItemCreated;
 121:              string result;
 122:              Trace newItem;
 123:   
 124:              newItem = new Trace();
 125:   
 126:              // insert new record
 127:              newItem.Ip = userHostAddress;
 128:              newItem.Host = global::Ia.Cl.Model.Default.BasicHost(url);
 129:              newItem.Guid = guid;
 130:              newItem.ServerVariables = serverVariables;
 131:              newItem.Created = DateTime.UtcNow.AddHours(3);
 132:   
 133:              newItemCreated = Trace.Create(newItem, out result);
 134:   
 135:              return newItemCreated;
 136:          }
 137:   
 138:          ////////////////////////////////////////////////////////////////////////////
 139:   
 140:          /// <summary>
 141:          /// Read traced records
 142:          /// </summary>
 143:          public static List<Trace> Read()
 144:          {
 145:              List<Trace> list;
 146:   
 147:              using (var db = new global::Ia.Cl.Model.Ia())
 148:              {
 149:                  list = (from q in db.Traces orderby q.Created descending select q).Take(100).ToList<Trace>();
 150:              }
 151:   
 152:              return list;
 153:          }
 154:   
 155:          ////////////////////////////////////////////////////////////////////////////
 156:   
 157:          /// <summary>
 158:          ///
 159:          /// </summary>
 160:          public static string FormatServerVariables(System.Web.HttpRequest request)
 161:          {
 162:              string serverVariables = null;
 163:   
 164:              try
 165:              {
 166:                  serverVariables = "";
 167:   
 168:                  foreach (string key in request.ServerVariables.AllKeys)
 169:                  {
 170:                      serverVariables += "[" + key + ": " + request.ServerVariables[key] + "]\r\n";
 171:                  }
 172:              }
 173:              catch (Exception)
 174:              {
 175:  #if DEBUG
 176:                  //line += "Error: " + ex.ToString();
 177:  #else
 178:                  //line += "Error: " + ex.Message;
 179:  #endif
 180:              }
 181:              finally { }
 182:   
 183:              return serverVariables;
 184:          }
 185:   
 186:          ////////////////////////////////////////////////////////////////////////////
 187:          ////////////////////////////////////////////////////////////////////////////
 188:      }
 189:  }