Created
February 6, 2013 17:35
-
-
Save ElvisLives/4724263 to your computer and use it in GitHub Desktop.
WindowsAzure.ELMAH.Tables (ELMAH with Windows Azure Table Storage ) ErrorEntity for updated for Azure SDK 1.8 and Storage SDK 2.0.0.0 and greater
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using Microsoft.WindowsAzure.Storage; | |
using Microsoft.WindowsAzure.Storage.Table; | |
using Elmah; | |
using System.Collections; | |
using Microsoft.WindowsAzure.ServiceRuntime; | |
namespace YouApplicationNameHere | |
{ | |
/// <summary> | |
/// This class was installed as part of the Elmah.Tables package | |
/// </summary> | |
public class ErrorEntity : TableEntity | |
{ | |
public string SerializedError { get; set; } | |
public ErrorEntity() { } | |
public ErrorEntity(Error error) | |
: base(string.Empty, (DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks).ToString("d19")) | |
{ | |
this.SerializedError = ErrorXml.EncodeString(error); | |
} | |
} | |
/// <summary> | |
/// This class was installed as part of the Elmah.Tables package | |
/// </summary> | |
public class TableErrorLog : ErrorLog | |
{ | |
private string connectionString; | |
public override ErrorLogEntry GetError(string id) | |
{ | |
var table = | |
CloudStorageAccount.Parse(connectionString) | |
.CreateCloudTableClient() | |
.GetTableReference("elmaherrors"); | |
TableOperation retrieveOperation = TableOperation.Retrieve<ErrorEntity>(string.Empty, id); | |
TableResult retrievedResult = table.Execute(retrieveOperation); | |
ErrorEntity entity = retrievedResult.Result as ErrorEntity; | |
return new ErrorLogEntry(this, id, ErrorXml.DecodeString(entity.SerializedError)); | |
} | |
public override int GetErrors(int pageIndex, int pageSize, IList errorEntryList) | |
{ | |
var count = 0; | |
var table = | |
CloudStorageAccount.Parse(connectionString) | |
.CreateCloudTableClient() | |
.GetTableReference("elmaherrors"); | |
TableQuery<ErrorEntity> query = | |
(new TableQuery<ErrorEntity>()).Where(TableQuery.GenerateFilterCondition("PartitionKey", | |
QueryComparisons.Equal, | |
string.Empty)) | |
.Take((pageIndex + 1)*pageSize); | |
var result = table.ExecuteQuery<ErrorEntity>(query); | |
var errors = result.ToList().Skip(pageIndex*pageSize); | |
foreach (var error in errors) | |
{ | |
errorEntryList.Add(new ErrorLogEntry(this, error.RowKey, ErrorXml.DecodeString(error.SerializedError))); | |
count += 1; | |
} | |
return count; | |
} | |
public override string Log(Error error) | |
{ | |
var entity = new ErrorEntity(error); | |
var table = | |
CloudStorageAccount.Parse(connectionString) | |
.CreateCloudTableClient() | |
.GetTableReference("elmaherrors"); | |
TableOperation insertOperation = TableOperation.Insert(entity); | |
table.Execute(insertOperation); | |
return entity.RowKey; | |
} | |
public TableErrorLog(IDictionary config) | |
{ | |
connectionString = (string)config["connectionString"] ?? RoleEnvironment.GetConfigurationSettingValue((string)config["connectionStringName"]); | |
Initialize(); | |
} | |
public TableErrorLog(string connectionString) | |
{ | |
this.connectionString = connectionString; | |
Initialize(); | |
} | |
void Initialize() | |
{ | |
CloudStorageAccount.Parse(connectionString).CreateCloudTableClient().GetTableReference("elmaherrors").CreateIfNotExists(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment