在写入新的事件日志源时,用C#编写的服务失败

在写入新的事件日志源时,用C#编写的服务失败,c#,service,C#,Service,给定以下代码 InitializeComponent(); try { if (!System.Diagnostics.EventLog.SourceExists(this.ServiceName)) { System.Diagnostics.EventLog.CreateEventSource( this.ServiceNam

给定以下代码

        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");
        }
如果我使用已经存在的事件日志源,它可以正常工作,但是如果我尝试用于此应用程序的事件源,它就会失败

我甚至试过以下方法:;它使用profileWatcherLog和ServiceName的属性,但仍然失败

            if (!System.Diagnostics.EventLog.SourceExists(this.ServiceName))
            {
                System.Diagnostics.EventLog.CreateEventSource(
                    this.ServiceName, profileWatcherLog.Log);
            }
            profileWatcherLog.Source = this.ServiceName;
有人知道我做错了什么吗


提前感谢

您只需重新启动计算机即可

如果源已映射到日志,而您将其重新映射到新日志,则必须重新启动计算机才能使更改生效


要分析问题,了解错误会有所帮助。“它失败”是什么意思?任何异常、错误消息等。。。?我记得您需要管理员权限/特权才能创建事件源。但OP不想将现有源重新映射到新日志,他想创建一个新源。问题的原因是此服务将查看网络驱动器,因此需要在网络帐户下运行。这没有本地系统权限,因此无法创建事件源。当服务在LocalSystem下运行时,它会创建源,但由于某种原因会写入应用程序日志。当我将日志设置为一个值时,它失败了,出现了一个异常-因此,正如您所说,我需要重新启动我的电脑