简体   繁体   中英

Service written in C# fails when writing to new event log source

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM