Merge lp:~dangarner/xibo/client-164 into lp:xibo/1.6

Proposed by Dan Garner
Status: Merged
Merged at revision: 349
Proposed branch: lp:~dangarner/xibo/client-164
Merge into: lp:xibo/1.6
Diff against target: 326 lines (+110/-137)
1 file modified
client/dotNET/XiboTraceListener.cs (+110/-137)
To merge this branch: bzr merge lp:~dangarner/xibo/client-164
Reviewer Review Type Date Requested Status
Xibo Maintainters Pending
Review via email: mp+237071@code.launchpad.net
To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'client/dotNET/XiboTraceListener.cs'
2--- client/dotNET/XiboTraceListener.cs 2013-09-28 14:00:01 +0000
3+++ client/dotNET/XiboTraceListener.cs 2014-10-03 14:12:11 +0000
4@@ -26,6 +26,8 @@
5 using System.Collections.ObjectModel;
6 using System.Windows.Forms;
7 using System.Security;
8+using System.Threading;
9+using XiboClient.Properties;
10
11 /// 17/02/12 Dan Changed to always Log audit if no category is given
12
13@@ -33,11 +35,10 @@
14 {
15 class XiboTraceListener : TraceListener
16 {
17+ private static readonly Object _locker = new Object();
18+
19 private Collection<TraceMessage> _traceMessages;
20- private String _logPath;
21- private Boolean _xmdsProcessing;
22- private xmds.xmds _xmds;
23- private String _lastSubmit;
24+ private string _logPath;
25 private HardwareKey _hardwareKey;
26
27 public XiboTraceListener()
28@@ -47,60 +48,88 @@
29
30 public XiboTraceListener(string r_strListenerName)
31 : base(r_strListenerName)
32- {
33- InitializeListener() ;
34- }
35+ {
36+ InitializeListener();
37+ }
38
39 private void InitializeListener()
40 {
41 // Make a new collection of TraceMessages
42 _traceMessages = new Collection<TraceMessage>();
43- _logPath = Application.UserAppDataPath + @"/" + Properties.Settings.Default.logLocation;
44-
45- _xmdsProcessing = false;
46- _xmds = new xmds.xmds();
47-
48- // Register a listener for the XMDS stats
49- _xmds.SubmitLogCompleted += new XiboClient.xmds.SubmitLogCompletedEventHandler(_xmds_SubmitLogCompleted);
50+ _logPath = Settings.Default.LibraryPath + @"\" + Settings.Default.logLocation;
51
52 // Get the key for this display
53 _hardwareKey = new HardwareKey();
54 }
55
56+ /// <summary>
57+ /// Get the LogType from a string
58+ /// </summary>
59+ /// <param name="category"></param>
60+ /// <returns></returns>
61+ private static LogType GetLogTypeFromString(string category)
62+ {
63+ LogType logType;
64+
65+ if (category == LogType.Audit.ToString())
66+ logType = LogType.Audit;
67+ else if (category == LogType.Error.ToString())
68+ logType = LogType.Error;
69+ else if (category == LogType.Info.ToString())
70+ logType = LogType.Info;
71+ else
72+ logType = LogType.Audit;
73+
74+ return logType;
75+ }
76+
77 private void AddToCollection(string message, string category)
78 {
79- TraceMessage traceMessage;
80-
81- traceMessage.category = category;
82- traceMessage.dateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
83- traceMessage.message = message;
84-
85- _traceMessages.Add(traceMessage);
86+ if (Settings.Default.LogLevel == "off")
87+ return;
88+
89+ LogType logtype = GetLogTypeFromString(category);
90+
91+ // Determine if we should log this or not.
92+ if (Settings.Default.LogLevel == "error" && logtype != LogType.Error)
93+ return;
94+
95+ if (Settings.Default.LogLevel == "info" && (logtype != LogType.Error && logtype != LogType.Info))
96+ return;
97+
98+ _traceMessages.Add(new TraceMessage
99+ {
100+ category = category,
101+ dateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
102+ message = message
103+ });
104+
105+ // Flush if we have build up a backlog.
106+ if (_traceMessages.Count > 25)
107+ Flush();
108 }
109
110 private void FlushToFile()
111 {
112- if (_traceMessages.Count < 1) return;
113+ if (_traceMessages.Count < 1)
114+ return;
115
116 try
117 {
118 // Open the Text Writer
119- StreamWriter tw = new StreamWriter(File.Open(_logPath, FileMode.Append, FileAccess.Write, FileShare.Read), Encoding.UTF8);
120-
121- String theMessage;
122-
123- foreach (TraceMessage message in _traceMessages)
124+ using (StreamWriter tw = new StreamWriter(File.Open(string.Format("{0}_{1}", _logPath, DateTime.Now.ToFileTimeUtc().ToString()), FileMode.Append, FileAccess.Write, FileShare.Read), Encoding.UTF8))
125 {
126- String traceMsg = message.message.ToString();
127-
128- theMessage = String.Format("<trace date=\"{0}\" category=\"{1}\">{2}</trace>", message.dateTime, message.category, traceMsg);
129- tw.WriteLine(theMessage);
130+ string theMessage;
131+
132+ foreach (TraceMessage message in _traceMessages)
133+ {
134+ string traceMsg = message.message.ToString();
135+
136+ theMessage = string.Format("<trace date=\"{0}\" category=\"{1}\">{2}</trace>", message.dateTime, message.category, traceMsg);
137+ tw.WriteLine(theMessage);
138+ }
139 }
140
141- // Close the tw.
142- tw.Close();
143- tw.Dispose();
144-
145 // Remove the messages we have just added
146 _traceMessages.Clear();
147 }
148@@ -117,84 +146,43 @@
149 /// <summary>
150 /// Flush the log to XMDS
151 /// </summary>
152- private void FlushToXmds()
153- {
154- String log;
155-
156- log = "<log>";
157-
158- // Load the Stats collection into a string
159- try
160- {
161- foreach (TraceMessage traceMessage in _traceMessages)
162- {
163- String traceMsg = traceMessage.message.ToString();
164-
165- if (!traceMsg.Contains("<message>"))
166- traceMsg = SecurityElement.Escape(traceMsg);
167-
168- log += String.Format("<trace date=\"{0}\" category=\"{1}\">{2}</trace>", traceMessage.dateTime, traceMessage.category, traceMsg);
169- }
170- }
171- catch (Exception ex)
172- {
173- System.Diagnostics.Trace.WriteLine(new LogMessage("FlushToXmds", String.Format("Error converting stat to a string {0}", ex.Message)), LogType.Error.ToString());
174- }
175-
176- log += "</log>";
177-
178- // Store the stats as the last sent (so we have a record if it fails)
179- _lastSubmit = log;
180-
181- // Clear the stats collection
182- _traceMessages.Clear();
183-
184- // Submit the string to XMDS
185- _xmdsProcessing = true;
186-
187- _xmds.SubmitLogAsync(Properties.Settings.Default.Version, Properties.Settings.Default.ServerKey, _hardwareKey.Key, log);
188- }
189-
190- /// <summary>
191- /// Capture the XMDS call and see if it went well
192- /// </summary>
193- /// <param name="sender"></param>
194- /// <param name="e"></param>
195- void _xmds_SubmitLogCompleted(object sender, XiboClient.xmds.SubmitLogCompletedEventArgs e)
196- {
197- _xmdsProcessing = false;
198-
199- // Test if we succeeded or not
200- if (e.Error != null)
201- {
202- // We had an error, log it.
203- System.Diagnostics.Trace.WriteLine(new LogMessage("_xmds_SubmitLogCompleted", String.Format("Error during Submit to XMDS {0}", e.Error.Message)), LogType.Error.ToString());
204-
205- // Dump the stats to a file instead
206- if (_lastSubmit != "")
207- {
208- try
209+ public void ProcessQueueToXmds()
210+ {
211+ lock (_locker)
212+ {
213+ // Get a list of all the log files waiting to be sent to XMDS.
214+ string[] logFiles = Directory.GetFiles(Settings.Default.LibraryPath, "*" + Settings.Default.logLocation + "*");
215+
216+ foreach (string fileName in logFiles)
217+ {
218+ // If we have some, create an XMDS object
219+ using (xmds.xmds logtoXmds = new xmds.xmds())
220 {
221- // Open the Text Writer
222- StreamWriter tw = new StreamWriter(File.Open(_logPath, FileMode.Append, FileAccess.Write, FileShare.Read), Encoding.UTF8);
223+ logtoXmds.Url = Settings.Default.XiboClient_xmds_xmds;
224+
225+ // construct the log message
226+ StringBuilder builder = new StringBuilder();
227+ builder.Append("<log>");
228+
229+ foreach (string entry in File.ReadAllLines(fileName))
230+ builder.Append(entry);
231+
232+ builder.Append("</log>");
233
234 try
235 {
236- tw.Write(_lastSubmit);
237+ logtoXmds.SubmitLog(Settings.Default.Version, Settings.Default.ServerKey, _hardwareKey.Key, builder.ToString());
238+
239+ // Delete the file we are on
240+ File.Delete(fileName);
241 }
242- catch {}
243- finally
244+ catch (Exception e)
245 {
246- tw.Close();
247- tw.Dispose();
248+ Trace.WriteLine(new LogMessage("FlushToXmds", string.Format("Exception when submitting to XMDS: {0}", e.Message)), LogType.Error.ToString());
249 }
250 }
251- catch {}
252 }
253 }
254-
255- // Clear the last sumbit
256- _lastSubmit = "";
257 }
258
259 public override void Write(string message)
260@@ -253,48 +241,33 @@
261 AddToCollection(message, detailMessage);
262 }
263
264+ /// <summary>
265+ /// Close the Trace Listener
266+ /// </summary>
267 public override void Close()
268 {
269 // Determine if there is anything to flush
270- if (_traceMessages.Count < 1) return;
271-
272- // As we are closing if XMDS is already busy just log to file.
273- if (_xmdsProcessing)
274- {
275- FlushToFile();
276- }
277- else
278- {
279- int threshold = ((int)Properties.Settings.Default.collectInterval * 5);
280-
281- // Determine where we want to log.
282- if (Properties.Settings.Default.XmdsLastConnection.AddSeconds(threshold) < DateTime.Now)
283- {
284- FlushToFile();
285- }
286- else
287- {
288- FlushToXmds();
289- }
290- }
291+ if (_traceMessages.Count < 1)
292+ return;
293+
294+ // Flush to file (we will send these next time we start up)
295+ FlushToFile();
296 }
297
298+ /// <summary>
299+ /// Flush the Listener
300+ /// </summary>
301 public override void Flush()
302 {
303 // Determine if there is anything to flush
304- if (_traceMessages.Count < 1 || _xmdsProcessing) return;
305-
306- int threshold = ((int)Properties.Settings.Default.collectInterval * 5);
307-
308- // Determine where we want to log.
309- if (Properties.Settings.Default.XmdsLastConnection.AddSeconds(threshold) < DateTime.Now)
310- {
311- FlushToFile();
312- }
313- else
314- {
315- FlushToXmds();
316- }
317+ if (_traceMessages.Count < 1)
318+ return;
319+
320+ FlushToFile();
321+
322+ // See if there are any records to flush to XMDS
323+ Thread logSubmit = new Thread(new ThreadStart(ProcessQueueToXmds));
324+ logSubmit.Start();
325 }
326 }
327

Subscribers

People subscribed via source and target branches

to all changes: