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

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