1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Diagnostics;
|
---|
6 | using Microsoft.WindowsAzure.StorageClient;
|
---|
7 | using Microsoft.WindowsAzure;
|
---|
8 | using System.Threading;
|
---|
9 |
|
---|
10 | namespace HeuristicLab.Services.Optimization.ControllerService.Logging {
|
---|
11 | public class TableStorageTraceEntry : TableServiceEntity {
|
---|
12 | public TableStorageTraceEntry(string message) {
|
---|
13 | this.PartitionKey = "Trace";
|
---|
14 | this.RowKey = Guid.NewGuid().ToString();
|
---|
15 | this.Message = message;
|
---|
16 | }
|
---|
17 |
|
---|
18 | public string Message { get; set; }
|
---|
19 | };
|
---|
20 |
|
---|
21 | public class TableStorageTraceListener : TraceListener {
|
---|
22 |
|
---|
23 | private string traceTable;
|
---|
24 |
|
---|
25 | public string TraceTable {
|
---|
26 | get {
|
---|
27 | if (traceTable == null) {
|
---|
28 | traceTable = CloudConfigurationManager.GetSetting("TraceTable");
|
---|
29 | }
|
---|
30 | return traceTable;
|
---|
31 | }
|
---|
32 | }
|
---|
33 |
|
---|
34 | private bool traceEnabled = false;
|
---|
35 | private DateTime? lastChecked = null;
|
---|
36 | public bool TraceEnabled {
|
---|
37 | get {
|
---|
38 | if (!lastChecked.HasValue || (DateTime.Now - lastChecked.Value).TotalSeconds > 20) {
|
---|
39 | lastChecked = DateTime.Now;
|
---|
40 | traceEnabled = CloudConfigurationManager.GetSetting("TraceTableEnabled") != null && CloudConfigurationManager.GetSetting("TraceTableEnabled").ToLower() == "true";
|
---|
41 | }
|
---|
42 | return traceEnabled;
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | private CloudStorageAccount account;
|
---|
47 | private TableServiceContext context;
|
---|
48 | private CloudTableClient client;
|
---|
49 | private ThreadLocal<bool> doLog = new ThreadLocal<bool>();
|
---|
50 |
|
---|
51 | public TableServiceContext Context {
|
---|
52 | get {
|
---|
53 | if (account == null) {
|
---|
54 | account = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
|
---|
55 | client = account.CreateCloudTableClient();
|
---|
56 | context = client.GetDataServiceContext();
|
---|
57 | }
|
---|
58 | client.CreateTableIfNotExist(TraceTable);
|
---|
59 | return context;
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 |
|
---|
64 | public override void Write(string message) {
|
---|
65 | if (!doLog.IsValueCreated) {
|
---|
66 | doLog.Value = true;
|
---|
67 | }
|
---|
68 |
|
---|
69 | // prevent stack overflows
|
---|
70 | if (!doLog.Value) {
|
---|
71 | return;
|
---|
72 | }
|
---|
73 |
|
---|
74 | doLog.Value = false;
|
---|
75 | if (TraceEnabled) {
|
---|
76 | Context.AddObject(TraceTable, new TableStorageTraceEntry(message));
|
---|
77 | Context.SaveChangesWithRetries();
|
---|
78 | }
|
---|
79 | doLog.Value = true;
|
---|
80 | }
|
---|
81 |
|
---|
82 | public override void WriteLine(string message) {
|
---|
83 | Write(message + "\n");
|
---|
84 | }
|
---|
85 |
|
---|
86 | public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data) {
|
---|
87 | Write(data.ToString());
|
---|
88 | }
|
---|
89 |
|
---|
90 | public override void TraceEvent(TraceEventCache eventCache, string source, TraceEventType eventType, int id, string message) {
|
---|
91 | Write(message);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|