English 中文(简体)
An event log source that s always available for writing?
原标题:

Is there an event log source that s always available for writing by an ASP.NET webapp?

Backstory, in case anyone has a seemingly unrelated solution:

Our ASP.NET webapp uses its own event log source, but it doesn t have the rights to create it. So, if the event log source doesn t exist when the webapp tries to write the entry (install instructions dictate that the admin register the event log source manually, but......) , our webapp doesn t put anything in the event log when it has problems.

I m hoping there s another (app-agnostic) source I can use to notify the folks who watch the event log.

最佳回答

The "ASP.NET X.Y.Z.0" source will, I think, always be available for writing.

问题回答

In this KB article it explains the issue http://support.microsoft.com/kb/329291

PRB: "Requested Registry Access Is Not Allowed" Error Message When ASP.NET Application Tries to Write New EventSource in the EventLog

Symptoms

When you use ASP.NET to create a new event source in the event log, you may receive the following error message:

System.Security.SecurityException: Requested registry access is not allowed.

Cause

By default, the user token of the ASP.NET worker process is ASPNET (or NetworkService for applications that run on Internet Information Services [IIS] 6.0). The problem in the "Symptoms" section occurs because your account does not have the correct user rights to create an event source.

Resolution

First approach:

Create an event source under the Application event log in Registry Editor (on the server, not your development PC). To do this, follow these steps:

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

Second (alternative) approach:

The EventLogInstaller class in the System.Diagnostics namespace permits you to install and configure an event log that your application reads from or writes to while running. You can create an event source by using EventLogInstaller. To do this, follow these steps:

  1. Use Microsoft Visual Basic .NET or Microsoft Visual C# .NET to create a new Class Library named EventLogSourceInstaller. By default, the Class1.vb file or the Class1.cs file is created.
  2. In Solution Explorer, right-click EventLogSourceInstaller, and then click Add References.
  3. In the Add Reference dialog box, double-click System.Configuration.Install.dll, and then click OK.
  4. Rename the Class1.vb`Class1.cs` to MyEventLogInstaller.vbMyEventLogInstaller.cs.
  5. Replace the existing code in MyEventLogInstaller.vb or MyEventLogInstaller.cs with the following sample code: Visual Basic .NET sample
Imports System.Diagnostics
Imports System.Configuration.Install
Imports System.ComponentModel

<RunInstaller(True)> _
Public Class MyEventLogInstaller
    Inherits Installer
    Private myEventLogInstaller As EventLogInstaller

    Public Sub New()
          Create an instance of  EventLogInstaller .
        myEventLogInstaller = New EventLogInstaller()
          Set the  Source  of the event log, to be created.
        myEventLogInstaller.Source = "TEST"
          Set the  Log  that the source is created in.
        myEventLogInstaller.Log = "Application"
          Add myEventLogInstaller to  InstallerCollection .
        Installers.Add(myEventLogInstaller)
    End Sub 
End Class 

Visual C# .NET sample

using System;
using System.Diagnostics;
using System.ComponentModel;
using System.Configuration.Install;


namespace EventLogSourceInstaller 
{
  [RunInstaller(true)]
  public class MyEventLogInstaller : Installer
  {
      private EventLogInstaller myEventLogInstaller;

      public MyEventLogInstaller()
      {
          //Create Instance of EventLogInstaller
          myEventLogInstaller = new EventLogInstaller();

          // Set the Source of Event Log, to be created.
          myEventLogInstaller.Source = "TEST";

          // Set the Log that source is created in
          myEventLogInstaller.Log = "Application";
          
          // Add myEventLogInstaller to the Installers Collection.
          Installers.Add(myEventLogInstaller);
      }
  }
}
  1. On the Build menu, click Build Solution to create EventLogSourceInstaller.dll.
  2. Open the Visual Studio .NET Command Prompt.
  3. At the command prompt, change to the folder where EventLogSourceInstaller.dll is located.
  4. Run the following command to create the EventSource: InstallUtil EventLogSourceInstaller.dll

If you use the second approach under the resolution you should get it to work.

If you don t want to do that or you can t get it to work correctly another way would be to use the identity tag in your web.config and impersonate a user that does have rights to edit the registry. This is a security whole only for this application but if you implement some extra security measures you should be fine.

Considered email notification? I guess some admins prefer to be notified from their cellphones.

You could also ring home (call a web service to write log back to your own server), in case the client refuse to create a key under HKLMSystemCurrentControlSetServicesEventLog

You might not have access to create an event source from the web app, but if memory serves, you can check to see if one exists.

In the global.asax or a custom handler, check to see if they created it like they were supposed to. If they didn t, have a really annoying reminder div show on every page. As soon as they create it like they were supposed to, the div goes away :)

You should be able to write to the built-in event logs (Application, Security, System) without much difficulty.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签