您的位置:首页 > 其它

Using ELMAH in Windows Azure with Table Storage

2011-12-04 02:19 651 查看
ELMAH (Error Logging Modules and Handlers)

http://www.wadewegner.com/2011/08/using-elmah-in-windows-azure-with-table-storage/

In order to get the Elmah code base to work with Windows Azure Table Storage, several steps have to be completed:

Download the Elmah framework to get the Elmah.dll file.
Create a class library project implementing Elmah for Windows Azure Table Storage. We will show you how in the next section.
Reference the newly created class library into an existing ASP.NET or ASP.NET MVC application along with the Elmah framework class libraries.

Make sure to add
Elmah.dll and Microsoft.WindowsAzure.StorageClient.dll as references to this new class library project.We have to createtwo
classes to make Elmah work. The first is the entity (model) which is translated into columns in the Windows Azure Table. The second class is the Error Log extension which Elmah instantiates . The Error log extension needs three methods at the very least:
Log() (add error to table), GetError() (select error from table), GetErrors() (select all errors of pagesize at pageindex).

Adding ELMAH to an ASP.NET Web Application

http://www.asp.net/web-forms/tutorials/deployment/logging-error-details-with-elmah-vb

Integrating ELMAH into a new or existing ASP.NET application is an easy and straightforward process that takes under five minutes. In a nutshell, it involves four simple steps:

Download ELMAH and add the Elmah.dll assembly to your web application,
Register ELMAH's HTTP Modules and Handler in Web.config,
Specify ELMAH's configuration options, and
Create the error log source infrastructure, if needed.

Web.config
<configSections>
<sectionGroup name="elmah">
<!-- NOTE! If you are using ASP.NET 1.x then remove the
requirePermission="false" attribute from the section
elements below as those are only needed for
partially trusted applications in ASP.NET 2.0 -->
<section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah"/>
<section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah"/>
<section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah"/>
<section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah"/>
<section name="errorTweet" requirePermission="false" type="Elmah.ErrorTweetSectionHandler, Elmah"/>
</sectionGroup>
</configSections>


<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="Elmah.ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
</modules>
<handlers>
<add name="Elmah" path="elmah.axd" verb="POST,GET,HEAD" type="Elmah.ErrorLogPageFactory, Elmah" preCondition="integratedMode" />
</handlers>
</system.webServer>

<elmah>
<security allowRemoteAccess="yes" />
<errorLog type="ELMAHSample.TableErrorLog, ELMAHSample"
connectionString="UseDevelopmentStorage=true" />
</elmah>
<!--
<errorLog
type="WebRole1.TableErrorLog, WebRole1"
connectionString="DefaultEndpointsProtocol=https;AccountName=YOURSTORAGEACCOUNT;
AccountKey=YOURSTORAGEKEY" />
-->


ErrorEntity.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure.StorageClient;
using Elmah;

namespace ELMAHSample
{
public class ErrorEntity : TableServiceEntity
{
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);
}
}
}


TableErrorLog.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure;
using System.Collections;
using Elmah;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.StorageClient;

namespace ELMAHSample
{
public class TableErrorLog : ErrorLog
{
private string connectionString;

public override ErrorLogEntry GetError(string id)
{
return new ErrorLogEntry(this, id, ErrorXml.DecodeString(CloudStorageAccount.Parse(
connectionString).CreateCloudTableClient().GetDataServiceContext()
.CreateQuery<ErrorEntity>("elmaherrors").Where(e => e.PartitionKey == string.Empty
&& e.RowKey == id).Single().SerializedError));
}

public override int GetErrors(int pageIndex, int pageSize, IList errorEntryList)
{
var count = 0;
foreach (var error in CloudStorageAccount.Parse(connectionString).
CreateCloudTableClient().GetDataServiceContext()
.CreateQuery<ErrorEntity>("elmaherrors")
.Where(e => e.PartitionKey == string.Empty).AsTableServiceQuery()
.Take((pageIndex + 1) * pageSize).ToList().Skip(pageIndex * pageSize))
{
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 context = CloudStorageAccount.Parse(connectionString)
.CreateCloudTableClient().GetDataServiceContext();
context.AddObject("elmaherrors", entity);
context.SaveChangesWithRetries();
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()
.CreateTableIfNotExist("elmaherrors");
}
}
}


Configure web.config to use the Elmah library.
Verify/Test class library works by throwing error then view it in the Elmah Web tool.

http://127.0.0.1:81/elmah.axd

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: