0

Given the following code

        InitializeComponent();

        try
        {
            if (!System.Diagnostics.EventLog.SourceExists(this.ServiceName))
            {
                System.Diagnostics.EventLog.CreateEventSource(
                    this.ServiceName, "ProfileWatcherLog");
            }
            profileWatcherLog.Source = this.ServiceName;
            profileWatcherLog.Log = "ProfileWatcherLog";
        }
        catch (Exception e)
        {
            profileWatcherLog.WriteEntry(String.Format("ERROR: {0}",e.Message));

        }
        finally
        {
            profileWatcherLog.WriteEntry("Profile Watcher setup");
        }

If I use an already existing event log source it works fine, but if I try the event source I want to use for this application then it fails.

I have even tried the following; which uses the properties of the profileWatcherLog and the ServiceName and it still fails.

            if (!System.Diagnostics.EventLog.SourceExists(this.ServiceName))
            {
                System.Diagnostics.EventLog.CreateEventSource(
                    this.ServiceName, profileWatcherLog.Log);
            }
            profileWatcherLog.Source = this.ServiceName;

Anyone see what I have done wrong?

Thanks in advance

1
  • 1
    To analyse a problem, it helps to know the error. What does "it fails" mean? Any exception, error message etc...? I remember you need admin rights/privileges to create an event source. Commented Sep 18, 2017 at 16:09

1 Answer 1

1

You only have to restart your computer.

If a source has already been mapped to a log and you remap it to a new log, you must restart the computer for the changes to take effect.

https://msdn.microsoft.com/en-us/library/2awhba7a.aspx

Sign up to request clarification or add additional context in comments.

2 Comments

But OP does not want to remap an existing source to a new log, he wants to create a new source.
Cause of my problem is that this service will be looking at network drives so needs to run under a network account. This does not have Local System rights and so fails to create the event source. When the service runs under LocalSystem it creates the source but for some reason wrote to the Application Log. When I set the log to a value it failed with an exception - so as you say I will need to reboot my PC

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.