/[projects]/dao/DaoMqPump2/DaoCommon/Logfile.cs
ViewVC logotype

Annotation of /dao/DaoMqPump2/DaoCommon/Logfile.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2169 - (hide annotations) (download)
Fri May 16 21:10:02 2014 UTC (10 years ago) by torben
File size: 3011 byte(s)
WIP
1 torben 2169 using System;
2     using System.Collections.Generic;
3     using System.Text;
4     using System.Globalization;
5     using System.IO;
6    
7     namespace DaoCommon
8     {
9     public enum LogfileType
10     {
11     LogTransactions,
12     LogEvents,
13     LogDiscarded
14     }
15    
16    
17    
18     public class Logfile : IDisposable
19     {
20     private string name;
21     private String logDirectory;
22     private LinkedList<string> logEntries = new LinkedList<string>();
23    
24    
25     public Logfile(string logname, string logDirectory)
26     {
27     this.logDirectory = logDirectory;
28     this.name = logname;
29     }
30    
31     public void Dispose()// adasd
32     {
33     }
34    
35     public static string getNowString()
36     {
37     DateTime now = DateTime.Now;
38    
39     return now.ToString("s");
40     }
41    
42    
43     public string getLogFilename(LogfileType type)
44     {
45     return getLogFilename(type, logDirectory, name);
46     }
47    
48     public static string getLogFilename(LogfileType type, string logDirectory, string name)
49     {
50    
51     DateTime now = DateTime.Now;
52     string filename = logDirectory + "\\";
53    
54     //Find uge nr
55     DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
56     Calendar myCal = CultureInfo.InvariantCulture.Calendar;//System.Globalization
57     int week = myCal.GetWeekOfYear(now, dfi.CalendarWeekRule, dfi.FirstDayOfWeek);
58    
59     switch (type)
60     {
61     case LogfileType.LogEvents:
62     filename += "eventlog_";
63     break;
64    
65     case LogfileType.LogTransactions:
66     filename += "transactionlog_";
67     break;
68     case LogfileType.LogDiscarded:
69     filename += "discardedlog_";
70     break;
71     }
72    
73    
74     filename += name + "_" + now.Year.ToString("D4") + "_W" + week.ToString("D2") + ".log";
75    
76     return filename;
77     }
78    
79    
80     public void addSingleLogEntry(string msg)
81     {
82     msg = Logfile.getNowString() + " " + msg;
83     lock (logEntries)
84     {
85     logEntries.AddFirst(msg);
86    
87     if (logEntries.Count > 20)
88     {
89     logEntries.RemoveLast();
90     }
91     }
92    
93     string filename = getLogFilename(LogfileType.LogEvents);
94     using (StreamWriter eventlog = new StreamWriter(filename, true))
95     {
96     eventlog.WriteLine(msg);
97     }
98     }
99    
100     public string[] getLog()
101     {
102     lock (logEntries)
103     {
104     List<string> tmpEntries = new List<string>();
105     foreach (string s in logEntries)
106     {
107     tmpEntries.Add(s);
108     }
109     return tmpEntries.ToArray();
110     }
111     }
112    
113    
114     }
115     }

  ViewVC Help
Powered by ViewVC 1.1.20