2011-02-02 51 views
7

我有一個安裝項目,安裝Windows服務。安裝項目的Windows服務和事件日誌

我們在winservice項目應該使用的自定義日誌中註冊事件日誌源(如何以及爲什麼不重要)。

我的問題是,安裝項目嘗試創建默認情況下的事件日誌源。這樣做會得到一條錯誤消息("Error 1001" source XXX already exists on local computer)並回滾。

我已到處尋找,無法找到註冊完成的位置或我可以關閉它。

如何強制Windows服務或安裝項目不創建事件日誌源?

回答

7

您可以刪除默認EventLogInstaller

namespace MyService 
{ 
    [RunInstaller(true)] 
    public partial class ProjectInstaller : Installer 
    { 
     public ProjectInstaller() 
     { 
      InitializeComponent(); 

      // Remove the default Event Log Installer 
      EventLogInstaller DefaultInstaller = null; 
      foreach (Installer installer in serviceInstaller1.Installers) 
      { 
       if (installer is EventLogInstaller) 
       { 
        DefaultInstaller = (EventLogInstaller)installer; 
        break; 
       } 
      } 
      if (DefaultInstaller != null) 
      { 
       serviceInstaller1.Installers.Remove(DefaultInstaller); 
      } 
     } 
    } 
} 

或者,您可以修改Log屬性:

foreach (Installer installer in serviceInstaller1.Installers) 
{ 
    if (installer is EventLogInstaller) 
    { 
     ((EventLogInstaller)installer).Log = "MyLog"; 
     break; 
    } 
} 

現在事件將成功記錄到MyLog,並且服務啓動/停止事件仍將記錄到應用程序日誌。

(來源:serviceInstaller component and its default EventLogInstaller

+0

注意,它的線,其中包括 「serviceInstaller1」`的foreach(在serviceInstaller1.Installers安裝程序安裝程序)`是很重要的。我把它寫成`foreach(安裝程序安裝程序在this.Installers)`。這個建好,安裝項目創建了MSI文件。但是當我嘗試安裝服務時,錯誤仍然存​​在。只有當我將「this.Installers」更改爲「serviceInstaller1.Installers」時,才解決了錯誤。 – 2015-06-15 23:15:15

2

這只是一個基於我的測試的猜測。

安裝程序項目(或WindowService類)會自動創建與myService.ServiceName同名的事件源。這很可能是因爲每次啓動/停止服務時,Start/Stop消息都會寫入日誌。這些消息需要一個源代碼。

換句話說:您不需要像爲您完成的那樣創建與ServiceName同名的源。

3

我想當你卸載服務時,某些東西不能正確卸載,特別是在事件日誌中。

要重新恢復到重新安裝該服務的能力,我發現(thanks to this article),你需要在註冊表中刪除器(regedit.exe)此項:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog\YOURSERVICENAME 
相關問題