![]() |
Sylloge
A C# helper library
|
00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 00017 namespace Sylloge 00018 { 00019 // TODO: redo this 00020 namespace Log 00021 { 00022 public class File : System.IDisposable 00023 { 00024 #region Class Members 00025 00026 #region Private 00027 00028 private bool m_IsOpen { get; set; } 00029 private bool m_HasLock { get; set; } 00030 private System.Threading.Mutex m_Mtx { get; set; } 00031 private System.IO.StreamWriter m_Stream { get; set; } 00032 00033 #endregion 00034 00035 #region Public 00036 00040 public string LogName { get; protected set; } 00041 00042 #endregion 00043 00044 #endregion 00045 00046 #region Methods 00047 00051 public File() 00052 { 00053 this.m_IsOpen = false; 00054 this.m_HasLock = false; 00055 this.m_Stream = null; 00056 this.m_Mtx = new System.Threading.Mutex(); 00057 this.LogName = string.Empty; 00058 } 00059 00064 public File(string logName) 00065 { 00066 this.m_IsOpen = false; 00067 this.m_HasLock = false; 00068 this.m_Stream = null; 00069 this.m_Mtx = new System.Threading.Mutex(); 00070 this.Open(logName); 00071 } 00072 00076 ~File() 00077 { 00078 this.Dispose(); 00079 } 00080 00084 public void Close() 00085 { 00086 if (this.m_HasLock) { this.m_Mtx.ReleaseMutex(); } 00087 if (this.m_Stream != null) { 00088 this.m_Stream.Close(); 00089 this.m_Stream.Dispose(); 00090 this.m_Stream = null; 00091 } 00092 this.m_HasLock = false; 00093 } 00094 00098 public void Dispose() 00099 { 00100 this.Close(); 00101 this.m_Mtx.Close(); 00102 this.m_Mtx = null; 00103 this.m_Stream = null; 00104 } 00105 00110 public virtual void Log(string msg) 00111 { 00112 this.m_Mtx.WaitOne(); 00113 this.m_HasLock = true; 00114 try { 00115 string tmsg = System.DateTime.Now.ToString() + ": " + msg; 00116 if (this.m_Stream != null && this.m_IsOpen) { 00117 this.m_Stream.WriteLine(tmsg); 00118 this.m_Stream.Flush(); 00119 } 00120 System.Console.WriteLine(tmsg); 00121 } catch (System.Exception ex) { 00122 System.Console.WriteLine( 00123 ("There was an error trying to write to the log" + System.Environment.NewLine + 00124 "Error:" + System.Environment.NewLine + ex.Message + System.Environment.NewLine + System.Environment.NewLine + 00125 "Original message attempting to write:" + System.Environment.NewLine + msg) 00126 ); 00127 } 00128 this.m_HasLock = false; 00129 this.m_Mtx.ReleaseMutex(); 00130 } 00131 00137 public bool Open(string logName) 00138 { 00139 return this.Open(logName, true); 00140 } 00141 00148 public bool Open(string logName, bool append) 00149 { 00150 if (this.m_IsOpen) { this.Close(); } 00151 if (!System.IO.File.Exists(logName)) { 00152 try { 00153 System.IO.File.Create(logName).Close(); 00154 } catch (System.Exception ex) { 00155 System.Console.WriteLine("Error opening log: " + ex.ToString()); 00156 return false; 00157 } 00158 } 00159 try { 00160 this.m_Stream = new System.IO.StreamWriter(logName, append); 00161 this.m_IsOpen = true; 00162 } catch (System.Exception ex) { 00163 System.Console.WriteLine("Error opening log: " + ex.ToString()); 00164 } 00165 return this.m_IsOpen; 00166 } 00167 00168 #endregion 00169 } 00170 00171 public class Visual : Sylloge.Log.File 00172 { 00176 public Visual() : base() { } 00177 00182 public Visual(string logName) : base(logName) { } 00183 00189 public new System.Windows.Forms.DialogResult Log(string msg) 00190 { 00191 return this.Log(msg, string.Empty, System.Windows.Forms.MessageBoxButtons.OK); 00192 } 00193 00200 public System.Windows.Forms.DialogResult Log(string msg, string caption) 00201 { 00202 return this.Log(msg, caption, System.Windows.Forms.MessageBoxButtons.OK); 00203 } 00204 00212 public System.Windows.Forms.DialogResult Log(string msg, string caption, System.Windows.Forms.MessageBoxButtons buttons) 00213 { 00214 this.Log(msg); 00215 return System.Windows.Forms.MessageBox.Show(msg, caption, buttons); 00216 } 00217 } 00218 00219 public class Exception : Sylloge.Log.Visual 00220 { 00224 public Exception() : base() { } 00225 00230 public Exception(string logName) : base(logName) { } 00231 00238 public System.Windows.Forms.DialogResult Log(string msg, System.Exception ex) 00239 { 00240 return this.Log(msg, ex, "Error"); 00241 } 00242 00250 public System.Windows.Forms.DialogResult Log(string msg, System.Exception ex, string caption) 00251 { 00252 Sylloge.Forms.Exception exfrm = new Sylloge.Forms.Exception(); 00253 exfrm.Caption = caption; 00254 exfrm.DisplayMessage = msg; 00255 exfrm.Stack = ex.StackTrace; 00256 ((Sylloge.Log.File)this).Log((msg + System.Environment.NewLine + ex.ToString())); 00257 return exfrm.ShowDialog(); 00258 } 00259 } 00260 } 00261 }
1.7.4