/[EmuXPortal]/trunk/EmuXPortal/Logging/logger.cs
ViewVC logotype

Contents of /trunk/EmuXPortal/Logging/logger.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 112 - (show annotations) (download)
Thu Aug 23 04:13:19 2012 UTC (8 years, 1 month ago) by william
File size: 2341 byte(s)
OPTIMIZE LOGGING

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.IO;
6 using System.Windows.Forms;
7
8 namespace EmuXPortal.Logging
9 {
10 public interface ILog
11 {
12 void Write(string format, params object[] args);
13 //void Write(Stream s, string format, params object[] args);
14 void WriteLine(string format, params object[] args);
15 //void WriteLine(Stream s, string format, params object[] args);
16 }
17 public class logger : ILog
18 {
19 static string APP_PATH = Application.StartupPath;
20 static string log_file = "EmuXPortal.log";
21 static string log_file_path = string.Format(@"{0}\{1}", APP_PATH, log_file);
22 static FileMode mode = FileMode.OpenOrCreate;
23 static FileAccess access = FileAccess.ReadWrite;
24 static FileStream fs;
25 public logger() { }
26
27 public void Write(string format, params object[] args)
28 {
29 StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
30 sw.AutoFlush = true;
31 sw.Write(string.Format("{0}: {1}", DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss tt"), string.Format(format, args)));
32 Console.WriteLine(string.Format("{0}: {1}", DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss tt"), string.Format(format, args)));
33 format = null;
34 args = null;
35 sw = null;
36 }
37
38 public void WriteLine( string format, params object[] args)
39 {
40 StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
41 sw.AutoFlush = true;
42 sw.WriteLine(string.Format("{0}: {1}", DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss tt"), string.Format(format, args)));
43 Console.WriteLine(string.Format("{0}: {1}", DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss tt"), string.Format(format, args)));
44 format = null;
45 args = null;
46 sw = null;
47 }
48
49 public void FlushLog() { fs.Flush(); }
50 public void OpenLog()
51 {
52 if (new FileInfo(log_file_path).Exists) { mode = FileMode.Append; access = FileAccess.Write; }
53 fs = new FileStream(log_file_path, mode, access, FileShare.ReadWrite);
54 }
55 public void CloseLog() { FlushLog(); fs.Close(); }
56 }
57 }

  ViewVC Help
Powered by ViewVC 1.1.22