Monday, January 14, 2013

Event Source Installer และ installutil command

เมื่อ solution ที่เราเขียน ต้องการการติดตั้งบางอย่างบนเครื่องที่เราจะใช้รัน เราสามารถสร้าง installer เพื่อทำการเตรียน Environment ให้โปรแกรมเราได้ เช่นตัวอย่างจะใช้สร้าง Event Source เพื่อใช้เก็บ Event Log

1. สร้าง class ที่ implement Installer ใน solution ที่เราเขียน

using System;
using System.Configuration.Install;
using System.Diagnostics;
using System.ComponentModel;

[RunInstaller(true)]
public class MyEventLogInstaller: Installer
{
    private EventLogInstaller myEventLogInstaller;

    public MyEventLogInstaller() 
    {
        // Create an instance of an EventLogInstaller.
        myEventLogInstaller = new EventLogInstaller();

        // Set the source name of the event log.
        myEventLogInstaller.Source = "NewLogSource";

        // Set the event log that the source writes entries to.
        myEventLogInstaller.Log = "MyNewLog";

        // Add myEventLogInstaller to the Installer collection.
        Installers.Add(myEventLogInstaller);   
    }
}


2. หลังจาก build จะได้ assembly .exe หรือ .dll เราก็สามารถใช้ ตัว VisualStudio Command prompt ติดตังตัว event source ลงบนเครื่องที่เราต้องการได้โดย run as admin แล้วรันคำสั่งต่อไปนี้



C:\xxx\bin>installutil App_Code.dll



3. ตัวอย่างผลรัน


C:\xxx\bin>installutil App_Code.dll
Microsoft (R) .NET Framework Installation utility Version 4.0.30319.17929
Copyright (C) Microsoft Corporation.  All rights reserved.


Running a transacted installation.

Beginning the Install phase of the installation.
See the contents of the log file for the C:\xxx\bin\App_Code.dll assembly's progress.
The file is located at C:\xxx\bin\App_Code.InstallLog.
Installing assembly 'C:\xxx\bin\App_Code.dll'.
Affected parameters are:
   logtoconsole =
   logfile = C:\xxx\bin\App_Code.InstallLog
   assemblypath = C:\xxx\bin\App_Code.dll
Creating EventLog source SemanticLoggingEventLogPerfTests in log ...

The Install phase completed successfully, and the Commit phase is beginning.
See the contents of the log file for the C:\xxx\bin\App_Code.dll assembly's progress.
The file is located at C:\xxx\bin\App_Code.Install
Log.
Committing assembly 'C:\xxx\bin\App_Code.dll'.
Affected parameters are:
   logtoconsole =
   logfile = C:\xxx\bin\App_Code.InstallLog
   assemblypath = C:\xxx\bin\App_Code.dll

The Commit phase completed successfully.

The transacted install has completed.


No comments:

Post a Comment