Security exception when writting to an EventLog from an ASP.NET MVC application

[亡魂溺海] 提交于 2019-12-03 12:37:28
UpTheCreek

From MSDN: "Applications that run using the Network Service identity can write to the event log by using existing event sources, but they cannot create new event sources because of insufficient registry permissions."

And...

"If the Source for the event log associated with the EventLog instance does not exist, a new event source is created."

So looks like your event log source doesn't exist, and it's trying to create a new event log source using the Network Service User (which requires writing to the registry, so wont work).

"To enable your ASP.NET application to write to the event log using an event source that does not already exist, you have two options:"

  • Create new event sources at application install time
  • Manually create new event source entry in the registry.

So, need to create the log outside of the application (you can't do it programatically with this user. Do it either manually, or create a simple command line app to simplify installation).

For full details:

http://msdn.microsoft.com/en-us/library/ms998320.aspx#paght000015_eventlogaccess

Personally I'd recommend that you don't alter the net user permissions, but rather create the log source outside of the web app. My preference is in a console app (which will take you about 5mins to write, and which you can also use to prep other machines). Start a new console app in VS.NET, and add the code to create the log sources. An example:

http://www.dotnetspider.com/resources/23593-Create-Event-log-VB-NET.aspx

Then just run the console app from the cmd line, when logged in with appropriate permissions.

If you're not sure what event source it is trying to create, the above accepted answer will be difficult to figure out.

A simpler solution would be to switch the application pool to temporarily use LocalSystem, then run the application and produce an error. It will be able to create the relevant event log source, and after that you can switch it back to using NetworkService.

I don't know why can't you create your own EventLog instead of writing on Application log.

You can create an window/console application with the following code and run it as administrator, this will create a new log for you.

if (!EventLog.Exists("LOG_NAME"))
   EventLog.CreateEventSource("LOG_NAME", "LOG_NAME");

this will create a new Log inside the event log and visible in application and service logs.

 if (!EventLog.SourceExists("MyMVCApp"))
    EventLog.CreateEventSource("MyMVCApp", "LOG_NAME");

This will create a new Source inside the custom "LOG_NAME" and you can make use of the code

Dim log = New EventLog("LOG_NAME", My.Computer.Name, "MyMVCApp")

to create a custom log.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!