• ELMAH (ASP.NET错误日志处理)使用方法


    转:http://www.oschina.net/question/12_9124

    ELMAH(Error Logging  Modules and Handlers for ASP.NET )是一款ASP.NET下的系统错误记录管理工具,它可以非常方便的把“黄屏”错误记录到XML,MS  SQLServer,SQLite,MySql等文件中,甚至它还可以发送邮件。

    这里我就记一下使用它的方法。本文以记录到MS  SQL Server 2005为例:

    1. 下载ELMAH

       ELMAH官方地址:http://code.google.com/p/elmah/
             版本:ELMAH-1.1-bin.zip

    2.把dll文件复制到项目的bin目录下。

       DLL文件:Elmah.dll   
              版本:1.0.9414.1441    
             大小:114688 bytes ( 112.000 KB,  0.109  MB )

    3.设置Web.config

      xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <错误日志管理系统 >
        <sectionGroup name="elmah">
          <section name="security"  type="Elmah.SecuritySectionHandler, Elmah"/>
          <section name="errorLog" type="Elmah.ErrorLogSectionHandler, Elmah"/>
          <section name="errorMail" type="Elmah.ErrorMailSectionHandler, Elmah"/>
          <section name="errorFilter"  type="Elmah.ErrorFilterSectionHandler, Elmah"/>
        <sectionGroup>
      <configSections>
      <elmah>
        <security allowRemoteAccess="0" />
        <errorLog type="Elmah.SqlErrorLog, Elmah" 
                connectionStringName="ConnectionString" />
      <elmah>
      < elmah只允许管理员访问>
      <location path="elmah.axd">
        <system.web>
          <authorization>
            <deny users="?"/>
            <allow users="Admin"/>
          <authorization>
        <system.web>
      <location>
      
       <appSettings />
    <  连接字符串> 
      <connectionStrings>
        <add name="ConnectionString" connectionString="Data Source=qq\SQLEXPRESS;Initial Catalog=xx;User ID=xx;Password=xx" providerName="System.Data.SqlClient" />
      <connectionStrings>
        <system.web>
          <httpHandlers>
            <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah"/>
          <httpHandlers>
          <httpModules>
            <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>
          <httpModules>
       <compilation debug="true" />
          <authentication mode="Forms">
            <forms loginUrl="login.aspx" name=".ASPXAUTH"><forms>
          <authentication>
          <authorization>
            <deny users="*"/>
           <authorization>
        <system.web>
    <configuration>
    

            4.向数据添加表

    /* 错误管理工具 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
    
    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

    5 预览

    http://localhost/youresiteName/elmah.axd

    可能需要先登录login.aspx.如果不需要登录,可以在web.config里删掉相应代码.

  • 相关阅读:
    我总结的存储过程编码规范 荣
    清空数据日志[转]
    GridView使用DataBind方法绑定数据时的分页问题[转]
    Datatable绑定GridView[转]
    javascript 刷新页面 关闭窗口[转]
    GridView和CheckBox结合[转]
    SQLServer 游标简介与使用说明[转]
    Windows Mobile 5中的新特性
    flash与后台数据交换方法整理3WebService篇
    netbeans+j2mepolish 环境下开发黑莓(BlackBerry) 程序
  • 原文地址:https://www.cnblogs.com/jackljf/p/3588964.html
Copyright © 2020-2023  润新知