ホテル管理システム
ogi
yesterday 1a1c8e71fcd14858f595029f089b2d4a00202b32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
using HotelPms.Share.Util;
using System;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading;
 
namespace HotelPms.Share.IO
{
    public class OperationLog
    {
        /// ****************************** Description *******************************
        /// žƒVƒXƒeƒ€–¼Ì
        /// @”Ä—pƒNƒ‰ƒX
        /// žŠT—v
        /// @ŠÈ’POpeLog”­s
        /// ž—š—ð
        /// @2016/02/28  ¬–؁@Ÿ—´    V‹Kì¬
        /// ****************************** Declarations ******************************
 
        #region  ššššš@Declartions@ššššš
 
        public enum LogLevelType : int
        { 
            Normal = 0,
            Level1,
            Level2,
            Level3,
            Level4,
            Level5,
            Level6,
            /// <summary>
            /// ’ʏíSQLƒƒO
            /// </summary>
            Level7,
            /// <summary>
            /// ƒ^ƒCƒ}[‚̏„‰ñˆ—SQLƒƒO
            /// </summary>
            Level8,
            /// <summary>
            /// DB‚Ìnew MsSqlNetƒŒƒxƒ‹
            /// </summary>
            Level9,
        }
 
        private static string LogLevelFile = System.AppDomain.CurrentDomain.BaseDirectory + @"LogLevel.dat";
        private static OperationLog? m_Default;
        public static object SyncRoot = new object();
        private string rootPath = System.AppDomain.CurrentDomain.BaseDirectory + @"OpeLog" + Path.DirectorySeparatorChar;
        private long m_FileMaxSize = 512;  //’PˆÊFKB
        private int m_FileIndex = 1;
        private bool m_Enabled = true;
        public event MessageEventHandler? OutPut;
 
        #endregion
 
        #region  ššššš@Property@ššššš
 
        private LogLevelType m_Level = LogLevelType.Normal;
 
        public LogLevelType Level
        {
            get { return m_Level; }
            set { m_Level = value; }
        }
 
        public static OperationLog Instance
        {
            get 
            {
                if (m_Default == null) { m_Default = new OperationLog(System.AppDomain.CurrentDomain.BaseDirectory + @"OpeLog" + Path.DirectorySeparatorChar + Environment.MachineName + "_" + Environment.UserName + Path.DirectorySeparatorChar, 512); }
                return m_Default;
            }
        }
 
        public long FileMaxSize
        {
            set { m_FileMaxSize = value; }
            get { return m_FileMaxSize; }
        }
 
        /// <summary>
        /// Log‚ðŽæ‚é‚©‚Ç‚¤‚©
        /// </summary>
        public string RootPath
        {
            get { return rootPath; }
            set { rootPath = value; }
        }
 
        public bool Enabled
        {
            get { return m_Enabled; }
            set { m_Enabled = value; }
        }
 
        #endregion
 
        #region  ššššš@Class Event@ššššš
 
        /// <summary>
        /// ƒRƒ“ƒXƒgƒ‰ƒNƒ^
        /// </summary>
        /// <param name="currentType">‹@”\ID‘Š“–(’ʏíSystemLog‚ª’è‹`‚³‚ê‚Ä‚¢‚鉿–ʁAƒNƒ‰ƒX)</param>
        /// <param name="running">–¾Ž¦“I‚ɃƒOŽæ‚è‚ð‚·‚é‚©‚Ç‚¤‚©</param>
        /// <param name="rootPath"></param>
        /// <param name="maxSize">’PˆÊFKB</param>
        public OperationLog(string rootPath, long maxSize)
        {
            if (rootPath.Length > 0) { this.rootPath = rootPath; }
            this.m_FileMaxSize = maxSize;
            if (File.Exists(LogLevelFile)) { m_Level = (LogLevelType)CConvert.ToInt(File.ReadAllText(LogLevelFile, Encoding.UTF8)); }
            InitLogger();
        }
 
        #endregion
 
        #region  ššššš@Public Functions@ššššš        
 
        /// <summary>
        /// ƒVƒXƒeƒ€ŽžŠÔ‚©‚çdelayMonthŒŽ‘O‚̃tƒHƒ‹ƒ_‚ðíœ‚·‚é
        /// </summary>
        /// <param name="delayMonth"></param>
        public void ClearLog(int delayMonth)
        {
            try
            {
                string[] dirList = Directory.GetDirectories(rootPath);
 
                foreach (string curDir in dirList)
                {
                    if (curDir.Substring(curDir.Length - 6, 6).CompareTo(DateTime.Now.Date.AddMonths(-delayMonth).ToString("yyyyMM")) <= 0)
                    {
                        FileOperation.DeleteDir(curDir);
                    }
                }
            }
            catch
            {
            }
        }
 
        /// <summary>
        /// ƒVƒXƒeƒ€ŽžŠÔ‚©‚çdelayDay“ú‘O‚̃tƒHƒ‹ƒ_‚ƃtƒ@ƒCƒ‹‚ðíœ‚·‚é
        /// </summary>
        /// <param name="delayMonth"></param>
        public void ClearLogByDay(int delayDay)
        {
            try
            {
                string beginYM = DateTime.Now.Date.AddDays(-delayDay).ToString("yyyyMM");
                int beginD = CConvert.ToInt(DateTime.Now.Date.AddDays(-delayDay).ToString("dd"));
 
                string[] dirList = Directory.GetDirectories(rootPath);
 
                foreach (string curDir in dirList)
                {
                    if (curDir.Substring(curDir.Length - 6, 6).CompareTo(beginYM) < 0)
                    {
                        FileOperation.DeleteDir(curDir);
                    }
                    else if (curDir.Substring(curDir.Length - 6, 6).CompareTo(beginYM) == 0)
                    {
                        //“ú•t‚ðíœ
                        string[] fileList = Directory.GetFiles(curDir);
                        foreach (string file in fileList)
                        {
                            if (CConvert.ToInt(Path.GetFileNameWithoutExtension(file).Substring(0, 2)) < beginD)
                            {
                                FileOperation.Delete(file);
                            }
                        }
                    }
                }
            }
            catch
            {
            }
        }
 
        #endregion
 
        #region  ššššš@Private Functions@ššššš
 
        private void RaiseOutputEvent(string msg)
        {
            if (OutPut != null) { OutPut(this, new MessageEventArgs(msg)); }
        }
 
        private string GetFileName()
        {
            if (m_FileMaxSize == 0) { return DateTime.Now.Day.ToString("00", NumberFormatInfo.CurrentInfo); }
 
            string dir = rootPath + DateTime.Now.ToString("yyyyMM");
            string fileBase = DateTime.Now.Day.ToString("00", NumberFormatInfo.CurrentInfo) + "_{0}";
 
            while (true)
            {
                string fileName = string.Format(fileBase, m_FileIndex);
                string fileFullPath = dir + Path.DirectorySeparatorChar + fileName + ".log";
                if (!File.Exists(fileFullPath))
                {
                    File.WriteAllText(fileFullPath, "ˆ—ŽžŠÔ,“à—e" + Environment.NewLine, Encoding.UTF8);
                    return fileName;
                }
 
                FileInfo fileInfo = new FileInfo(fileFullPath);
                if (fileInfo.Length < (m_FileMaxSize * 1024)) { return fileName; }
                RaiseOutputEvent("GetFileNameF" + fileFullPath);
                m_FileIndex++;
            }
        }
 
        /// <summary>
        /// logger‰Šú‰»
        /// </summary>
        private void InitLogger()
        {
            try
            {
                string dir = rootPath + DateTime.Now.ToString("yyyyMM");
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                    m_FileIndex = 1;
                }
                string filePath = dir + Path.DirectorySeparatorChar + GetFileName() + ".log";
                if (!File.Exists(filePath))
                {
                    File.WriteAllText(filePath, "ˆ—ŽžŠÔ,“à—e" + Environment.NewLine, Encoding.UTF8);
                }
                //this.projectName = Assembly.GetCallingAssembly().ManifestModule.Name;
            }
            catch (Exception ex)
            {
                RaiseOutputEvent("InitLoggerF" + ex.Message);
            }
        }
 
        public void WriteLog(string message)
        {
            WriteLog(message, LogLevelType.Normal);
        }
 
        public void WriteLog(string message, LogLevelType level)
        {
            if (!m_Enabled) { return; }
            if ((int)m_Level < (int)level) { return; }
 
            lock (SyncRoot)
            {
                InitLogger();   //ŒŽ•Ï‚í‚Á‚½Žž‚É
 
                try
                {
                    string filePath = rootPath + DateTime.Now.ToString("yyyyMM") + Path.DirectorySeparatorChar + GetFileName() + ".log";
                    using (StreamWriter sw = new StreamWriter(filePath, true, Encoding.UTF8))
                    {
                        sw.WriteLine(string.Format("y{0}z{1}", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss fff"), message));
                        sw.Close();
                    }
                }
                catch (Exception ex)
                {
                    RaiseOutputEvent("WriteLogF" + ex.Message);
                }
            }
        }
 
        public static void Write(string msg)
        {
            Write(msg, LogLevelType.Normal);
        }
 
        public static void Write(string msg, LogLevelType level)
        {
            try
            {
                //”ñ“¯Šú•ûŽ®
                ParameterizedThreadStart theradStart = new ParameterizedThreadStart(delegate(object data) 
                {
                    LogInfo item = data as LogInfo;
                    OperationLog.Instance.WriteLog(item.Message, item.Level);
                });
                Thread tr = new Thread(theradStart);
                tr.Start(new LogInfo(msg, level));
            }
            catch
            {
                return;
            }
        }
 
        public static void Clear(int delayMonth)
        {
            try
            {
                //”ñ“¯Šú•ûŽ®
                ParameterizedThreadStart theradStart = new ParameterizedThreadStart(delegate(object data)
                {
                    OperationLog.Instance.ClearLog(int.Parse(data.ToString()));
                });
                Thread tr = new Thread(theradStart);
                tr.Start(delayMonth);
            }
            catch
            {
                return;
            }
        }
 
 
        #endregion
    }
}