Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /RsiEditor/Logger.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (hide annotations) (download)
Sun Jul 26 02:18:54 2009 UTC (14 years, 8 months ago) by pex
File size: 3928 byte(s)


1 pex 1 using System;
2     using System.Collections.Generic;
3     using System.Text;
4     using System.IO;
5    
6     namespace RipSync.RsiEditor
7     {
8     /// <summary>
9     /// ログの出力レベル。
10     /// </summary>
11     public enum LogLevel
12     {
13     /// <summary>
14     /// 情報。
15     /// </summary>
16     Info,
17     /// <summary>
18     /// 報告。
19     /// </summary>
20     Notice,
21     /// <summary>
22     /// 警告。
23     /// </summary>
24     Warn,
25     /// <summary>
26     /// エラー。
27     /// </summary>
28     Error
29     }
30    
31     /// <summary>
32     /// ログを出力するクラス。
33     /// </summary>
34     public class Logger : IDisposable
35     {
36     static private Logger instance = null;
37    
38     /// <summary>
39     /// 唯一のインスタンスを取得する。
40     /// </summary>
41     /// <returns></returns>
42     static public Logger GetInstance()
43     {
44     if (instance == null)
45     {
46     instance = new Logger();
47     }
48     return instance;
49     }
50    
51     private TextWriter writer = null;
52     private Stream stream = null;
53     private string log = "";
54    
55     /// <summary>
56     /// デフォルトのログファイル名を取得する。
57     /// </summary>
58     /// <returns></returns>
59     static public string GetDefaultLogFileName()
60     {
61     string appdir = System.Windows.Forms.Application.StartupPath;
62     return Path.Combine(appdir, "RSIEditor.log");
63     }
64    
65     /// <summary>
66     /// ログファイル(log.txt)をオープンする。
67     /// </summary>
68     /// <returns></returns>
69     public bool Open()
70     {
71     return Open(GetDefaultLogFileName());
72     }
73    
74     /// <summary>
75     /// ログファイルをオープンする。
76     /// </summary>
77     /// <param name="_logfile"></param>
78     /// <returns></returns>
79     public bool Open(string _logfile)
80     {
81     Close();
82    
83     TextReader tr = null;
84     TextWriter tw = null;
85     Stream st = null;
86     try
87     {
88     st = new FileStream(_logfile, FileMode.OpenOrCreate, FileAccess.ReadWrite);
89     tw = new StreamWriter(st);
90     tr = new StreamReader(st);
91     log = tr.ReadToEnd();
92    
93    
94     }
95     catch
96     {
97     if (st != null)
98     {
99     st.Close();
100     }
101     return false;
102     }
103    
104     writer = tw;
105     stream = st;
106    
107     Write(LogLevel.Info, "Logger", "Logger starts. ---------------------------");
108    
109     return true;
110     }
111    
112     /// <summary>
113     /// ログファイルをクローズする。
114     /// </summary>
115     public void Close()
116     {
117     if (writer != null)
118     {
119     Write(LogLevel.Info, "Logger", "Logger closes. ---------------------------");
120    
121     stream.Close();
122     stream = null;
123     writer = null;
124     }
125     }
126    
127     /// <summary>
128     /// エラーログを出力する。
129     /// </summary>
130     /// <param name="_name"></param>
131     /// <param name="_e"></param>
132     public void ErrorWrite(string _name, Exception _e)
133     {
134     StringBuilder sb = new StringBuilder();
135     sb.AppendLine("エラーが発生しました。");
136     sb.AppendLine(String.Format("type {0}", _e.GetType().Name));
137     sb.AppendLine(String.Format("at {0} ({1})", _e.Source, _e.TargetSite));
138     sb.AppendLine(_e.Message);
139     sb.AppendLine(_e.StackTrace);
140    
141     Write(LogLevel.Error, _name, sb.ToString());
142     }
143    
144     /// <summary>
145     /// ログを出力する。
146     /// </summary>
147     /// <param name="_level">ログレベル</param>
148     /// <param name="_name">ログ出力者名</param>
149     /// <param name="_message">ログ</param>
150     public void Write(LogLevel _level, string _name, string _message)
151     {
152     if (writer == null) return;
153    
154     string type = "";
155     switch (_level)
156     {
157     case LogLevel.Info: type = "Info"; break;
158     case LogLevel.Notice: type = "Notice"; break;
159     case LogLevel.Warn: type = "Warn"; break;
160     case LogLevel.Error: type = "Error"; break;
161     }
162    
163     string w = String.Format("[{0}][{1}][{2}] {3} ", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), type, _name, _message);
164     log += "\r\n" + w;
165    
166     writer.WriteLine(w);
167     writer.Flush();
168     }
169    
170     /// <summary>
171     /// ログを取得する。
172     /// </summary>
173     public string Log
174     {
175     get
176     {
177     return log;
178     }
179     }
180    
181     #region IDisposable メンバ
182    
183     public void Dispose()
184     {
185     Close();
186     }
187    
188     #endregion
189     }
190     }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26