【问题标题】:Custom httpModule is not logging to Windows Logs自定义 httpModule 未记录到 Windows 日志
【发布时间】:2014-03-03 20:14:31
【问题描述】:

我在使用自定义 HTTPModule 类将消息记录到 EventViewer\WindowsLogs 时遇到问题。我已经尝试使用管理员权限运行 Visual Studio,我也尝试从 IIS 6.0 开始。它不会崩溃,只是不添加任何代码。下面是模块类和配置文件。

HttpModule

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Diagnostics;

namespace Chapter_V.HttpModules
{
    public class MyHttpModule : IHttpModule
    {
        public void Init(HttpApplication application)
        {
            application.AuthenticateRequest += new EventHandler(OnAuthentication);
        }

        private void OnAuthentication(object sender, EventArgs e)
        {
            string name = HttpContext.Current.User.Identity.Name;

            EventLog log = new EventLog();
            log.Source = "My First HttpModule";
            log.WriteEntry(name + " was authenticated !");
        }

        public void Dispose()
        {
        }
    }
}

web.config

<?xml version="1.0"?>
<configuration>
    <system.web>
      <httpModules>
        <add name="MyHttpModule" type="Chapter_V.HttpModules.MyHttpModule,ChapterV"/>

      </httpModules>
    </system.web>
  <system.webServer>
    <modules>

      <add name="MyHttpModule" type="Chapter_V.HttpModules.MyHttpModule,ChapterV"/>
    </modules>

  </system.webServer>
</configuration>

您对此问题有任何想法吗? (仅供学习使用)

【问题讨论】:

    标签: asp.net httpmodule


    【解决方案1】:

    如果您的应用程序池中的用户身份有权创建事件日志条目。 尝试运行以下脚本:

    eventcreate /ID 1 /L APPLICATION /T INFORMATION  /SO "My First HttpModule" /D "My first log"
    

    这将在 APPLICATION 事件日志下创建一个名为“My First HttpModule”的新事件源,作为 INFORMATION 事件类型。

    不确定确切原因,但事件日志中必须已经存在(创建)一个源,然后才能在代码中使用它。

    Source of information is here

    【讨论】:

    • 谢谢你,布伦特。该行在 APPLICATION 事件日志中添加了一个事件日志。我仍然不明白为什么它不想使用我的 Http 模块添加日志。我尝试使用 Visual Server Web 服务器 (CTRL + F5) 以及 IIS。但没有成功。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-23
    • 1970-01-01
    • 1970-01-01
    • 2016-08-29
    • 2019-11-15
    • 2023-03-12
    • 1970-01-01
    相关资源
    最近更新 更多