博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ELMAH在ASP.NET MVC中的使用
阅读量:6550 次
发布时间:2019-06-24

本文共 10548 字,大约阅读时间需要 35 分钟。

Google:

Demo:
Elman:
elmah)
Compact:
elmah.sqlservercompact)

安装

最简单的方式通过Nuget获得

我的环境是MVC4所以安装Elamh.MVC,因为这样会为我们默认配置好大部分的配置,当然也可以安装Elamh(如配置 ),也可以安装XML,MongDB,MSSQL,MYSQL,Fiter等配置

Elmah.MVC 依赖与Elmah.corelibrary

"Could not load type 'Elmah.ErrorLogModule' from assembly 'Elmah'." 这个错误花了好长时间,开始我以为是IIS6与IIS7配置问题,最后发现是项目名与Elmah重复了

这里我故意写了句代码

public ActionResult Index()        {            ViewBag.Message = "修改此模板以快速启动你的 ASP.NET MVC 应用程序。";            int num = Convert.ToInt32("Irving");            return View();        });

访问: 显示错误

当然可以配置不显示错误

或者将异常抛出去

public ActionResult Index()        {            ViewBag.Message = "修改此模板以快速启动你的 ASP.NET MVC 应用程序。";            try            {                int num = Convert.ToInt32("Irving");            }            catch (Exception e)            {                ErrorSignal.FromCurrentContext().Raise(e);            }            return View();        }

访问:

可以查看详情,以XML 或者JSON方式显示

储存方式

Memory

这个没有什么好说的,正式环境不推荐使用

XML

Email

有关stmp协议看这里

SQLite

没有测试

MSSQL

这里需要手动插件表,脚本如下 或者 Install-Package elmah.sqlservercompact

View Code
/*     ELMAH - Error Logging Modules and Handlers for ASP.NET   Copyright (c) 2004-9 Atif Aziz. All rights reserved.      Author(s):          Atif Aziz, http://www.raboof.com        Phil Haacked, http://haacked.com     Licensed under the Apache License, Version 2.0 (the "License");   you may not use this file except in compliance with the License.   You may obtain a copy of the License at        http://www.apache.org/licenses/LICENSE-2.0     Unless required by applicable law or agreed to in writing, software   distributed under the License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   See the License for the specific language governing permissions and   limitations under the License.  */-- ELMAH DDL script for Microsoft SQL Server 2000 or later.-- $Id: SQLServer.sql 677 2009-09-29 18:02:39Z azizatif $DECLARE @DBCompatibilityLevel INTDECLARE @DBCompatibilityLevelMajor INTDECLARE @DBCompatibilityLevelMinor INTSELECT     @DBCompatibilityLevel = cmptlevel FROM     master.dbo.sysdatabases WHERE     name = DB_NAME()IF @DBCompatibilityLevel <> 80BEGIN    SELECT @DBCompatibilityLevelMajor = @DBCompatibilityLevel / 10,            @DBCompatibilityLevelMinor = @DBCompatibilityLevel % 10               PRINT N'    ===========================================================================    WARNING!     ---------------------------------------------------------------------------        This script is designed for Microsoft SQL Server 2000 (8.0) but your     database is set up for compatibility with version '     + CAST(@DBCompatibilityLevelMajor AS NVARCHAR(80))     + N'.'     + CAST(@DBCompatibilityLevelMinor AS NVARCHAR(80))     + N'. Although     the script should work with later versions of Microsoft SQL Server,     you can ensure compatibility by executing the following statement:        ALTER DATABASE ['     + DB_NAME()     + N']     SET COMPATIBILITY_LEVEL = 80    If you are hosting ELMAH in the same database as your application     database and do not wish to change the compatibility option then you     should create a separate database to host ELMAH where you can set the     compatibility level more freely.        If you continue with the current setup, please report any compatibility     issues you encounter over at:        http://code.google.com/p/elmah/issues/list    ==========================================================================='ENDGO/* ------------------------------------------------------------------------         TABLES   ------------------------------------------------------------------------ */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]GOALTER TABLE [dbo].[ELMAH_Error] WITH NOCHECK ADD     CONSTRAINT [PK_ELMAH_Error] PRIMARY KEY NONCLUSTERED ([ErrorId]) ON [PRIMARY] GOALTER TABLE [dbo].[ELMAH_Error] ADD     CONSTRAINT [DF_ELMAH_Error_ErrorId] DEFAULT (NEWID()) FOR [ErrorId]GOCREATE NONCLUSTERED INDEX [IX_ELMAH_Error_App_Time_Seq] ON [dbo].[ELMAH_Error] (    [Application]   ASC,    [TimeUtc]       DESC,    [Sequence]      DESC) ON [PRIMARY]GO/* ------------------------------------------------------------------------         STORED PROCEDURES                                                         ------------------------------------------------------------------------ */SET QUOTED_IDENTIFIER ON GOSET ANSI_NULLS ON GOCREATE PROCEDURE [dbo].[ELMAH_GetErrorXml](    @Application NVARCHAR(60),    @ErrorId UNIQUEIDENTIFIER)AS    SET NOCOUNT ON    SELECT         [AllXml]    FROM         [ELMAH_Error]    WHERE        [ErrorId] = @ErrorId    AND        [Application] = @ApplicationGOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOSET QUOTED_IDENTIFIER ON GOSET ANSI_NULLS ON GOCREATE 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    SELECT         @TotalCount = COUNT(1)     FROM         [ELMAH_Error]    WHERE         [Application] = @Application    -- Get the ID of the first error for the requested page    SET @StartRowIndex = @PageIndex * @PageSize + 1    IF @StartRowIndex <= @TotalCount    BEGIN        SET ROWCOUNT @StartRowIndex        SELECT              @FirstTimeUTC = [TimeUtc],            @FirstSequence = [Sequence]        FROM             [ELMAH_Error]        WHERE               [Application] = @Application        ORDER BY             [TimeUtc] DESC,             [Sequence] DESC    END    ELSE    BEGIN        SET @PageSize = 0    END    -- Now set the row count to the requested page size and get    -- all records below it for the pertaining application.    SET ROWCOUNT @PageSize    SELECT         errorId     = [ErrorId],         application = [Application],        host        = [Host],         type        = [Type],        source      = [Source],        message     = [Message],        [user]      = [User],        statusCode  = [StatusCode],         time        = CONVERT(VARCHAR(50), [TimeUtc], 126) + 'Z'    FROM         [ELMAH_Error] error    WHERE        [Application] = @Application    AND        [TimeUtc] <= @FirstTimeUTC    AND         [Sequence] <= @FirstSequence    ORDER BY        [TimeUtc] DESC,         [Sequence] DESC    FOR        XML AUTOGOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOSET QUOTED_IDENTIFIER ON GOSET ANSI_NULLS ON GOCREATE 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        )GOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GO

ErrorFiltering

我们可以通过代码的方式过滤错误,如我们不想在应用程序发生404错误的时候记录日志,我们只需要在Global.asax文件中自定义

这里我启用XML与Email两种方式

-->

allowRemoteAccess 中0表示允许本地浏览,1表示允许本地与远程,其他方式没有试(RSS MYSQL ORACEL等)

protected void ErrorLog_Filtering(object sender, ExceptionFilterEventArgs e)        {            FilterError404Exception(e);            FilterErrorValidationException(e);            FilterErrorNotFileException(e);        }        protected void ErrorMail_Filtering(object sender, ExceptionFilterEventArgs e)        {            FilterError404Exception(e);        }        ///         /// 处理404        ///         ///         void FilterError404Exception(ExceptionFilterEventArgs e)        {            if (e.Exception.GetBaseException() is HttpException)            {                HttpException ex = (HttpException)e.Exception.GetBaseException();                if (ex.GetHttpCode() == 404)                {                    e.Dismiss();                }            }        }        ///         /// 处理 HttpRequestValidationException        ///         ///         void FilterErrorValidationException(ExceptionFilterEventArgs args)        {            if (args.Exception.GetBaseException() is HttpRequestValidationException)            {                args.Dismiss();            }        }        void FilterErrorNotFileException(ExceptionFilterEventArgs e)        {            if (e.Exception.GetBaseException() is FileNotFoundException)            {                e.Dismiss();            }        }

IIS配置

II6与IIS7+异同,这个貌似跟集成模式与经典模式有关系,MVC中如果是用Nuget安装的话,应该不用考虑这个问题,如果有问题参考下面链接

安全性

可以配置<security>节点设置allowRemoteAccess为0,这样就不能够通过远程的方式进行访问,如果想既支持远程查看日志又能够保证安全性,首先设置allowRemoteAccess为1然后需要我们对elmah.axd路径进行权限控制,可以在<loaction>节点下进行相关配置(Form验证)。

这篇文章非常好,之前有一篇文章讨论 From验证安全性 machineconfig中的key不被篡改的话,能够确保安全性,链接找不到了

ElmahR

ElmahR是一个集成了SignalR技术,可以把错误的消息推送到浏览器上,可以在这里看DEMO :

ElmahR = ELMAH + SignalR (1.0.0 released!)
Streaming logs with SignalR 2.0

更新:新版本已支持APS.NET MVC 性能路由的调试

Refer:

ASP.NET MVC

ASP.NET WEBFROM

ErrorFiltering

Securing

  (备注)

IIS

转载地址:http://gvuco.baihongyu.com/

你可能感兴趣的文章
【270天】每日项目总结系列008(2017.11.02)
查看>>
记一次线上CPU超高的排查过程
查看>>
获取群成员邀请关系
查看>>
Ionic:livereload on iOS and android
查看>>
react day one 让陡峭的学习曲线平缓一点
查看>>
Coursera 的 GraphQL 之旅
查看>>
打造高性能高可靠块存储系统
查看>>
TCP/IP及内核参数优化调优
查看>>
LINUX查看CPU信息
查看>>
AppServ开启虚拟主机
查看>>
如何定位和解决Andorid的内存溢出问题(大总结)
查看>>
Linux下php安装openSSL模块
查看>>
如何删除mysql数据库的日志文件
查看>>
Swift/OC计时器使用方法
查看>>
AD的备份与还原
查看>>
和第三代动词算子式代码生成器光配合的前后端分离示例代码
查看>>
502 Bad Gateway 错误的解决办法
查看>>
convirt(二)—— 创建第一台虚机
查看>>
足球——2011-2012意甲球队队标
查看>>
mysql性能优化
查看>>