使用 Elmah一些要注意的问题
Posted 吴晓阳
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用 Elmah一些要注意的问题相关的知识,希望对你有一定的参考价值。
http://www.cnblogs.com/apsnet/archive/2012/04/28/2474730.html
1. Elmah使用后,在发布时,要区分IIS6和IIS7 ,IIS6下 HttpModules必须在web.config或App.config 配置文件中使用 system.web注册,而IIS7 必须使用 sysem.webserver.
IIS6:
1 <system.web> 2 <httpModules> 3 <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" /> 4 </httpModules> 5 </system.web>
IIS7:
1 <system.webServer> 2 <modules> 3 <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" /> 4 </modules> 5 </system.webServer>
令外介绍下Elmah使用,这个是一款ASP.NET下的系统错误记录管理工具。
下载相应的dll,引用到project.
如果想记录在数据库,必须建立一张错误记录表。web.config配置如下.
1 <configSections> 2 <sectionGroup name="elmah"> 3 <section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah"/> 4 <section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah"/> 5 <section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah"/> 6 <section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah"/> 7 </sectionGroup> 8 </configSections> 9 10 <connectionStrings> 11 <add name="strCon" connectionString="Data Source=J18423X;Initial Catalog=UserDB;User ID=sa;Password=1234" providerName="System.Data.SqlClient"/> 12 </connectionStrings> 13 14 <elmah> 15 <security allowRemoteAccess="0" /> 16 <errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="strCon" /> 17 </elmah> 18 19 <httpHandlers> 20 <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah"/> 21 </httpHandlers> 22
//注意httpModules在IIS6和7中需要注意,如问题所述。
23 <httpModules> 24 <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/> 25 </httpModules> 26 27 <location path="elmah.axd"> 28 <system.web> 29 <authorization> 30 <deny users="?"/> 31 </authorization> 32 </system.web> 33 </location>
数据库相关
/* 错误管理工具 SQL代码 */ CREATE TABLE dbo.ELMAH_Error ( ErrorId UNIQUEIDENTIFIER NOT NULL, Application NVARCHAR(60) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, Host NVARCHAR(50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, Type NVARCHAR(100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, Source NVARCHAR(60) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, Message NVARCHAR(500) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [User] NVARCHAR(50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, StatusCode INT NOT NULL, TimeUtc DATETIME NOT NULL, Sequence INT IDENTITY (1, 1) NOT NULL, AllXml NTEXT COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO
//创建主键
ALTER TABLE dbo.ELMAH_Error WITH NOCHECK ADD CONSTRAINT PK_ELMAH_Error PRIMARY KEY NONCLUSTERED ( ErrorId ) ON [PRIMARY] GO
//创建默认约束
ALTER TABLE dbo.ELMAH_Error ADD CONSTRAINT DF_ELMAH_Error_ErrorId DEFAULT (newid()) FOR [ErrorId] GO
//创建非聚集索引
CREATE NONCLUSTERED INDEX IX_ELMAH_Error_App_Time_Seq ON dbo.ELMAH_Error ( [Application] ASC, [TimeUtc] DESC, [Sequence] DESC ) ON [PRIMARY] GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
//创建存储过程,得到单个错误xml
CREATE PROCEDURE dbo.ELMAH_GetErrorXml ( @Application NVARCHAR(60), @ErrorId UNIQUEIDENTIFIER ) AS SET NOCOUNT ON SELECT AllXml FROM ELMAH_Error WHERE ErrorId = @ErrorId AND Application = @Application GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO
//创建存储过程,得到错误总记录
SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO CREATE PROCEDURE dbo.ELMAH_GetErrorsXml ( @Application NVARCHAR(60), @PageIndex INT = 0, @PageSize INT = 15, @TotalCount INT OUTPUT ) AS SET NOCOUNT ON DECLARE @FirstTimeUTC DateTime DECLARE @FirstSequence int DECLARE @StartRow int DECLARE @StartRowIndex int -- Get the ID of the first error for the requested page SET @StartRowIndex = @PageIndex * @PageSize + 1 SET ROWCOUNT @StartRowIndex SELECT @FirstTimeUTC = TimeUTC, @FirstSequence = Sequence FROM ELMAH_Error WHERE Application = @Application ORDER BY TimeUTC DESC, Sequence DESC -- Now set the row count to the requested page size and get -- all records below it for the pertaining application. SET ROWCOUNT @PageSize SELECT @TotalCount = COUNT(1) FROM ELMAH_Error WHERE Application = @Application SELECT errorId, application, host, type, source, message, [user], statusCode, CONVERT(VARCHAR(50), TimeUtc, 126) + \'Z\' time FROM ELMAH_Error error WHERE Application = @Application AND TimeUTC <= @FirstTimeUTC AND Sequence <= @FirstSequence ORDER BY TimeUTC DESC, Sequence DESC FOR XML AUTO GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO
//存储过程:插入数据
SET QUOTED_IDENTIFIER ON GO SET ANSI_NULLS ON GO CREATE PROCEDURE dbo.ELMAH_LogError ( @ErrorId UNIQUEIDENTIFIER, @Application NVARCHAR(60), @Host NVARCHAR(30), @Type NVARCHAR(100), @Source NVARCHAR(60), @Message NVARCHAR(500), @User NVARCHAR(50), @AllXml NTEXT, @StatusCode INT, @TimeUtc DATETIME ) AS SET NOCOUNT ON INSERT INTO ELMAH_Error ( ErrorId, Application, Host, Type, Source, Message, [User], AllXml, StatusCode, TimeUtc ) VALUES ( @ErrorId, @Application, @Host, @Type, @Source, @Message, @User, @AllXml, @StatusCode, @TimeUtc ) GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO
以上是关于使用 Elmah一些要注意的问题的主要内容,如果未能解决你的问题,请参考以下文章