系统。保安。未找到源,但无法搜索部分或全部事件日志。无法访问的日志: 安全性

我正在尝试创建一个 Windows 服务,但是当我尝试安装它时,它回滚给我这个错误:

系统。安全性 没有找到来源,但部分或全部 无法搜索事件日志。 无法访问的日志: 安全性。

我不知道这意味着什么——我的应用程序只有最低限度,因为我只是首先测试一些东西。

我的安装代码:

namespace WindowsService1
{
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
public ProjectInstaller()
{
//set the privileges
processInstaller.Account = ServiceAccount.LocalSystem;
processInstaller.Username = null;
processInstaller.Password = null;


serviceInstaller.DisplayName = "My Service";
serviceInstaller.StartType = ServiceStartMode.Manual;


//must be the same as what was set in Program's constructor
serviceInstaller.ServiceName = "My Service";


this.Installers.Add(processInstaller);
this.Installers.Add(serviceInstaller);
}


private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
}


private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
}
}
}

我的服务代码:

public partial class Service1 : ServiceBase
{
public Service1()
{
this.ServiceName = "My Service";
}


protected override void OnStart(string[] args)
{
base.OnStart(args);
}


protected override void OnStop()
{
base.OnStop();
}
}
100194 次浏览

If you are being prompted for a user name and password, then something, somewhere is set to Account = ServiceAccount.User - that's the only way that could (should) happen. Perhaps your code in the comment above is not being executed or it is being changed back by later executing code.

As far as your second paragraph, in general, I would think a service would be fine for this if you don't want it to be see on the console or run as a task. I am not sure if I understand the part about running it as ASP.NET and having it not allow you to see the database...

Finally, in your last paragraph, I can't speak to the NullExeception without knowing more about what is going on in your installer's code.

I got the same exception when trying to install a service from the command line when using installutil in Windows 7. The solution was to open the command line as Administrator and then run installutil.

Also you may find it easier to use a framework like TopShelf to host your services, because it manages all the setup configuration from the name and description of the service to how your recovery process will work. It also allows to easily start your service from inside the IDE when you are debugging it.

Run your command prompt as administrator. It will solve your problem

I solve this same issue by opening the VS2013 Developer Console with administrative permissions.

Run as Administrator

This is a very common issue that programmers are missing out on.

You are probably trying to install a service using

  1. A user account which does not have sufficient rights OR
  2. A user with Administrator privileges but did not run the Command Prompt in 'Administrator Mode'.

Specifically, the issue in this case is the creation of some EventLog registry keys during service install.

One way to fix this is to make sure that you are running the Command Prompt in Administrator mode. (Right-click > Run as Administrator)

I have also encountered some cases where this method still fails to solve the SecurityException problem due to some registry keys not having 'Full Control' permissions for Administrator accounts.

The following keys should have 'Full Control' set for Administrators in order for the service to be able to write to the EventLog:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\EventLog\Application

This may be done by:

  1. Invoking the Windows Registry Editor
    • Run [Win + R]
    • Type 'regedit'
    • OK
  2. Navigate to a path listed above
  3. Right click on the desired path
  4. Make sure that both Read and Full Control permission checkboxes are ticked for Administrators
  5. Click Apply and OK
  6. Repeat the same process for the other path

I was getting this error(above in the OP) while trying to test for the existence of an EventLog

   if (!EventLog.SourceExists("applicatioName"))
EventLog.CreateEventSource("applicatioName", "Application");

Running VisualStudio as Administrator resolved the issue.

In my case, I had a problem in the ProjectInstaller class, I tried to change a property of the service and this attribution generated an object reference error once the coding was wrong. Once I got the right code, the System.Security.SecurityException error was solved.