-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBlobErrorLog.cs
117 lines (100 loc) · 3.96 KB
/
BlobErrorLog.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
using System;
using System.Collections;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
namespace Elmah.Azure
{
public sealed class BlobErrorLog : ErrorLog
{
public BlobErrorLog(IDictionary config)
{
if (config == null) throw new ArgumentNullException("config");
if (!config.Contains("connectionStringName"))
throw new System.ApplicationException("Configuration string is missing for the Windows Azure Blob Storage Error Log.");
_connectionString = RoleEnvironment.GetConfigurationSettingValue((string)config["connectionStringName"]);
Initialize();
}
public BlobErrorLog(string connectionString)
{
if (connectionString == null) throw new ArgumentNullException("connectionString");
this._connectionString = connectionString;
this.Initialize();
}
public override ErrorLogEntry GetError(string id)
{
if (id == null) throw new ArgumentNullException("id");
var blobContainer = GetBlobContainer();
var blob = blobContainer.GetBlockBlobReference(id);
using (var blobStream = blob.OpenRead())
using (var reader = new XmlTextReader(blobStream))
{
var error = ErrorXml.Decode(reader);
return new ErrorLogEntry(this, id, error);
}
}
public override int GetErrors(int pageIndex, int pageSize, IList errorEntryList)
{
if (pageIndex < 0) throw new ArgumentOutOfRangeException("pageIndex", pageIndex, null);
if (pageSize < 0) throw new ArgumentOutOfRangeException("pageSize", pageSize, null);
if (errorEntryList == null) throw new ArgumentNullException("errorEntryList");
var blobContainer = GetBlobContainer();
var blobs = blobContainer.ListBlobs()
.OfType<CloudBlockBlob>()
.Skip(pageIndex * pageSize)
.Take(pageSize);
var count = 0;
foreach (var blob in blobs)
{
count++;
using (var blobStream = blob.OpenRead())
using (var reader = new XmlTextReader(blobStream))
{
var error = ErrorXml.Decode(reader);
errorEntryList.Add(new ErrorLogEntry(this, blob.Name, error));
}
}
return count;
}
public override string Log(Error error)
{
var errorId = string.Format(CultureInfo.InvariantCulture, "error-{0:x16}-{1:N}.xml",
(DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks),
Guid.NewGuid()
);
var blobContainer = GetBlobContainer();
var blob = blobContainer.GetBlockBlobReference(errorId);
using (var blobStream = blob.OpenWrite())
using (var writer = new XmlTextWriter(blobStream, Encoding.UTF8))
{
writer.WriteStartElement("error");
ErrorXml.Encode(error, writer);
writer.WriteEndElement();
}
return errorId;
}
public override string Name
{
get
{
return "Windows Azure Blob Storage Error Log";
}
}
private void Initialize()
{
var blobContainer = GetBlobContainer();
blobContainer.CreateIfNotExists();
}
private CloudBlobContainer GetBlobContainer()
{
var blobClient = CloudStorageAccount.Parse(_connectionString).CreateCloudBlobClient();
var blobContainer = blobClient.GetContainerReference("elmaherrors");
return blobContainer;
}
private readonly string _connectionString;
}
}