2022年5月28日 星期六

[研究][ASP.NET]ELMAH 1.2 SP2 Web.Config 範例

[研究][ASP.NET]ELMAH 1.2 SP2 Web.Config 範例

2022-05-27

ELMAH - Wikipedia
https://en.wikipedia.org/wiki/ELMAH
ELMAH是用於ASP.NET Web服務的開源調試工具。當添加到機器上正在運行的Web應用程序中時,引發的異常將觸發ELMAH工具中的事件處理程序。這些事件處理程序可以包括日誌記錄到各種數據庫後端,可以從Web門戶查看的日誌記錄,以及發送通知電子郵件,推文和RSS文章以向管理員建議問題。

官方網站
https://elmah.github.io/


ELMAH-1.2-sp2-sample-web.config
來源:https://code.google.com/archive/p/elmah/downloads

xxxxx
<?xml version="1.0" encoding="utf-8"?>
<configuration>

    <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>

    <elmah>
    
        <security allowRemoteAccess="0" />

        <!--
            Use to log errors to Microsoft SQL Server 2000 or later
            using ASP.NET 2.0. Set value of connectionStringName attribute
            to the name of the connection string settings to use from
            the <connectionStrings> section.
        
        <errorLog type="Elmah.SqlErrorLog, Elmah" 
            connectionStringName="..." />
        -->

        <!--
            Use to log errors to Microsoft SQL Server 2000 or later
            using ASP.NET 1.x and adjust the value of connectionString 
            attribute to your settings.

        <errorLog type="Elmah.SqlErrorLog, Elmah" 
            connectionString="Data Source=.;Initial Catalog=ELMAH;Trusted_Connection=True" />
        -->

        <!--
            Use to log errors to MySQL 5.0+. Set value of 
            connectionStringName attribute to the name of the connection 
            string settings to use from the <connectionStrings> section.

        <errorLog type="Elmah.MySqlErrorLog, Elmah" 
            connectionString="..." />
        -->

     <!--
             Use to log errors to a SQL Server Compact 4 database file 
             (requires .NET 3.5 SP1). Set value of connectionStringName 
             attribute to the name of the connection string settings to use 
             from the <connectionStrings> section.
    
          <errorLog type="Elmah.SqlServerCompactErrorLog, Elmah" connectionStringName="..." />

             The format of the connection string should be as follows:
    
          <connectionStrings>
            <add name="..." connectionString="data source=[path to the database file]" />
          </connectionStrings>
    
            Replace the content of the brackets with a file path, for 
            example:
    
          data source=C:\Elmah.sdf
    
             If the database file doesn't exist it is created automatically.
             You can also place the file in the App_Data folder, by using 
             the |DataDirectory| macro:
    
          <connectionStrings>
             <add name="..." connectionString="data source=|DataDirectory|\Elmah.sdf" />
          </connectionStrings>

      -->

      <!--
            Use to log errors to SQLite using ASP.NET 2.0. Set value of 
            connectionStringName attribute to the name of the connection 
            string settings to use from the <connectionStrings> section.
            
        <errorLog type="Elmah.SQLiteErrorLog, Elmah" connectionStringName="..." />
        
            The format of the connection string should be as follows:
            
        <connectionStrings>
          <add name="..." connectionString="data source=[path to the database file]" />
        </connectionStrings>
            
            Replace the content of the brackets with a file path, 
            for example:
            
        data source=C:\Elmah.db
            
            If the database file doesn't exist it is created automatically.
            To specify a path relative to and below the application root, 
            start with the root operator (~) followed by a forward slash 
            (/), as it is common in ASP.NET applications. For example:
            
        data source=~/App_Data/Error.db
        -->

        <!--
            Use to log errors into separate XML files that are stored on 
            disk at the path specified in the logPath attribute.

        <errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="..." />
        -->

        <!--
            Use to log errors to Oracle using ASP.NET 2.0. Set value of 
            connectionStringName attribute to the name of the connection 
            string settings to use from the <connectionStrings> section.
        
            The schemaOwner parameter is *optional*. Use this if the owner 
            of the ELMAH package (pkg_elmah$error) is not the same user that 
            you are using to connect to the database.
            
        <errorLog type="Elmah.OracleErrorLog, Elmah" 
            schemaOwner="xxx" />
            connectionStringName="..." />
        -->

        <!--
            Use to log errors to Oracle using ASP.NET 1.1 and adjust the 
            value of connectionString attribute to your settings.

            The schemaOwner parameter is *optional*. Use this if the owner 
            of the ELMAH package (pkg_elmah$error) is not the same user that 
            you are using to connect to the database.

        <errorLog type="Elmah.OracleErrorLog, Elmah" 
            schemaOwner="xxx" />
            connectionString="Data Source=xxxx;User ID=username;Password=password" />
        -->

        <!--
            Use to log errors to Microsoft Access using ASP.NET 1.x and 
            adjust the value of connectionString attribute to your settings.
            Use Elmah.mdb as your database.

        <errorLog type="Elmah.AccessErrorLog, Elmah" 
            connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Elmah.mdb" />
        -->

        <!--
            Use to log errors to Microsoft Access using ASP.NET 2.0. Set 
            value of connectionStringName attribute to the name of the 
            connection string settings to use from the <connectionStrings> 
            section. Use Elmah.mdb as your database.
        
        <errorLog type="Elmah.AccessErrorLog, Elmah" 
            connectionStringName="..." />
        -->
    
    <!--
            Use to log errors to PostgresSQL using ASP.NET 2.0. Set value of 
            connectionString to a valid Npgsql connection string.
        
        <errorLog type="Elmah.PgsqlErrorLog, Elmah" 
            connectionString="Server=...;Port=...;Database=...;User Id=...;Password=...;" />
        -->

        <!--
            Use to send error reports by e-mail and adjust the attributes
            according to settings desired. Note that only the from and
            to attributes are required. The rest are optional and may
            be removed. If the SMTP server does not require authentication,
            you MUST remove the userName and password attributes or
            set them to empty values (resulting in zero-length strings).
            If you are using ELMAH compiled for .NET Framework 1.x, then
            the to attribute may contain multiple recipient addresses,
            each of which must be delimited by a semi-colon (;). If you are 
            using ELMAH compiled for .NET Framework 2.0 or later, then the
            to attribute may contain multiple recipient addresses,
            each of which must be delimited by a comma (,).

        <errorMail 
            from="elmah@example.com" 
            to="admin@example.com" 
            subject="..."
            priority="Low|Normal|High"
            async="true|false"
            smtpPort="25"
            smtpServer="smtp.example.com" 
            useSsl="true|false"
            userName="johndoe"
            password="secret" 
            noYsod="true|false" />
        -->

        <!--
            Use to send short error messages to a Twitter account.
        <errorTweet 
            userName="johndoe" 
            password="secret" />
        -->

        <!--
            Use to prevent errors from being mailed or logged based on
            assertions. An assertion evaluates the current context, 
            resulting in a Boolean outcome. An error is filtered if the 
            assertion results in true. For a more detailed explanation, see:
            
            http://code.google.com/p/elmah/wiki/ErrorFiltering
            
            The example below will filter errors when any of the
            following conditions occur:
            
            - The status code is set to 404 
            - The root/base cause is System.IO.FileNotFoundException
            - The root/base cause is System.Web.HttpRequestValidationException
            - The user agent making the request identifies itself as "crawler"
            - The request is from the local machine
            
            The assertion below is expressed as a JScript expression and is 
            the most generic and simplest of the assertions to use if you 
            are familiar with the JavaScript language. It can work in Medium 
            and Full trust environments.
            
            NOTE: For versions of ELMAH 1.2 SP1 and earlier, use the 
            undocumented `$context` instead of `$` in the expression below 
            to workaround a bug when the expression is evaluated under full 
            trust environments. See issue #278[1] for more details.            
            
            [1] http://code.google.com/p/elmah/issues/detail?id=278

        <errorFilter>
            <test>
                <jscript>
                    <expression>
                    <![CDATA[
                    // @assembly mscorlib
                    // @assembly System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
                    // @import System.IO
                    // @import System.Web

                    $.HttpStatusCode == 404
                    || $.BaseException instanceof FileNotFoundException 
                    || $.BaseException instanceof HttpRequestValidationException
                    /* Using RegExp below (see http://msdn.microsoft.com/en-us/library/h6e2eb7w.aspx) */
                    || $.Context.Request.UserAgent.match(/crawler/i)                      
                    || $.Context.Request.ServerVariables['REMOTE_ADDR'] == '127.0.0.1' // IPv4 only
                    ]]>
                    </expression>
                </jscript>
            </test>
        </errorFilter>
        -->

    </elmah>

    <system.web>

        <httpModules>
            
            <!-- 
                Uncomment the entry below if all of the following are true: 
                1) you are using wildcard mapping in IIS 5.x
                2) you are not using .NET 1.0
                3) you are experiencing HTTP 404 errors when trying to view
                   elmah.axd or elmah.axd/detail?id=* etc
            -->
            <!--<add name="FixIIS5xWildcardMapping" type="Elmah.FixIIS5xWildcardMappingModule, Elmah" />-->

            <!-- 
                Uncomment the entry below if you are using MS AJAX v1.0.x.x
                and want to capture errors during Partial Postbacks
                NB This is not required for the version of AJAX shipped with 
                .NET Framework v3.5!
            -->
            <!--<add name="MsAjaxDeltaErrorLog" type="Elmah.MsAjaxDeltaErrorLogModule, Elmah" />-->

            <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
        
            <!-- 
                Uncomment the entries below if error mail reporting and 
                filtering is desired.
            -->
            
            <!--<add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" />-->
            <!--<add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" />-->
            <!--<add name="ErrorTweet" type="Elmah.ErrorTweetModule, Elmah" />-->

        </httpModules>
        
    </system.web>

    <!-- 
        The <system.webServer> section is required for running ELMAH under 
        Internet Information Services (IIS) 7. It is not necessary for 
        previous version of IIS.
        
        In general, it would be best to include it for all .NET Framework 
        2.0 and above configurations in order to have a more portable 
        solution between various versions of IIS.

        IIS 5.x, IIS 6 require the modules and handlers to be declared in 
        <system.web> whereas IIS 7 needs them declared here and complains if 
        they are in fact declared in <system.web>. Fortunately, the
        <validation validateIntegratedModeConfiguration="false" /> entry 
        tells IIS 7 not to worry about the modules and handlers declared in 
        <system.web>.
        
        If you only ever want to use IIS 7, then do the following:
        
        1. Remove handlers and modules from <system.web>
        2. Remove the <validation validateIntegratedModeConfiguration="false" /> element
    -->

    <system.webServer>
        <validation validateIntegratedModeConfiguration="false" />
        <modules>
            <add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" preCondition="managedHandler" />
            <add name="ErrorFilter" type="Elmah.ErrorFilterModule, Elmah" preCondition="managedHandler" />
            <add name="ErrorMail" type="Elmah.ErrorMailModule, Elmah" preCondition="managedHandler" />
            <add name="ErrorTweet" type="Elmah.ErrorTweetModule, Elmah" preCondition="managedHandler" />
        </modules>
    </system.webServer>

    <!-- 
        This entry provides the UI for the error log at elmah.axd below the 
        application path. For example, if your domain is www.example.com and 
        your application is at the root, then you can view the error log by
        visiting the following URL:
                
        http://www.example.com/elmah.axd

        IMPORTANT! To secure this location, consult the following notes:
                
        http://code.google.com/p/elmah/wiki/SecuringErrorLogPages
    -->

    <location path="elmah.axd">  
        <system.web>
            <httpHandlers>
                <add verb="POST,GET,HEAD" 
                     path="elmah.axd" 
                     type="Elmah.ErrorLogPageFactory, Elmah" />
            </httpHandlers>
            <!-- 
                The following entry denies unauthenticated users access to 
                the path from where ELMAH's handlers are accessible. If you 
                have role-based security implemented, you SHOULD consider 
                instead allowing access by a particular role (for example, 
                operators and administrators).
        
                IMPORTANT: If you change the path of the handler then you 
                MUST also change the path in the containing location tag 
                to be the same.

                For more information, see:
                http://code.google.com/p/elmah/wiki/SecuringErrorLogPages
            -->
            <authorization>
                <deny users="?" />  
            </authorization>  
        </system.web>
        <system.webServer>
            <handlers>
                <add name="ELMAH" 
                     verb="POST,GET,HEAD"
                     path="elmah.axd" 
                     type="Elmah.ErrorLogPageFactory, Elmah"
                     preCondition="integratedMode" />
            </handlers>
        </system.webServer>
    </location>  

</configuration>


下面,errorFilter過濾404錯誤

https://code.google.com/archive/p/elmah/wikis/ErrorFiltering.wiki

<elmah> 
    ... 
    <errorFilter> 
        <test> 
            <equal binding="HttpStatusCode" value="404" type="Int32" /> 
        </test> 
    </errorFilter> 
</elmah>

errorFilter範例
https://elmah.github.io/a/error-filtering/
https://elmah.github.io/a/error-filtering/examples/

留做參考。

(完)


沒有留言:

張貼留言