我正在使用 VS2005 C# 2.0 和 SQL Server 2005。
我指的是有关配置健康监控的本指南。
在指南的最后,Default.aspx和按钮的on_Click 上会有一个按钮,一条新记录将插入到我的SQL 表中。
但是,当按下我的按钮时,表中没有插入任何记录。
我不确定错误是什么,因为没有显示错误消息,所以我想唯一的方法是反复试验我出错的地方。
PS我无法编译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");
}
}
}
Default.aspx.cs
在主 Web 应用程序中:
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.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
。
我可以知道我的代码的哪一部分出错或遗漏了吗?