)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » Trace (Ia.Cl.Models)

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