سؤال

I am using VS2005 C# 2.0 and SQL Server 2005.

I am referring to this guide on configuring Health Monitoring.

At the end of the guide, there will be a button on Default.aspx and on_Click of the button, a new record will be inserted into my SQL table.

However, when my button is pressed, there is no record inserted in the table.

I am unsure of what the error as there are no error messages shown, so I guess the only way is to trial and error on where I have gone wrong.

P.S. I am unable to compile MyWebEvents class library because there is no output. In my main web application, I added reference dll from the bin folder of my MyWebEvents project file. Is the DLL i referenced valid for use, or is there a step I missed in compiling?

Below are the codes which I ran, with reference to the microsoft website:


MyCriticalEvent.cs in MyWebEvents class library:

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 in Main Web Application:

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 in Main Web Applicatiion:

<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>

enter image description here


Table in my database:

enter image description here


Error raised in the Event log if SQL Connection is broken:

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


Problem Summary: When button1 on Default.aspx.cs is clicked, no log records were inserted in the aspnet_WebEvent_Event table.

May I know which part of my code have I gone wrong or missed out?

هل كانت مفيدة؟

المحلول

If you want to see it right away try disabling buffering

<add name="MySqlWebEventProvider" type="System.Web.Management.SqlWebEventProvider" connectionStringName="MySqlConnection" maxEventDetailsLength="1073741823" buffer="true" bufferMode="Logging"/>

change to

<add name="MySqlWebEventProvider" type="System.Web.Management.SqlWebEventProvider" connectionStringName="MySqlConnection" maxEventDetailsLength="1073741823" buffer="false" bufferMode="Logging"/>

This should stop the delay caused by buffering and you should see the rows appear right away.

Plus you may want to decrease your minInterval on the profiles to something quick like "00:00:01" :)

You can read more about buffering here:

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

On a live system it maybe a good idea to leave buffering in place in case you think your SQL server might get overloaded with lots of events firing.

OR

Ive found i can force it save to the log by:

1 : start your project in visual studio 2 : click the button 3 : Stop the project and then start it again 4 : Check in the database you should see an event logged

نصائح أخرى

Try temporarily replacing the SqlWebEventProvider with a TraceWebEventProvider to see if the problem lies in ASP.NET WebEvents or in accessing the SQL database.

The TraceWebEventProvider should avoid permission related problems.

Updated

web.config:

<system.web>
    ...
    <providers>
        <clear/>
            <add name="TraceEventProvider"
        type="System.Web.Management.TraceWebEventProvider, 
          System.Web"
        buffer="false"
        bufferMode=""
        maxEventLength="4096"
        maxSize="4096"
        maxMessagesPerNotification="1"
    />
    </providers>
    ...
    <rules>
        <clear/>
        <add name="All Audits Default" eventName="All Audits" provider="TraceEventProvider" profile="Audit Logs"/>
        <add name="All Errors Default" eventName="All Errors" provider="TraceEventProvider" profile="Error Logs"/>
    </rules>
    ...
</system.web>
...
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top