Enabling and configuring error logging
So far, if an uncaught exception bubbled up in the application no error would be shown by ELMAH, since it's not been configured for intercepting errors yet.
Error logging is achieved via an HTTP module called ErrorLogModule which intercepts uncaught exceptions at the application level and logs them into a persistence store.
To enable this behavior, the ErrorLogModule needs to be declared in the httpModules node of the Web.config file, as shown in the snippet below.
<system.web>
<httpModules>
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
</httpModules>
</system.web>
<system.web>
<httpModules>
<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah" />
</httpModules>
</system.web>
Error logging is performed by means of one out of several concrete inheritors of the base ErrorLog class.
As introduced before, by default errors are logged in central memory via the MemoryErrorLog class, thus making them volatile due to application restarts, but other, non volatile storages are available, and customized providers can be created by inheriting the base ErrorLog class.
Once the logging module has been enabled, uncaught exceptions should end up being intercepted by ELMAH and then available for viewing in the default reporting page.
Figure 5: Default error view containing errors

附件:
您所在的用户组无法下载或查看附件ELMAH ships with four error log providers, listed in the following table, together with their compatibility with Medium Trust level and .NET version.
ErrorLog implementationDescriptionMedium Trust (2.0 only)1.x2.0
| MemoryErrorLog | Logs errors in RAM. | Yes | Yes | Yes |
| XmlFileErrorLog | Logs errors in multiple XML files. | Yes | Yes | Yes |
| SQLiteErrorLog | Logs errors in SQLite. | No | No | Yes |
| SqlErrorLog | Logs errors in Microsoft SQL Server. | Yes | Yes | Yes |
Note: XmlFileErrorLog provider works in Medium Trust as long as it is configured to store XML error files into a folder under the root folder of the application. Medium Trust, in fact, doesn't allow access to the file system except for the application directory and its subfolders.
Choosing and configuring a storage provider is accomplished with the errorLog child node of the main elmah section group in the Web.config file. The following paragraphs instruct how to enable and configure each error provider.
Storing errors in memory
This is the default option, and therefore requires no additional configuration. By default, the maximum number of errors the provider will store is set to 15. This can be changed via configuration, but it's however limited to a maximum of 500 to prevent memory issues. The snippet below shows how to set the size of the error store.
<elmah>
<errorLog type="Elmah.MemoryErrorLog, Elmah" size="50" />
</elmah>
<elmah>
<errorLog type="Elmah.MemoryErrorLog, Elmah" size="50" />
</elmah>
Storing errors in XML files
The XmlFileErrorLog enables error entries to be stored into XML files. Each error gets its own file containing all of its details.
The following snippet shows how to configure the XmlFileErrorLog class.
<elmah>
<errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/App_Data" />
</elmah>
<elmah>
<errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/App_Data" />
</elmah>
The logPath attribute must point to an existing location on the file system, on which the identity the ASP.NET worker process runs with needs to have write permission.
As noted before, logging to XML files works in Medium Trust level only if the folder the logPath points to is a subfolder of the application.
The syntax accepted by the logPath attribute is either an absolute path on the file system, or the new application relative syntax ~/ introduced by ASP.NET 2.0. Relative paths are not supported in ASP.NET 1.1.
Once exceptions are thrown the target folder gets populated as shown in the following figure.
Figure 6: Xml errors

附件:
您所在的用户组无法下载或查看附件Storing errors in SQLite
SQLite is an open source lightweight embedded relational database engine. A managed and standalone version written for the .NET environment is available under public domain license. ELMAH has recently adopted the managed SQLite provider as an alternative storage mechanism to SQL Server, although it comes with a couple of limitations. The class which implements logging via SQLite is called SQLiteErrorLog.
Note: The SQLiteErrorLog class requires ASP.NET 2.0 and Full Trust level.
The snippet below shows how to configure ELMAH to use the SQLite provider.
<connectionStrings>
<add name="Elmah.SQLite" c />
</connectionStrings>
<elmah>
<errorLog type="Elmah.SQLiteErrorLog, Elmah" connecti />
</elmah>
<connectionStrings>
<add name="Elmah.SQLite" c />
</connectionStrings>
<elmah>
<errorLog type="Elmah.SQLiteErrorLog, Elmah" connecti />
</elmah>
As shown above it leverages the new connectionStrings section available in ASP.NET 2.0. The data source attribute can point to either an absolute or application relative path, on which the account running ASP.NET must have edit (which include both write and read) permissions. A database file name needs to be specified as well, by convention with the .db extension, but it doesn't need to exist ahead. If it doesn't, the provider automatically creates it.
Storing errors in SQL Server
The last alternative for error storage is Microsoft SQL Server via the SqlErrorLog class. ELMAH supports both SQL Server 2000 and 2005, though it doesn't leverage new features offered by SQL Server 2005 to maintain backwards compatibility, and both ASP.NET 1.x and 2.0.
Before enabling SQL Server error logging a database needs to be created. ELMAH will then take care of creating a table with the correct schema where the errors will be stored, but the database should already be there.
Due to the availability of the new connectionStrings section in ASP.NET 2.0 the configuration varies depending on the version of ASP.NET the application is running on.
The snippet below shows how to configure the SqlErrorLog in ASP.NET 1.x, along with a sample connection string which, of course, needs to be adapted to reflect custom needs. It presumes that a database called ELMAH has been created and is accessible according to the parameters specified in the connection string.
<elmah>
<errorLog type="Elmah.SqlErrorLog, Elmah"
c />
</elmah>
<elmah>
<errorLog type="Elmah.SqlErrorLog, Elmah"
c />
</elmah>
The following snippet, instead, shows how the same configuration can be achieved under ASP.NET 2.0, but note that this is just an improvement, the previous syntax will work as well.
<connectionStrings>
<add name="Elmah.Sql"
c />
</connectionStrings>
<elmah>
<errorLog type="Elmah.SqlErrorLog, Elmah" connecti />
</elmah>
<connectionStrings>
<add name="Elmah.Sql"
c />
</connectionStrings>
<elmah>
<errorLog type="Elmah.SqlErrorLog, Elmah" connecti />
</elmah>
Choosing among error log providers
Given the several storage mechanisms provided by ELMAH, here's a brief guide which explains how to choose between them and what scenarios they were built to tackle.
Storing errors in memory is fast and suitable as long as they don't need to survive application restarts. Since application resets are often unpredictable this log provider is usually employed for testing and troubleshooting only; in cases when everything is going wrong it's good to remove dependencies on external services and the MemoryErrorLog might help reducing moving parts. Moreover, as the error number grows their memory occupation might grow as well at unwanted levels. For these reasons most of the times a persistent storage is preferable.
Storing errors into XML files is a nice solution in that it's compatible with both ASP.NET 1.x and 2.0 and works under Medium Trust; furthermore, it doesn't require any database engines, like SQL Server, which usually come at additional costs in hosting plans. However, although very versatile, this solution is not as performant as relational database alternatives, and might not be suitable for larger applications. In case this turned out to be the only available choice a smart way of keeping it run smoothly is limiting the growth of the number of XML files by running a scheduled task to archive the old logs and clean up the folder.
The SQLite storage is a good choice in terms of speed and cost because it is known to be very fast and doesn't require any additional service to be provided by the web application hoster. Its main limitations are that it is supported only in ASP.NET 2.0 and needs Full Trust to work.
Finally, whenever SQL Server 2000 or 2005 are available they represent definitely the best choice for every application size and complexity. Speed, performance, data resilience and many others are intrinsic features of relational database engines. In addition, the SqlErrorLog provider works on both ASP.NET 1.x and 2.0 as well as in Medium Trust.
Furthermore, since being the only server-based error logging provider it features the capability of storing exceptions coming from different applications. This means that several applications can point to the same database and ELMAH will be able to store their exceptions in a way that each application will only see and be notified of its own exceptions. This is accomplished by storing the name of each application, which is unique, into a field of the table where ELMAH logs the errors and consequently filtering on the same field whenever a query is performed.
This is especially useful in case an hosting provider or an operations environment within an enterprise want to provide a centralized error logging mechanism out of the box without wasting too many server resources and without requiring each user to have its own database for that. They would expose a public connection string pointing to a database every user can connect to just for error logging.