Writing to the EventLog from an ASP.NET page

Last post 07-04-2007 1:38 AM by jasonken. 7 replies.

Sort Posts:

  • Writing to the EventLog from an ASP.NET page

    04-21-2006, 11:23 AM
    • Participant
      804 point Participant
    • jbeall
    • Member since 07-06-2005, 12:46 PM
    • Posts 253
    We've been trying to do this, and we're getting the following exception:

    System.Security.SecurityException was unhandled by user code
      Message="The source was not found, but some or all event logs could not be searched.  Inaccessible logs: Security."
      Source="System"
      StackTrace:
           at System.Diagnostics.EventLog.FindSourceRegistration(String source, String machineName, Boolean readOnly)
           at System.Diagnostics.EventLog.SourceExists(String source, String machineName)
           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 message, EventLogEntryType type)
           at HSLDA.EEntry.Common.Request.Request.Save() in D:\NetProjects2005\EEntry\EEntryRequest\Request.vb:line 169
           at _Default.SendApplication() in d:\dev\EEntryGateway\Default.aspx.cs:line 258
           at _Default.Page_Load(Object sender, EventArgs e) in d:\dev\EEntryGateway\Default.aspx.cs:line 41
           at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
           at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
           at System.Web.UI.Control.OnLoad(EventArgs e)
           at System.Web.UI.Control.LoadRecursive()
           at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)


    I assume this is because the ASP.NET process is not allowed to write to the event logs.  So, should I:

    1) Change permissions so that the ASP.NET process can write to the logs?  If so, how do I go about that?
    2) Use some other method for writing to the event logs?
    3) Run as a different user that has permissions to write to the event log?  This seems like an incorrect solution...
    4) Something else?

    Thanks for any help!
    -jb
  • Re: Writing to the EventLog from an ASP.NET page

    04-21-2006, 11:59 AM
    • All-Star
      46,022 point All-Star
    • joteke
    • Member since 06-16-2002, 11:24 AM
    • Kyro, Finland
    • Posts 6,879
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    Hi,

    have you initially created a event source? You need to create the event source (with administrative permissions), and after that ASP.NET is able to write to event log. It just can't create the event source by default.

    http://msdn2.microsoft.com/en-us/library/xzwc042w(VS.80).aspx

     

    Thanks,

    Teemu Keiski
    Finland, EU
  • Re: Writing to the EventLog from an ASP.NET page

    05-02-2006, 9:31 AM
    • Participant
      804 point Participant
    • jbeall
    • Member since 07-06-2005, 12:46 PM
    • Posts 253
    I had to create the event source manually by editing my registry.  Doing it in code from my web app didn't work (I assume because my web app was not running as a user with sufficient privileges).

    Thanks for the pointer.
    -jb
  • Re: Writing to the EventLog from an ASP.NET page

    06-05-2006, 11:52 AM
    • Member
      292 point Member
    • sahina
    • Member since 12-11-2004, 11:04 AM
    • New Jersey
    • Posts 67
    How did you manually create it? I need to do the same thing. I am having problems creating it programmatically.

    Thanks
  • Re: Writing to the EventLog from an ASP.NET page

    06-05-2006, 12:06 PM
    • Participant
      804 point Participant
    • jbeall
    • Member since 07-06-2005, 12:46 PM
    • Posts 253
    http://support.microsoft.com/default.aspx?scid=kb;en-us;329291

    You want the method described under "First Approach".
    -jb
  • Re: Writing to the EventLog from an ASP.NET page

    06-07-2006, 2:09 PM
    • All-Star
      46,022 point All-Star
    • joteke
    • Member since 06-16-2002, 11:24 AM
    • Kyro, Finland
    • Posts 6,879
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    As I referred to having Admin permissions, I have been doing this by having a custom WinForm app which does the registration (creating event source with given name etc).

    It is just run once by admin (or by me in this case) and after that ASP.NET is able to write to the event log (with that source). Point is just to create the source with adequate permissions.

    Thanks,

    Teemu Keiski
    Finland, EU
  • Re: Writing to the EventLog from an ASP.NET page

    06-01-2007, 4:20 PM

    Good idea Joteke. Maybe I can add this as a custom action to the setup and deployment package for my web app.

    Filed under: ,
  • Re: Writing to the EventLog from an ASP.NET page

    07-04-2007, 1:38 AM
    • Member
      2 point Member
    • jasonken
    • Member since 07-04-2007, 1:31 AM
    • Posts 1

    Powershell offers another really good option as you can call directly into the framework. This way you don't have to create a project just for the sake of performing some minor admin legwork. Also, you could probably package the script in a setup package.

    From the powershell commandline:

    $x = [System.Diagnostics.EventLog]
    $x::CreateEventSource("MyApp", "Application")

     

     

Page 1 of 1 (8 items)