我正在使用VS2005 C# 2.0和SQL Server 2005。
我参考了这个配置Health Monitoring的指南。
在指南的末尾,会有一个按钮在Default.aspx页面上,当点击该按钮时,将会在我的SQL表中插入一条新记录。
但是,当我按下按钮后,并没有在表中插入任何记录。
我不确定出现了什么错误,因为没有显示任何错误消息,所以我猜想唯一的方法就是试错,找出哪里出了问题。
P.S. 我无法编译MyWebEvents
类库,因为没有输出。在我的主Web应用程序中,我从MyWebEvents项目文件的bin文件夹中添加了引用dll。我引用的DLL是否可用,或者我漏掉了编译的步骤?
以下是我运行的代码,参考了微软网站:
MyCriticalEvent.cs
位于MyWebEvents类库中:
namespace MyWebEvents
{
public class MyCriticalEvent : WebAuditEvent
{
private string userID;
private string authType;
private bool isAuthenticated;
public MyCriticalEvent(string msg, object eventSource, int eventCode)
: base(msg, eventSource, eventCode)
{
// Obtain the HTTP Context and store authentication details
userID = HttpContext.Current.User.Identity.Name;
authType = HttpContext.Current.User.Identity.AuthenticationType;
isAuthenticated = HttpContext.Current.User.Identity.IsAuthenticated;
}
public MyCriticalEvent(string msg, object eventSource, int eventCode,
int eventDetailCode)
: base(msg, eventSource, eventCode, eventDetailCode)
{
userID = HttpContext.Current.User.Identity.Name;
authType = HttpContext.Current.User.Identity.AuthenticationType;
isAuthenticated = HttpContext.Current.User.Identity.IsAuthenticated;
}
public override void FormatCustomEventDetails(WebEventFormatter formatter)
{
base.FormatCustomEventDetails(formatter);
formatter.AppendLine("User ID: " + userID);
formatter.AppendLine("Authentication Type: " + authType);
formatter.AppendLine("User Authenticated: " +
isAuthenticated.ToString());
formatter.AppendLine("Activity Description: Critical Operation");
}
}
}
主Web应用程序中的Default.aspx.cs
:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
MyCriticalEvent testEvent = new MyCriticalEvent(
"Critical Operation Performed",
this,
WebEventCodes.WebExtendedBase + 1);
testEvent.Raise();
}
}
主 Web 应用程序中的 Web.config
文件:
<configuration>
<appSettings/>
<connectionStrings>
<add name="MySqlConnection" connectionString="Data Source=<IP>;Initial Catalog=<DB NAME>;Persist Security Info=True;User ID=<admin username>;Password=<admin password>" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<healthMonitoring enabled="true" heartbeatInterval="0">
<bufferModes>
<clear/>
<add name="Logging" maxBufferSize="1000" maxFlushSize="200" urgentFlushThreshold="800" regularFlushInterval="00:05:00" urgentFlushInterval="00:01:00" maxBufferThreads="1"/>
</bufferModes>
<providers>
<clear/>
<add name="MySqlWebEventProvider" type="System.Web.Management.SqlWebEventProvider" connectionStringName="MySqlConnection" maxEventDetailsLength="1073741823" buffer="true" bufferMode="Logging"/>
</providers>
<eventMappings>
<clear/>
<add name="All Audits" type="System.Web.Management.WebAuditEvent" startEventCode="0" endEventCode="2147483647"/>
<add name="All Errors" type="System.Web.Management.WebBaseErrorEvent" startEventCode="0" endEventCode="2147483647"/>
</eventMappings>
<profiles>
<clear/>
<add name="Audit Logs" minInstances="1" maxLimit="Infinite" minInterval="00:00:15"/>
<add name="Error Logs" minInstances="1" maxLimit="Infinite" minInterval="00:00:15"/>
</profiles>
<rules>
<clear/>
<add name="All Audits Default" eventName="All Audits" provider="MySqlWebEventProvider" profile="Audit Logs"/>
<add name="All Errors Default" eventName="All Errors" provider="MySqlWebEventProvider" profile="Error Logs"/>
</rules>
</healthMonitoring>
</system.web>
</configuration>
我的数据库中有一个表:
如果SQL连接断开,事件日志会记录以下错误:
The following exception was thrown by the web event provider 'MySqlWebEventProvider' in the application '/TestLogSite' (in an application lifetime a maximum of one exception will be logged per provider instance):
问题概述: 当点击Default.aspx.cs中的button1
时,没有将日志记录插入aspnet_WebEvent_Event
表。
请问我哪部分代码出了问题或者是遗漏了?
aspnet_regsql.exe -E -S <ServerName> -A w
。数据库中是否创建了任何表格? - Amar Palsapureaspnet_regsql.exe -S <IP> -U <username> -P <password> -d <DB> -A w
命令。表已经被创建。我也更新了我的帖子,并附上了截图。 - gymcode