使用运行状况监控的 ASP.NET Web 应用程序日志记录不起作用

发布于 2025-01-02 01:42:04 字数 5219 浏览 4 评论 0原文

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

我指的是有关配置运行状况的指南监控。

在指南的最后,Default.aspx 上将有一个按钮,单击该按钮后,一条新记录将插入到我的 SQL 表中。

但是,当按下我的按钮时,表中没有插入任何记录。

我不确定错误是什么,因为没有显示错误消息,所以我想唯一的方法就是在我出错的地方进行尝试和错误。

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

下面是我运行的代码,参考了微软网站:


MyWebEvents类库中的MyCriticalEvent.cs

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 应用程序中的 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 连接中断,则事件日志中引发错误:

应用程序“/TestLogSite”中的 Web 事件提供程序“MySqlWebEventProvider”引发了以下异常(在应用程序生命周期中最多为每个提供程序实例将记录一个异常):


问题摘要:当单击 Default.aspx.cs 上的 button1 时,不会日志记录已插入 aspnet_WebEvent_Event 表中。

我可以知道我的代码的哪一部分出错或遗漏了吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

失而复得 2025-01-09 01:42:04

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

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

更改为

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

这应该会停止缓冲引起的延迟,并且您应该立即看到行出现。

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

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

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

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

或者

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

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

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

李白 2025-01-09 01:42:04

尝试暂时将 SqlWebEventProvider 替换为 TraceWebEventProvider 查看问题是否出在 ASP.NET WebEvents 或访问 SQL 数据库上。

TraceWebEventProvider 应避免与权限相关的问题。

更新

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

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>
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文