使用Health Monitoring的ASP.NET Web App日志记录无法正常工作

gym*_*ode 12 c# asp.net logging health-monitoring visual-studio

我正在使用VS2005 C#2.0和SQL Server 2005.

我指的是配置健康监控的指南.

在指南的末尾,按钮的Default.aspx和on_Click 上会有一个按钮,新的记录将被插入到我的SQL表中.

但是,按下我的按钮时,表中没有插入记录.

我不确定错误是什么,因为没有显示错误消息,所以我想唯一的方法是试错我出错的地方.

PS我无法编译MyWebEvents类库,因为没有输出.在我的主Web应用程序中,我从myWebEvents项目文件的bin文件夹中添加了引用dll .我引用的DLL是否有效,或者在编译时是否遗漏了一个步骤?

以下是我运行的代码,参考microsoft网站:


MyCriticalEvent.csMyWebEvents类库中:

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");
    }
} 
}
Run Code Online (Sandbox Code Playgroud)

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();
}
}
Run Code Online (Sandbox Code Playgroud)

Web.config主要Web应用程序中:

<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>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


我的数据库中的表:

在此输入图像描述


如果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):


问题摘要:当button1Default.aspx.cs被点击时,没有日志记录被插入aspnet_WebEvent_Event表中.

我可以知道我的代码中哪些部分出错了或错过了吗?

Ric*_*ley 3

如果您想立即看到它,请尝试禁用缓冲

<add name="MySqlWebEventProvider" type="System.Web.Management.SqlWebEventProvider" connectionStringName="MySqlConnection" maxEventDetailsLength="1073741823" buffer="true" bufferMode="Logging"/>
Run Code Online (Sandbox Code Playgroud)

改成

<add name="MySqlWebEventProvider" type="System.Web.Management.SqlWebEventProvider" connectionStringName="MySqlConnection" maxEventDetailsLength="1073741823" buffer="false" bufferMode="Logging"/>
Run Code Online (Sandbox Code Playgroud)

这应该会停止缓冲造成的延迟,您应该会看到行立即出现。

另外,您可能希望将配置文件上的 minInterval 减少到类似“00:00:01”这样的快速值:)

您可以在此处阅读有关缓冲的更多信息:

http://msdn.microsoft.com/en-us/library/ms178708%28v=vs.80%29.aspx

在实时系统上,保留缓冲可能是个好主意,以防您认为 SQL 服务器可能因大量事件触发而过载。

或者

我发现我可以通过以下方式强制将其保存到日志中:

1:在 Visual Studio 中启动项目 2:单击按钮 3:停止项目,然后再次启动 4:检查数据库,您应该会看到记录的事件