-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathLog.cs
246 lines (217 loc) · 8.49 KB
/
Log.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* *********************************************************************
* This Source Code Form is copyright of 51Degrees Mobile Experts Limited.
* Copyright © 2017 51Degrees Mobile Experts Limited, 5 Charlotte Close,
* Caversham, Reading, Berkshire, United Kingdom RG4 7BY
*
* This Source Code Form is the subject of the following patents and patent
* applications, owned by 51Degrees Mobile Experts Limited of 5 Charlotte
* Close, Caversham, Reading, Berkshire, United Kingdom RG4 7BY:
* European Patent No. 2871816;
* European Patent Application No. 17184134.9;
* United States Patent Nos. 9,332,086 and 9,350,823; and
* United States Patent Application No. 15/686,066.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0.
*
* If a copy of the MPL was not distributed with this file, You can obtain
* one at http://mozilla.org/MPL/2.0/.
*
* This Source Code Form is “Incompatible With Secondary Licenses”, as
* defined by the Mozilla Public License, v. 2.0.
* ********************************************************************* */
#region Usings
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
#endregion
using System.Threading.Tasks;
#if AZURE
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using System.Text.RegularExpressions;
#endif
namespace FiftyOne
{
/// <summary>
/// This is the base class for recording messages in text files. The write operation to
/// file occurs outside of the current thread ensuring minimal impact on performance.
/// If the process completes before the thread has finished writing it is possible that
/// some messages will not be written. This is by design.
/// </summary>
/// <remarks>
/// This class should not be used in developers code.
/// </remarks>
public abstract class Log
{
#region Fields
/// <summary>
/// An internal object used for synchronising access to the message queue.
/// </summary>
protected internal static object _syncQueue = new object();
/// <summary>
/// An internal object used to synchronising access to the log file.
/// </summary>
protected internal static object _syncWait = new object();
/// <summary>
/// The message queue to use to record log entries.
/// </summary>
private readonly Queue<string> _queue = new Queue<string>();
/// <summary>
/// Set to true if the message writing thread is running.
/// </summary>
private bool _running;
#endregion
#region Abstract Methods
/// <summary>
/// Returns the full path to the file to be written to.
/// </summary>
protected abstract string LogFile { get; }
#endregion
#region Methods
#if AZURE
/// <summary>
/// The main loop for the log table service thread.
/// </summary>
protected void Run(Object stateInfo)
{
// Initialise the Azure table service creating the table if it does not exist.
var storageAccount = CloudStorageAccount.Parse(Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.GetConfigurationSettingValue(FiftyOne.Foundation.Mobile.Constants.AZURE_STORAGE_NAME));
var serviceContext = new TableServiceContext(storageAccount.TableEndpoint.ToString(), storageAccount.Credentials);
var tableName = Regex.Replace(LogFile, "[^A-Za-z]+", String.Empty);
storageAccount.CreateCloudTableClient().CreateTableIfNotExist(tableName);
while (IsQueueEmpty() == false)
{
try
{
while (IsQueueEmpty() == false)
{
lock (_syncQueue)
{
string message = _queue.Peek();
if (message != null)
{
var entity = new LogMessageEntity(message);
serviceContext.AddObject(tableName, entity);
_queue.Dequeue();
}
}
}
// Commit the new log entries to the database.
serviceContext.SaveChanges();
}
catch
{
// End the thread and wait until another message comes
// arrives to resume writing.
_running = false;
return;
}
// Sleep for 50ms incase any new messages come in.
Thread.Sleep(50);
}
_running = false;
}
#else
/// <summary>
/// The main loop for the log file thread.
/// </summary>
protected void Run(Object stateInfo)
{
while (IsQueueEmpty() == false)
{
try
{
if (String.IsNullOrEmpty(LogFile) == false)
{
try
{
using (var writer = new StreamWriter(
File.Open(LogFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite)))
{
while (IsQueueEmpty() == false)
{
lock (_syncQueue)
{
string message = _queue.Peek();
if (message != null)
{
writer.WriteLine(message);
_queue.Dequeue();
}
}
}
}
}
catch
{
// End the thread and wait until another message
// arrives to resume writing.
_running = false;
return;
}
}
}
catch
{
// End the thread and wait until another message comes
// arrives to resume writing.
_running = false;
return;
}
// Sleep for 50ms incase any new messages come in.
Thread.Sleep(50);
}
_running = false;
}
#endif
/// <summary>
/// Returns true if the message queue is empty.
/// </summary>
/// <returns></returns>
private bool IsQueueEmpty()
{
return (_queue.Count == 0);
}
/// <summary>
/// Makes sure the logging thread is running and then writes the message
/// to the queue of messages to be serviced.
/// </summary>
/// <param name="message">The message to be written to the log file.</param>
protected internal void Write(string message)
{
// If the queue is very large sleep for 10ms and
// give it time to reduce.
// This should never happen if debug is disabled.
if (_queue.Count > 100 && _running)
{
lock (_syncWait)
{
if (_queue.Count > 100 && _running)
{
Thread.Sleep(10);
}
}
}
// Add this entry to the queue for writing.
lock (_syncQueue)
{
_queue.Enqueue(message);
}
// Check the thread is running.
if (_running == false)
{
lock (_syncQueue)
{
if (_running == false)
{
_running = true;
Task.Factory.StartNew(() => Run(null));
}
}
}
}
#endregion
}
}