ASP.NET中用healthMonitor属性用法

 更新时间:2006年09月28日 00:00:00   作者:  

  在ASP.NET 2.0中,可以使用healthMonitoring属性监测事件。healthMonitoring属性是一个基于方法的provider,在这里可以构造自己的provider。利用healthMonitoring属性,我们可以诸如记录错语、成功的事件等,对不同的数据源,如事件日志,Sql Server甚至对于自己通过继承WebEventProvider类创建自己的providers。在此文章中,我打算介绍配置一个监测SqlServer错语的并且对某人的电子信箱地址发送邮件的web应用程序。首先,看一下在web.config中的healthMonitoring程序片段,在此可以建立将要用到的事件。

<healthMonitoring Enabled="true|false" heartBeatInterval="time interval">
 <bufferModes>... </bufferModes>
 <providers>... </providers>
 <eventMappings>... </eventMappings>
 <profiles>... </profiles>
 <rules>... </rules>
</healthMonitoring>

  如果看一下<healthMonitoring>元素,就可以判断是否可以将设置属性为有效或无效,也可以指定对WebHeaderBeatEvent被唤醒的时间间隔。healthMonitoring有5个孩子。

  bufferModes,在此可以定义一个Provider的缓冲区大小。

  Providers,在此说明处理事件的Providers。

  eventMappings, 在此可以画出与友好事件类型相关的事件名称。

  profiles, 在此定义一个可以用来配置事件的参数集集合。

  rules, 在此画出Providers的事件图。

  可以阅读更多含在VS 2550文档中的关于这些元素的东西。

  在继续之前,这里有一份含ASP.NET中的一些Providers清单:

System.Web.Management.MailWebEventProvider
System.Web.Management.SimpleMailWebEventProvider
System.Web.Management.TemplatedMailWebEventProvider
System.Web.Management.TraceWebEventProvider
System.Web.Management.EventLogWebEventProvider
System.Web.Management.SqlWebEventProvider
System.Web.Management.WmiWebEventProvider

  不需要解释这些,名字告诉我们它们是干什么的。还要提一下SqlWebEventProvider依靠Sql server而工作,它将事件存储在aspnet_Web_Event表。为了安装此数据库,必须运行位于framework文件夹中的aspnet_regsql.exe向导。

  现在,配置对Sql server provider有登录错语并且发送一个电子邮件而产生错误的程序。

  下面是一个使用SqlWebEventProvider和SimpleMailWebEventProvider来存错语事件的例子。

<healthMonitoring enabled="true" heartBeatInterval="0">
<bufferModes>
<add name="Critical Notification" maxBufferSize="100" maxFlushSize="20"urgentFlushThreshold="1" regularFlushInterval="Infinite" urgentFlushInterval="00:01:00" maxBufferThreads="1"/>

<add name="Analysis" maxBufferSize="1000" maxFlushSize="100" urgentFlushThreshold="100"
regularFlushInterval="00:05:00" urgentFlushInterval="00:01:00" maxBufferThreads="1"/>

</bufferModes>
<providers>

<add name="CriticalMailEventProvider" type="System.Web.Management.SimpleMailWebEventProvider, System.Web ..." from=info@nsquared2.net to=fnormen@hotmail.com priority="High" bodyHeader="Warning!"
bodyFooter="Please investigate ASAP." subjectPrefix="Action required." buffer="true" bufferMode="Critical Notification" maxEventLength="4096" maxSize="4096" maxMessagesPerNotification="1"/>

<add name="SqlWebEventProvider" type="System.Web.Management.SqlWebEventProvider, System.Web ..."
connectionStringName="LocalSqlServer" maxEventDetailsLength="1073741823" buffer="true"
bufferMode="Analysis"/>

</providers>

<eventMappings>

<add name="All Errors" type="System.Web.Management.WebBaseErrorEvent, System.Web ..."/>
<add name="Request Processing Errors" type="System.Web.Management.WebRequestErrorEvent, System.Web .../>

</eventMappings>

<profiles>

<add name="Default" minInstances="1" maxLimit="Infinite" minInterval="00:10:00"/>

</profiles>

<rules>

<add name="All Errors Default" eventName="All Errors" provider="SqlWebEventProvider" profile="Default"
minInterval="00:00:30"/>

<add name="Request Processing Errors" eventName="Request Processing Errors" provider="CriticalMailEventProvider" profile="Default"/>

</rules>

</healthMonitoring>

  在此例子中,使用Sql provider来记录所有错语事件,并且当Web请求错误事件被唤醒时使用mail provider来发送一个消息。

  这里有一些ASP .NET 2.0一起发布的事件:

System.Web.Management.WebBaseEvent
System.Web.Management.WebHeartBeatEvent
System.Web.Management.WebApplicationLifetimeEvent
System.Web.Management.WebRequestEvent
System.Web.Management.WebBaseErrorEvent
System.Web.Management.WebErrorEvent
System.Web.Management.WebRequestErrorEvent
System.Web.Management.WebAuditEvent
System.Web.Management.WebFailureAuditEvent
System.Web.Management.WebSuccessAuditEvent
System.Web.Management.WebManagementEvent
System.Web.Management.WebViewStateFailureAuditEvent
System.Web.Management.WebAuthenticationFailureAuditEvent
System.Web.Management.WebAuthenticationSuccessAuditEvent

  可以使用这些事件来对一个provider画图。也可以创建通过WebBaseEvent类继承来的自己的事件。

  为自动唤醒一个事件,可以使用WebBaseEvent类的唤醒方法:

try
{
 //....
}

catch(Exception e)
{
 if (HealthMonitoringManager.Enabled)
 {
  WebBaseEvent.Raise(new WebErrorEvent("My Error message", null, 5000, e));
 }
}

or:

if (HealthMonitoringManager.Enabled)
{
 WebErrorEvent event = new WebErrorEvent("My error message", null, 5000, e);
 event.Raise();
}
 

相关文章

  • ASP.NET MVC解决上传图片脏数据的方法

    ASP.NET MVC解决上传图片脏数据的方法

    这篇文章介绍了ASP.NET MVC解决上传图片脏数据的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-09-09
  • MVC 5 第一章 创建MVC 5 web应用程序

    MVC 5 第一章 创建MVC 5 web应用程序

    本章将讲述一些构建ASP.NET MVC 5 web application的一些基础知识, 通过本章学习,你应该能够掌握到构建MVC 5应用程序的基本步骤,并且通过展示一个完整的MVC 5 hello world应用程序了解MVC 5应用程序所带来的用户体验。
    2014-06-06
  • ASP.NET中利用存储过程实现模糊查询

    ASP.NET中利用存储过程实现模糊查询

    ASP.NET中利用存储过程实现模糊查询...
    2006-09-09
  • Equals和==的区别 公共变量和属性的区别小结

    Equals和==的区别 公共变量和属性的区别小结

    Equals 和==的区别 公共变量和属性的区别 总结一下。
    2009-11-11
  • 初识ASP.NET Mvc5+EF7的奇妙之旅

    初识ASP.NET Mvc5+EF7的奇妙之旅

    这篇文章主要和大家一起感受ASP.NET Mvc5+EF7的奇妙之旅,从旅程中认识了解.NET5框架,感兴趣的小伙伴们可以参考一下
    2015-09-09
  • ASP.NET.4.5.1+MVC5.0设置系统角色与权限(一)

    ASP.NET.4.5.1+MVC5.0设置系统角色与权限(一)

    这篇文章主要介绍了ASP.NET.4.5.1+MVC5.0设置系统角色与权限的部分内容,后续我们将继续讨论这个话题,希望小伙伴们喜欢。
    2015-01-01
  • ASP.NET Core实现中间件的几种方式

    ASP.NET Core实现中间件的几种方式

    这篇文章介绍了ASP.NET Core实现中间件的几种方式,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-08-08
  • Asp.Net MVC学习总结之过滤器详解

    Asp.Net MVC学习总结之过滤器详解

    本篇文章主要介绍了Asp.Net MVC学习总结之过滤器详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-03-03
  • 在.NET 6.0中自定义接口路由的方法

    在.NET 6.0中自定义接口路由的方法

    这篇文章主要介绍了在.NET 6.0中自定义接口路由,在本文,我们学习了如何使用终止中间件组件作为接口,并用将该接口映射到新的路由引擎,从而让我们的路由变得更加强大和灵活,需要的朋友可以参考下
    2023-04-04
  • ASP.NET Core中使用LazyCache的全过程

    ASP.NET Core中使用LazyCache的全过程

    这篇文章主要给大家介绍了关于ASP.NET Core中使用LazyCache的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03

最新评论