Skip to content

Instantly share code, notes, and snippets.

@ElvisLives
Created February 6, 2013 17:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ElvisLives/4724263 to your computer and use it in GitHub Desktop.
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
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