ASP.NET EventLog Access

Writing to the Event Log in ASP.NET is a piece of cake. The following is a static class that I have used time and time again to simply encapsulate the event logging writing code:

  1. public class EventLogging
  2.     {
  3.         public static void WriteToEventLog(EventLogEntryType logType, string message, string source)
  4.         {
  5.             if (!EventLog.SourceExists(source))
  6.                 EventLog.CreateEventSource(source, "Application");
  8.             EventLog.WriteEntry(source, message, logType);
  9.         }
  11.         public static void WriteErrorToEventLog(string message, string source)
  12.         {
  13.             WriteToEventLog(EventLogEntryType.Error, message, source);
  14.         }
  16.         public static void WriteInfoToEventLog(string message, string source)
  17.         {
  18.             WriteToEventLog(EventLogEntryType.Information, message, source);
  19.         }
  20.     }

This class works great in a console application where you can run as administrator. However, when you try to run from an ASP.NET application for the first time on a new event source, most often you will get permission denied:

Requested Registry Access Is Not Allowed

For security purposes the ASP.NET application will run under a limited role (at least it should be). This limited role prevents the application from having permission to create a new event log source. The solution is simple… you can manually add in an event log source which will prevent the application from trying to create one (all apps can write a log, just not create an event source).

Here are some steps, however, be warned this is manually changing some registry settings – make sure you backup and complete at your own risk:

  1. Click Start, and then click Run.
  2. In the Open text box, type regedit.
  3. Locate the following registry subkey:
  4. Right-click the Application subkey, point to New, and then click Key.
  5. Type TEST for the key name.
  6. Close Registry Editor.

Check out some further MSDN notes and explanations here:

Leave a Reply