How To: write to EventLog from .NET 2.0 web app?

  • Thread starter Rob R. Ainscough
  • Start date
R

Rob R. Ainscough

I'm having difficulty writing anything to the web servers event log from my
web app located on that server.

Putting this in my web apps web.config did NOT help - immediate causes and
error in my web app before it displays the default page.

<healthMonitoring>
<rules>
<add name="All Events" eventName="All Events" provider="EventLogProvider"
profile="Critical" />
</rules>
</healthMonitoring>


How can I write my own events to the EventLog?
 
G

Guest

Rob,
This is all it takes:

protected void Page_Load(object sender, EventArgs e)
{
System.Diagnostics.EventLog.WriteEntry("MyApp", "Howdy!");

}

Peter
 
R

Rob R. Ainscough

I tried that and nothing happens on my web server -- Event Viewer shows no
entries at all for MyApp.

If I run the exact same code locally via VS 2005 it works (as in I see the
entries in the Event Viewer Applications) -- so I'm guess I need something
more than just code? Security settings and/or web.config or something?

Any suggestions?
 
G

Guest

The account the web page is running under needs permission to write to the
event log. You can either grant additional permissions to the default aspnet
account (if that's how your app is running) or use the <impersonation ="true
....
web.config element to have your app run under a more privileged account.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
 
R

Rob R. Ainscough

Yes this a security issue for sure:

System.Security.SecurityException: Requested registry access is not allowed.
at System.ThrowHelper.ThrowSecurityException(ExceptionResource resource) at
Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable) at
System.Diagnostics.EventLog.CreateEventSource(EventSourceCreationData
sourceData) at System.Diagnostics.EventLog.VerifyAndCreateSource(String
sourceName, String currentMachineName) at
System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType
type, Int32 eventID, Int16 category, Byte[] rawData) at
System.Diagnostics.EventLog.WriteEntry(String source, String message,
EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData) at
System.Diagnostics.EventLog.WriteEntry(String source, String message) at
..... The Zone of the assembly that failed was: MyComputer

should I impersonate administrator? I thought using impersonation was a
security problem?
 
Q

Q. John Chen

No. Never. It would be a security issue as you mentioned.

The problem in creating the EventLog entry is because the ASP Account
can not create the EVENT SOURCE. As long as you created the event
source in the system. WriteEntry() will be ok if it writes to an
existing event source.

To Create an event souce:

1. In registry under
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Application\
add a subkey (MyApp in your case).

2. Or, Try to create the above mentioned registry key automatically
such as in the installer or a setup utility.


John
 
R

Rob R. Ainscough

John,

Thanks for the response. I created the MyApp entry and added read/write
permissions for ASP.NET and NETWORK SERVICES accounts on the reg key
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\Application\
-- that solved the problem.

I don't like to do impersonate in web.config, just too risky IMHO.

Rob.
 
Q

Q. John Chen

Rob,

Glad it helped. I just thought that you don't have to add permissions
for ASP.NET and NETWORK SERVICE.

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top