4月20
ASP.NET MVC程序日志记录工具ELMAH
复制适当版本的Elmah.dll到big目录下
配置web.config,添加如下节点
在站点根目录创建Error文件夹
一切完成。
访问异常页
访问elmah.axd
可以看到异常列表了
Error文件夹下面也有异常文件了
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="elmah">
<!--添加异常记录配置节点-->
<!-- 提示! ASP.NET1.x把requirePermission="false"去掉 -->
<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"/>
</sectionGroup>
</configSections>
<elmah>
<security allowRemoteAccess="0" />
<!--是否允许远程访问。0代表否、1代表是-->
<errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/Error" />
<!--记录到文件-->
<errorFilter>
<test>
<and>
<!--过滤400到499的错误 部分url转发可能会引发404错误
<greater binding="HttpStatusCode" value="399" type="Int32" />
<lesser binding="HttpStatusCode" value="500" type="Int32" />
-->
<equal binding="HttpStatusCode" value="404" type="Int32" />
<!--过滤404错误-->
</and>
</test>
</errorFilter>
</elmah>
<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>
</system.web>
</configuration>
elmah使用配置还是蛮简单的
有多种数据存储结构进行选择
更多信息可以查看其官方介绍
下面介绍下其它几种日记存储方式
1)把日志记录到SQL Server 2000 or 2005
<errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="此处为connectionString的name" /> 适应ASP.NET2.0
<errorLog type="Elmah.SqlErrorLog, Elmah" connectionString="Data Source=.;Initial Catalog=ELMAH;Trusted_Connection=True" /> 适应ASP.NET1.x
2) 把日志记录到SQLite 适应ASP.NET2.0 1.x不支持
<errorLog type="Elmah.SQLiteErrorLog, Elmah" connectionStringName="..." />
字符串格式如下:
<connectionStrings>
<add name="..." connectionString="data source=data source=C:\Elmah.db或~/App_Data/Error.db" />
</connectionStrings>
3)把日志记录到XML
<errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="E:\LogHaHa\" />
4)把日志记录到Oracle
<errorLog type="Elmah.OracleErrorLog, Elmah" schemaOwner="xxx" />
connectionStringName="..." />适应ASP.NET2.0
<errorLog type="Elmah.OracleErrorLog, Elmah" 适应ASP.NET1.x
schemaOwner="xxx" />
connectionString="Data Source=xxxx;User ID=username;Password=password" />
5)把日志记录到Access
<errorLog type="Elmah.AccessErrorLog, Elmah" connectionStringName="..." />适应ASP.NET2.0
<errorLog type="Elmah.AccessErrorLog, Elmah" 适应ASP.NET1.x
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Elmah.mdb" />
6)把日志记录到VistaDB
<errorLog type="Elmah.VistaDBErrorLog, Elmah"
connectionStringName="..." />
字符串格式:
<connectionStrings>
<add name="..." connectionString="Data Source='E:\Elmah.vdb3';Open Mode=NonExclusiveReadWrite;Pooling=True" providerName="VistaDB.Provider" />
</connectionStrings>
7)通过电子邮件
通过电子邮件发送错误报告。请注意,只有From和To属性是必需的。其余的都是可选的,并可能会被删除。如果服务器不要求身份验证,您必须删除的用户名 和密码属性或设置为空值(导致零长度字符串) 。如果您使用 NET Framework 1.x,那么到属性可能包含多个收件人地址,每个必须划定的分号( ; ) 。如果您使用的是NET Framework 2.0或更高版本,然后到属性可能包含多个收件人地址,每个必须划定逗号( , ) 。
<errorMail
from="lcs@dai8.net"
to="lcs@dai8.net"
subject="..."
async="true|false"
smtpPort="25"
smtpServer="smtp.dai8.net"
userName="lcskey"
password="dai8.net" />
复制适当版本的Elmah.dll到big目录下
配置web.config,添加如下节点
在站点根目录创建Error文件夹
一切完成。
访问异常页
访问elmah.axd
可以看到异常列表了
Error文件夹下面也有异常文件了
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="elmah">
<!--添加异常记录配置节点-->
<!-- 提示! ASP.NET1.x把requirePermission="false"去掉 -->
<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"/>
</sectionGroup>
</configSections>
<elmah>
<security allowRemoteAccess="0" />
<!--是否允许远程访问。0代表否、1代表是-->
<errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="~/Error" />
<!--记录到文件-->
<errorFilter>
<test>
<and>
<!--过滤400到499的错误 部分url转发可能会引发404错误
<greater binding="HttpStatusCode" value="399" type="Int32" />
<lesser binding="HttpStatusCode" value="500" type="Int32" />
-->
<equal binding="HttpStatusCode" value="404" type="Int32" />
<!--过滤404错误-->
</and>
</test>
</errorFilter>
</elmah>
<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>
</system.web>
</configuration>
elmah使用配置还是蛮简单的
有多种数据存储结构进行选择
更多信息可以查看其官方介绍
下面介绍下其它几种日记存储方式
1)把日志记录到SQL Server 2000 or 2005
<errorLog type="Elmah.SqlErrorLog, Elmah" connectionStringName="此处为connectionString的name" /> 适应ASP.NET2.0
<errorLog type="Elmah.SqlErrorLog, Elmah" connectionString="Data Source=.;Initial Catalog=ELMAH;Trusted_Connection=True" /> 适应ASP.NET1.x
2) 把日志记录到SQLite 适应ASP.NET2.0 1.x不支持
<errorLog type="Elmah.SQLiteErrorLog, Elmah" connectionStringName="..." />
字符串格式如下:
<connectionStrings>
<add name="..." connectionString="data source=data source=C:\Elmah.db或~/App_Data/Error.db" />
</connectionStrings>
3)把日志记录到XML
<errorLog type="Elmah.XmlFileErrorLog, Elmah" logPath="E:\LogHaHa\" />
4)把日志记录到Oracle
<errorLog type="Elmah.OracleErrorLog, Elmah" schemaOwner="xxx" />
connectionStringName="..." />适应ASP.NET2.0
<errorLog type="Elmah.OracleErrorLog, Elmah" 适应ASP.NET1.x
schemaOwner="xxx" />
connectionString="Data Source=xxxx;User ID=username;Password=password" />
5)把日志记录到Access
<errorLog type="Elmah.AccessErrorLog, Elmah" connectionStringName="..." />适应ASP.NET2.0
<errorLog type="Elmah.AccessErrorLog, Elmah" 适应ASP.NET1.x
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Elmah.mdb" />
6)把日志记录到VistaDB
<errorLog type="Elmah.VistaDBErrorLog, Elmah"
connectionStringName="..." />
字符串格式:
<connectionStrings>
<add name="..." connectionString="Data Source='E:\Elmah.vdb3';Open Mode=NonExclusiveReadWrite;Pooling=True" providerName="VistaDB.Provider" />
</connectionStrings>
7)通过电子邮件
通过电子邮件发送错误报告。请注意,只有From和To属性是必需的。其余的都是可选的,并可能会被删除。如果服务器不要求身份验证,您必须删除的用户名 和密码属性或设置为空值(导致零长度字符串) 。如果您使用 NET Framework 1.x,那么到属性可能包含多个收件人地址,每个必须划定的分号( ; ) 。如果您使用的是NET Framework 2.0或更高版本,然后到属性可能包含多个收件人地址,每个必须划定逗号( , ) 。
<errorMail
from="lcs@dai8.net"
to="lcs@dai8.net"
subject="..."
async="true|false"
smtpPort="25"
smtpServer="smtp.dai8.net"
userName="lcskey"
password="dai8.net" />

ASP.NET MVC如何做程序日志
雨夜





