System.Security.SecurityException: The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security

C#WindowsWindows ServicesEvent Log

C# Problem Overview


I am trying to create a Windows Service, but when I try and install it, it rolls back giving me this error:

> System.Security.SecurityException: The > source was not found, but some or all > event logs could not be searched. > Inaccessible logs: Security.

I don't know what this means - my application has the bare minimum since I am just testing things out first.

My Installer Code:

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)
        {
        }
    }
}

My Service Code:

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();
    }
}

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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

Solution 3 - C#

Run as Administrator

This is a very common issue that programmers are missing out

Solution 4 - C#

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
  1. Navigate to a path listed above
  2. Right click on the desired path
  3. Make sure that both Read and Full Control permission checkboxes are ticked for Administrators
  4. Click Apply and OK
  5. Repeat the same process for the other path

Solution 5 - C#

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

Solution 6 - C#

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.

Solution 7 - C#

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.

Solution 8 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmichelleView Question on Stackoverflow
Solution 1 - C#WhiteKnightView Answer on Stackoverflow
Solution 2 - C#VickyView Answer on Stackoverflow
Solution 3 - C#OmerView Answer on Stackoverflow
Solution 4 - C#keith.gView Answer on Stackoverflow
Solution 5 - C#MarioArayaView Answer on Stackoverflow
Solution 6 - C#JimView Answer on Stackoverflow
Solution 7 - C#Chris CatignaniView Answer on Stackoverflow
Solution 8 - C#tgoliveira11View Answer on Stackoverflow