Skip to content

Instantly share code, notes, and snippets.

@tommck
Forked from ElvisLives/ErrorEntity.cs
Created May 9, 2013 19:48
Show Gist options
  • Save tommck/5550049 to your computer and use it in GitHub Desktop.
Save tommck/5550049 to your computer and use it in GitHub Desktop.
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