c# - 使用健康监控的 ASP.NET Web 应用程序日志记录不起作用

标签 c# asp.net visual-studio logging health-monitoring

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

我指的是 this有关配置健康监控的指南。

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

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

我不确定错误是什么,因为没有显示错误消息,所以我想唯一的方法是反复试验我哪里出错了。

P.S. 我无法编译 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.csMain 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.configMain 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


我的数据库中的表:

enter image description here


如果 SQL 连接断开,事件日志中会出现错误:

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


问题总结:当点击 Default.aspx.cs 上的 button1 时,aspnet_WebEvent_Event< 中没有插入任何日志记录 表。

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

最佳答案

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

<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 : 在 vi​​sual studio 中开始你的项目 2:点击按钮 3:停止项目,然后重新启动 4:检查数据库你应该看到记录的事件

关于c# - 使用健康监控的 ASP.NET Web 应用程序日志记录不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9089787/

相关文章:

c# - 在 C#.NET 中使用 USB PS2 手控器

c# - 如何在特定页面类型而不是父页面上要求 SSL

asp.net - 将所有页面内容放入 Web 用户控件中是一个很好的做法吗?

c# - 什么时候值得压缩 ViewState?

c++ - .rc 文件中的 TEXTINCLUDE 部分重复

javascript - 模拟键盘输入/将字符串插入文本区域(adwords)

c# - CultureInfo 转换 (C#)

asp.net - 为什么每个人都不喜欢ViewData?

c# - 是否可以在 VS 2013 中使用 HTML 设计器来构建 MVC Web 应用程序?

visual-studio - 在 Visual Studio (2008) 中,有没有办法在另一个自定义文件上创建自定义依赖文件?