Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing to an event log in ASP.NET on Windows Server 2008 IIS7

Tags:

asp.net

iis

iis-7

I'm trying to use log4net to write to a customer event log under IIS7 on Windows Server 2008 SP1. However, account doesn't seem to have access to write to the event log. Does anyone have any suggestions?

like image 692
mhenderson Avatar asked Apr 03 '09 00:04

mhenderson


People also ask

Where is the event log in Windows Server 2008?

To access the Event Viewer in Windows 7 and Windows Server 2008 R2: Click Start > Control Panel > System and Security > Administrative Tools. Double-click Event Viewer. Select the type of logs that you wish to review (ex: Windows Logs)

How do you write an event log?

The Write-EventLog cmdlet writes an event to an event log. To write an event to an event log, the event log must exist on the computer and the source must be registered for the event log. The cmdlets that contain the EventLog noun (the EventLog cmdlets) work only on classic event logs.

How do I configure Windows event log?

In Windows, you can adjust Event Viewer settings by right-clicking the log and clicking Properties. You can adjust the following Event Log settings: Maximum log size. Overwrite events as needed.

How do I use Microsoft event log?

Open "Event Viewer" by clicking the "Start" button. Click "Control Panel" > "System and Security" > "Administrative Tools", and then double-click "Event Viewer" Click to expand "Windows Logs" in the left pane, and then select "Application". Click the "Action" menu and select "Save All Events As".


2 Answers

The problem is probably your event source. You have to create an event source before you can write to the event log (if you don't, the Event log object tries to create one for you "automagically" the first time you write to the log).

You have to have hightened permissions to create an event log source. In some of my web apps, I have put the code to create the event source into my setup (setup runs as admin, so I'm always guaranteed to be able to create the source).

You just have to create the source once. After that, your ASP.Net app should have sufficient permissions to write entries specifying the source (or sources) that you created.

You can use an EventLogInstaller in your setup to create the source, or you could just write a little utility to call EventLog.CreateEventSource() as an admin.

I'll show you both ways:

  // You would do this one from within an Installer class in a setup:         private void InstallEventLog()         {             EventLogInstaller logInstaller;              //Create an instance of an EventLogInstaller.             logInstaller = new EventLogInstaller();              //Set the source name of the event log.             logInstaller.Source = "TheEventSourceName";             Installers.Add(logInstaller);         }   

Method 2: just call CreateEventSource once as an admin (you could put the following code into a console app, for example, and run the console app as admin

  EventLog.CreateEventSource("TheSourceName", "Application");  

Bonus: If you have Powershell installed on your server, you can do it from the Powershell command prompt: (Make sure you are running Powershell as an admin)

  [system.Diagnostics.EventLog]::CreateEventSource("SourceName", "Application")  

Hop that helps

like image 135
JMarsch Avatar answered Oct 03 '22 18:10

JMarsch


Give the ASPNET permission to the event log.

Run -> regedit - > Browse to

HKEY_LOCAL_MACHINE    \SYSTEM       \CurrentControlSet          \Services             \Eventlog 

Right click select permissions and give the ASPNET account full control

like image 39
Michael Kniskern Avatar answered Oct 03 '22 17:10

Michael Kniskern