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

Integrated Applications Programming Company

Skip Navigation LinksHome » Code Library » Log

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

Log file support class.

   1:  using System;
   2:  using System.Web;
   3:  using System.Xml;
   4:  using System.IO;
   5:  using System.Configuration;
   6:  using System.Text;
   7:   
   8:  namespace Ia.Cl.Model
   9:  {
  10:      /// <summary publish="true">
  11:      /// Log file support class.
  12:      /// </summary>
  13:      /// <remarks> 
  14:      /// Copyright � 2001-2015 Jasem Y. Al-Shamlan (info@ia.com.kw), Internet Applications - Kuwait. All Rights Reserved.
  15:      ///
  16:      /// 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
  17:      /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
  18:      ///
  19:      /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  20:      /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  21:      /// 
  22:      /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
  23:      /// 
  24:      /// Copyright notice: This notice may not be removed or altered from any source distribution.
  25:      /// </remarks>
  26:      /// </summary>
  27:      public class Log
  28:      {
  29:          public Log() { }
  30:   
  31:          ////////////////////////////////////////////////////////////////////////////
  32:   
  33:          /// <summary>
  34:          ///
  35:          /// </summary>
  36:          public static void Open()
  37:          {
  38:          }
  39:   
  40:          ////////////////////////////////////////////////////////////////////////////
  41:   
  42:          /// <summary>
  43:          ///
  44:          /// </summary>
  45:          public static void Append(string file_path, string first_line, string line)
  46:          {
  47:              // 
  48:              string path;
  49:              StreamWriter sw = null;
  50:   
  51:              path = global::Ia.Cl.Model.Default.AbsolutePath();
  52:   
  53:              path = path + file_path;
  54:   
  55:              try
  56:              {
  57:                  if (!System.IO.File.Exists(path))
  58:                  {
  59:                      using (sw = System.IO.File.CreateText(path)) sw.WriteLine(first_line);
  60:                  }
  61:   
  62:                  using (sw = System.IO.File.AppendText(path)) sw.WriteLine(line);
  63:              }
  64:              catch (Exception) { }
  65:          }
  66:   
  67:          ////////////////////////////////////////////////////////////////////////////
  68:   
  69:          /// <summary>
  70:          ///
  71:          /// </summary>
  72:          public static void Append(string file_path, string line)
  73:          {
  74:              // 
  75:              string path;
  76:              StreamWriter sw = null;
  77:   
  78:              path = global::Ia.Cl.Model.Default.AbsolutePath();
  79:   
  80:              path = path + file_path;
  81:   
  82:              try
  83:              {
  84:                  if (!System.IO.File.Exists(path))
  85:                  {
  86:                      using (sw = System.IO.File.CreateText(path)) sw.WriteLine(line);
  87:                  }
  88:                  else
  89:                  {
  90:                      using (sw = System.IO.File.AppendText(path)) sw.WriteLine(line);
  91:                  }
  92:              }
  93:              catch (Exception) { }
  94:          }
  95:   
  96:          ////////////////////////////////////////////////////////////////////////////
  97:   
  98:          /// <summary>
  99:          ///
 100:          /// </summary>
 101:          public static string Read(string file_path)
 102:          {
 103:              string path, sa;
 104:              StreamReader sr = null;
 105:              StringBuilder sb = new StringBuilder(110000);
 106:   
 107:              path = global::Ia.Cl.Model.Default.AbsolutePath();
 108:   
 109:              path = path + file_path;
 110:   
 111:              try
 112:              {
 113:                  if (System.IO.File.Exists(path))
 114:                  {
 115:                      using (sr = System.IO.File.OpenText(path))
 116:                      {
 117:                          while ((sa = sr.ReadLine()) != null) sb.Append(sa + "\n");
 118:                      }
 119:                  }
 120:              }
 121:              catch (Exception)
 122:              {
 123:              }
 124:   
 125:              return sb.ToString();
 126:          }
 127:   
 128:          ////////////////////////////////////////////////////////////////////////////
 129:   
 130:          /// <summary>
 131:          /// Log a standard logging entry into a special database table
 132:          /// </summary>
 133:          public long Log2(int TypeId, string user_id, string refe, long reference_log_id, int direction_id, int system_id, int process_id, int function_id, string detail, DateTime created)
 134:          {
 135:              long l;
 136:              string s;
 137:   
 138:              // See table ia_log and log.xml
 139:   
 140:              /*
 141:  CREATE TABLE [dbo].[ia_log]
 142:  (
 143:   [id]                    int    IDENTITY(1,1) CONSTRAINT [ia_log_id_pk] PRIMARY KEY,
 144:   [TypeId]                tinyint NULL,
 145:   [user_id]                uniqueidentifier NULL,
 146:   [ref]                    nvarchar(32) NULL,
 147:   [ia_log_id]            int NULL,
 148:   [direction_id]    tinyint NULL,
 149:   [system_id]            smallint NULL,
 150:   [process_id]            smallint NULL,
 151:   [function_id]            smallint NULL,
 152:   [detail]                ntext NULL,
 153:   [created]                smalldatetime NULL
 154:  )
 155:              */
 156:   
 157:              if (user_id == null) user_id = "NULL";
 158:              else user_id = "'" + user_id + "'";
 159:   
 160:              if (reference_log_id == 0) s = "NULL";
 161:              else s = reference_log_id.ToString();
 162:   
 163:              //sql = "INSERT INTO [ia_log] ([TypeId],[user_id],[ref],[ia_log_id],[direction_id],[system_id],[process_id],[function_id],[detail],[created]) VALUES (" + TypeId + "," + user_id + ",'" + refe + "'," + s + "," + direction_id + "," + system_id + "," + process_id + "," + function_id + ",'" + HttpUtility.HtmlEncode(detail) + "','" + SmallDateTime(created) + "');SELECT SCOPE_IDENTITY()";
 164:   
 165:              //Sql(sql);
 166:   
 167:              //s = Scalar(sql);
 168:   
 169:              l = long.Parse(s);
 170:   
 171:              return l;
 172:          }
 173:   
 174:          ////////////////////////////////////////////////////////////////////////////
 175:          ////////////////////////////////////////////////////////////////////////////
 176:      }
 177:  }