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
ErrorEntity.cs
TableErrorLog.cs
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
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
相关文章推荐
- More about “PartitionKey”&"RowKey” in windows azure table storage
- Windows Azure Table Storage 解决 Guid 查询问题
- Windows Azure Storage (6) Windows Azure Storage之Table
- 云计算和数据 关于Windows Azure Table Storage 和 SQL Azure的选择
- Overview of Running a Windows Azure Application with Local Storage
- How Do I: Use Windows Azure Storage with my Application?
- 【FWD】Windows Azure Table Storage vs. Windows SQL Azure
- Overview of Running a Windows Azure Application with Local Storage
- Azure Basic - Insert data into BlockBlob, Table and Queue (with Diagnostic + Azure Storage Explore)
- 云计算和数据 关于Windows Azure Table Storage 和 SQL Azure的选择
- Using the default SQL Server instance for Windows Azure development storage
- [Windows Azure] How to use the Windows Azure Blob Storage Service in .NET
- Windows Azure Table Storage vs. Windows SQL Azure
- [MSDN] Using the Windows Azure Storage Services
- Using the ReportViewer ASP.NET Control in Windows Azure
- Windows Azure Table storage 之 动态Table类 DynamicTableEntity
- Windows Azure Platform (二十六) Windows Azure Storage之Table、Queue
- Overview of Retry Policies in the Windows Azure Storage Client Library
- Designing Data Storage Architecture-Using the Windows Azure Storage Services
- using sqlite in windows phone 7/successful to deal with the problem in wp to use like select chinese character