2

私は次の指示に従っています:

http://msdn.microsoft.com/en-us/library/9k985bc9.aspx - サービスの作成方法

http://msdn.microsoft.com/en-us/library/ddhy0byf.aspx - 方法: サービス アプリケーションにインストーラーを追加する

次に、管理者特権のコマンド ウィンドウとコマンドを使用して、サービスをインストールします。 installutil.exe -i WindowsService1.exe

これにより、サービスとすべてが完全にビルドおよびインストールされます。

ただし、サービスにログを追加したいので、イベント ログ ソースを作成し、インストーラーで最初のメッセージを書き込みます (インストーラーは昇格された特権で実行されますが、実際のサービスはそうではない可能性があるため)。 )

そこで、インストーラー コードに以下を追加します ( projectinstaller.cs)

public ProjectInstaller()
{
    InitializeComponent();

    if (!System.Diagnostics.EventLog.SourceExists("TestService"))
    {
        System.Diagnostics.EventLog.CreateEventSource(
            "TestService Service", "TestService");
    }
    System.Diagnostics.EventLog TestEventLog = new System.Diagnostics.EventLog();
    TestEventLog.Source = "TestService ServiceInstaller";
    TestEventLog.Log = "TestService";

    TestEventLog.WriteEntry("New log created");
}

これはまだビルドされますが、現在installutil.exe -i WindowsService1.exe(まだ昇格された特権で実行されています)、次のエラーがスローされます。

An exception occurred during the Install phase.
System.InvalidOperationException: Unable to create an instance of the WindowsService1.ProjectInstaller installer type.
The inner exception System.Reflection.TargetInvocationException was thrown with the following error message: Exception has been thrown by the target of an invocation..
The inner exception System.ArgumentException was thrown with the following error message: Log TestService has already been registered as a source on the local computer..

Google は、これが Windows RE がログを作成する際の何らかの形式のアクセス許可エラーであると信じさせ、レジストリに手動で書き込むことを含む解決策に導きました。ただし、より良い方法がある場合は、レジストリをいじらないことをお勧めします(たとえば、これを機能させるネイティブのC#の方法)

サービスのインストール時に、ログを登録して初期メッセージを書き込むにはどうすればよいですか?

4

1 に答える 1