Elmah on Azure ASP MVC Websites and Azure Table Storage

This blog post explains how to install Elmah on ASP MVC website that can be hosted on Windows Azure and how to store the error logs in Azure Table Storage. It is an updated version of the post written by Wade Wegner (which works well on Azure Storage v1.7), the code below is written based on Wade’s works with minor changes like Azure Storage v2, LINQ queries of azure storage and some refactoring.

Nuget required:

  • Elmah MVC
  • Elmah with Windows Azure Table Storage
  • Windows Azure Storage
  • Windows Azure Configuration Manager

After the Elmah with Windows Azure Table Storage Nuget installation, a file named ErrorEntity.cs will be created, make the following changes to the file

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);
}
}
public class TableErrorLog : ErrorLog
{
private const string ElmahTableName = "elmaherrors";
private readonly string _connectionString;

public override ErrorLogEntry GetError(string id)
{
var elmahTable = CloudStorageAccount.Parse(_connectionString).CreateCloudTableClient().GetTableReference(ElmahTableName);
var errors = from errorEntity in elmahTable.CreateQuery()
where errorEntity.PartitionKey.Equals(string.Empty) &&
errorEntity.RowKey.Equals(id)
select errorEntity;

return new ErrorLogEntry(this, id, ErrorXml.DecodeString(errors.Single().SerializedError));
}

public override int GetErrors(int pageIndex, int pageSize, IList errorEntryList)
{
var count = 0;
var elmahTable = CloudStorageAccount.Parse(_connectionString).CreateCloudTableClient().GetTableReference(ElmahTableName);
var errors = (from errorEntity in elmahTable.CreateQuery()
where errorEntity.PartitionKey.Equals(string.Empty)
select errorEntity).Take((pageIndex + 1) * pageSize).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 elmahTable = CloudStorageAccount.Parse(_connectionString).CreateCloudTableClient().GetTableReference(ElmahTableName);
elmahTable.Execute(TableOperation.Insert(entity));
return entity.RowKey;
}

public TableErrorLog(IDictionary config)
{
_connectionString = CloudConfigurationManager.GetSetting("StorageConnectionString");
Initialize();
}

public TableErrorLog(string connectionString)
{
this._connectionString = connectionString;
Initialize();
}

void Initialize()
{
CloudStorageAccount.Parse(_connectionString).CreateCloudTableClient().GetTableReference(ElmahTableName).CreateIfNotExists();
}
}

Add the following settings to the web.config (make sure you created an Azure Storage Service and use your own AccountName and AccountKey.

    <appSettings>
        <add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=xxx;AccountKey=xxx"/>
    </appSettings>

    <elmah>
        <security allowRemoteAccess="1" />
        <errorLog type="SoftInn.TableErrorLog, SoftInn"
              connectionString="DefaultEndpointsProtocol=https;AccountName=xxx;AccountKey=xxx" />
    </elmah>

Done, your ASP MVC website is now Elmah and Azure Storage log ready.

To test the setting locally (localhost://), you can make the following changes (please make sure you revert to the production before uploading it to Azure in order for it to work).

<appSettings>
<add key="StorageConnectionString" value="UseDevelopmentStorage=true"/>
</appSettings>

<elmah>
<security allowRemoteAccess="1" />
<errorLog type="SoftInn.TableErrorLog, SoftInn"
connectionString="UseDevelopmentStorage=true" />
</elmah>

Make sure the Visual Studio Azure Storage Emulator is turned ON to run that locally.
ElmahSetup-1-AzureStorageLocalEmulator

Navigate to /elmah/test/ to get text exception and navigate back to log to see the details of the recorded exception.
ElmahSetup-2-ElmahTestException

I referred to the sites below for additional information about

Advertisements

4 comments

    • Here’s my using list.
      using System;
      using System.Collections.Generic;
      using System.Configuration;
      using System.Linq;
      using Microsoft.WindowsAzure.Storage;
      using Microsoft.WindowsAzure.Storage.Table;
      using Elmah;
      using System.Collections;
      using Microsoft.WindowsAzure;

      Like

  1. I created something similar as well in NuGet and GitHub.

    Also, the count should represent the total number of records, not the number returned. Since the count can’t be grabbed, I do some simple math based on the pageIndex and pageSize and add one if they are at the end to let them know there are more records.

    Hope this helps some of you.

    Like

  2. Attraction is is something you must do to make your ex girlfriend or ex boyfriend feel towards you.
    How frequently can any of us stay off our feet to the
    point exactly where a foot fracture will fully recover.
    These camgirls free live are very popular in porn movies.
    Teen webcams have met the e-commerce version of the wedding registry –
    the wish list. Home Our Team About Us Services Staff Sites Articles
    Networking Trainings Location Contact.

    Like


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s