Over the past couple of weeks I have been looking at migrating existing ASP.NET applications from being hosted in on premise to being hosted on the Windows Azure Platform.

One of the problems that I have run into whilst trying to migrate the applications to Windows Azure was ensuring that the Event Log’s and Performance Counter were accessible and could be written to by the application.

When running inside of Windows Azure applications tend to run without elevated permissions so we ended up running into multiple security messages signifying that the application could not write to the required event logs due to the lack of security permissions.

A work around for this is to use a new feature recently added to Windows Azure and the ability to create startup tasks that can be run as administrator when the VM starts. From this we can run command statements to install the event logs and performance counters.

Getting a startup task running in the Azure nodes means adding the following snippet to each role, this will run the specified command line task (startup.cmd) with elevated privileges before anything else is done on the VM (taskType=simple).

The file startup.cmd is then added to the site (or could be added as an additional file during packaging) and ensure that the properties are set so that this and any other file needed for the startup task is set to “Copy always” this will ensure the file is within the package when built.

Within this startup.cmd task we are to first set up PowerShell and then run our own PowerShell script which will contain the code to start register the event logs:

Within CreateEventLog.ps1 we can then easily create event logs and sources with the following PowerShell snippet:

This will now have created the needed event logs that can be written to by the application when it starts running ... no more security errors, and your eventlog is now accessiable.