0

I have a problem with logging an message to EventViewer\WindowsLogs using a custom HTTPModule class. I've already try to run Visual Studio with admin rights, I also tried from IIS 6.0. It doesn't crash, it just doesn't add any code. Below it's the module class and the config file.

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>

Do you have any ideea about this issue? (this is only for study purposes)

Brent Mannering
  • 2,316
  • 20
  • 19
Cosmin
  • 26
  • 2

1 Answers1

0

Provided the user identity on your application pool has the rights to create event log entries. Try running the following script:

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

This will create a new event source named "My First HttpModule" under APPLICATION event log as INFORMATION event type.

Not sure of the exact reasons why, but a source must already exist (be created) in the event log before it can be used in code.

Source of information is here

Community
  • 1
  • 1
Brent Mannering
  • 2,316
  • 20
  • 19
  • Thank you, Brent. That line added an event log in the APPLICATION event log. I still don't get it why it doesn't want to add a log using my Http Module. I try it with the Visual Server web server (CTRL + F5) and also from IIS. But no success. – Cosmin Mar 05 '14 at 20:36