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: public class Log
24: {
25: ////////////////////////////////////////////////////////////////////////////
26:
27: /// <summary>
28: ///
29: /// </summary>
30: public Log() { }
31:
32: ////////////////////////////////////////////////////////////////////////////
33:
34: /// <summary>
35: ///
36: /// </summary>
37: public static void Open()
38: {
39: }
40:
41: ////////////////////////////////////////////////////////////////////////////
42:
43: /// <summary>
44: ///
45: /// </summary>
46: public static void Append(string file_path, string first_line, string line)
47: {
48: //
49: string path;
50: StreamWriter sw = null;
51:
52: path = Ia.Cl.Model.Default.AbsolutePath();
53:
54: path = path + file_path;
55:
56: try
57: {
58: if (!System.IO.File.Exists(path))
59: {
60: using (sw = System.IO.File.CreateText(path)) sw.WriteLine(first_line);
61: }
62:
63: using (sw = System.IO.File.AppendText(path)) sw.WriteLine(line);
64: }
65: catch (Exception) { }
66: }
67:
68: ////////////////////////////////////////////////////////////////////////////
69:
70: /// <summary>
71: ///
72: /// </summary>
73: public static void Append(string file_path, string line)
74: {
75: //
76: string path;
77: StreamWriter sw = null;
78:
79: path = Ia.Cl.Model.Default.AbsolutePath();
80:
81: path = path + file_path;
82:
83: try
84: {
85: if (!System.IO.File.Exists(path))
86: {
87: using (sw = System.IO.File.CreateText(path)) sw.WriteLine(line);
88: }
89: else
90: {
91: using (sw = System.IO.File.AppendText(path)) sw.WriteLine(line);
92: }
93: }
94: catch (Exception) { }
95: }
96:
97: ////////////////////////////////////////////////////////////////////////////
98:
99: /// <summary>
100: ///
101: /// </summary>
102: public static string Read(string file_path)
103: {
104: string path, sa;
105: StreamReader sr = null;
106: StringBuilder sb = new StringBuilder(110000);
107:
108: path = Ia.Cl.Model.Default.AbsolutePath();
109:
110: path = path + file_path;
111:
112: try
113: {
114: if (System.IO.File.Exists(path))
115: {
116: using (sr = System.IO.File.OpenText(path))
117: {
118: while ((sa = sr.ReadLine()) != null) sb.Append(sa + "\n");
119: }
120: }
121: }
122: catch (Exception)
123: {
124: }
125:
126: return sb.ToString();
127: }
128:
129: ////////////////////////////////////////////////////////////////////////////
130:
131: /// <summary>
132: /// Log a standard logging entry into a special database table
133: /// </summary>
134: 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)
135: {
136: long l;
137: string s;
138:
139: // See table ia_log and log.xml
140:
141: /*
142: CREATE TABLE [dbo].[ia_log]
143: (
144: [id] int IDENTITY(1,1) CONSTRAINT [ia_log_id_pk] PRIMARY KEY,
145: [TypeId] tinyint NULL,
146: [user_id] uniqueidentifier NULL,
147: [ref] nvarchar(32) NULL,
148: [ia_log_id] int NULL,
149: [direction_id] tinyint NULL,
150: [system_id] smallint NULL,
151: [process_id] smallint NULL,
152: [function_id] smallint NULL,
153: [detail] ntext NULL,
154: [created] smalldatetime NULL
155: )
156: */
157:
158: if (user_id == null) user_id = "NULL";
159: else user_id = "'" + user_id + "'";
160:
161: if (reference_log_id == 0) s = "NULL";
162: else s = reference_log_id.ToString();
163:
164: //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()";
165:
166: //Sql(sql);
167:
168: //s = Scalar(sql);
169:
170: l = long.Parse(s);
171:
172: return l;
173: }
174:
175: ////////////////////////////////////////////////////////////////////////////
176: ////////////////////////////////////////////////////////////////////////////
177: }
178: }