<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>You had me at "Hello World" : Windows Service</title><link>http://blogs.msdn.com/helloworld/archive/tags/Windows+Service/default.aspx</link><description>Tags: Windows Service</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>How to Create Custom Event Log for Windows Service</title><link>http://blogs.msdn.com/helloworld/archive/2008/12/11/creating-an-event-log.aspx</link><pubDate>Thu, 11 Dec 2008 20:52:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9197604</guid><dc:creator>HelloWorld</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/helloworld/comments/9197604.aspx</comments><wfw:commentRss>http://blogs.msdn.com/helloworld/commentrss.aspx?PostID=9197604</wfw:commentRss><description>&lt;p&gt;When you created a Windows Service, you usually add a Service Installer to allow this service to be installed using InstallUtil or installing it programmatically like this &lt;a href="http://blogs.msdn.com/helloworld/archive/2008/10/21/how-to-install-windows-service-programmatically.aspx"&gt;post&lt;/a&gt; shows you.&lt;/p&gt;  &lt;p&gt;The service installer will create the Event Log for the service, and by default it is &amp;quot;Application&amp;quot;, and the source is the name of your service.&lt;/p&gt;  &lt;p&gt;The Event Source is most likely to be fine, but the event log may need to be set to something else. Unfortunately, the option is not visible from the installer.&lt;/p&gt;  &lt;p&gt;The Installer object for your windows service, actually hosted several other Installer objects, one of the child Installer is responsible to create the Event Log. You need to find this object and then changed the Log property and it will create the appropriate event log. It is easy to find this object as this object is an instance of EventLogInstaller.&lt;/p&gt;  &lt;p&gt;The code:&lt;/p&gt;  &lt;p&gt;&lt;span style="color: blue"&gt;public partial class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MyServiceInstaller &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;Installer     &lt;br /&gt;&lt;/span&gt;{    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;private const string&lt;/span&gt;LogName = &lt;span style="color: #a31515"&gt;&amp;quot;My Service Log&amp;quot;&lt;/span&gt;;    &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;public &lt;/span&gt;MyServiceInstaller()    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; InitializeComponent();    &lt;br /&gt;&lt;span style="color: green"&gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color: #2b91af"&gt;EventLogInstaller &lt;/span&gt;EventLogInstall = &lt;span style="color: blue"&gt;null&lt;/span&gt;;    &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Installer &lt;/span&gt;I &lt;span style="color: blue"&gt;in this&lt;/span&gt;.serviceInstaller.Installers)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; EventLogInstall = I &lt;span style="color: blue"&gt;as &lt;/span&gt;&lt;span style="color: #2b91af"&gt;EventLogInstaller&lt;/span&gt;;    &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;if&lt;/span&gt;(EventLogInstall != &lt;span style="color: blue"&gt;null&lt;/span&gt;)    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; {    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; EventLogInstall.Log = LogName;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; EventLogInstall.UninstallAction = &lt;span style="color: #2b91af"&gt;UninstallAction&lt;/span&gt;.NoAction;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;break&lt;/span&gt;;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }    &lt;br /&gt;}&lt;/p&gt;  &lt;p&gt;That code above shows how to create a custom Event Log for your service, the code also shows that the custom event log will never be uninstalled, depending on the requirements it can be easily changed.&lt;/p&gt;  &lt;p&gt;If the event source needs to be changed as well, simply change the Source property and you are set.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;One thing to pay attention&lt;/strong&gt;, if you create a custom event log, you install the service programmatically, and your service is running with a restricted account, &lt;u&gt;then you have to modify the code to install the service by adding a code to write one entry to the event log that you just created&lt;/u&gt;.&lt;/p&gt;  &lt;p&gt;After the service installer is executed, the log is 'registered', but not created yet. To create it, one event must be written. If the service runs using a restricted user account, that account may not have enough security permission to write the first log, as the log need to be created.&lt;/p&gt;  &lt;p&gt;When installing the service, the user must run the installer as Administrator, so the installer has all security privilege that it needs. So remember to write one event entry after programmatically installing the service. &lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9197604" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/helloworld/archive/tags/Programming/default.aspx">Programming</category><category domain="http://blogs.msdn.com/helloworld/archive/tags/.Net+Framework/default.aspx">.Net Framework</category><category domain="http://blogs.msdn.com/helloworld/archive/tags/How+to/default.aspx">How to</category><category domain="http://blogs.msdn.com/helloworld/archive/tags/Windows+Service/default.aspx">Windows Service</category></item><item><title>How to Install Windows Service Programmatically</title><link>http://blogs.msdn.com/helloworld/archive/2008/10/21/how-to-install-windows-service-programmatically.aspx</link><pubDate>Tue, 21 Oct 2008 02:41:09 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9008362</guid><dc:creator>HelloWorld</dc:creator><slash:comments>9</slash:comments><comments>http://blogs.msdn.com/helloworld/comments/9008362.aspx</comments><wfw:commentRss>http://blogs.msdn.com/helloworld/commentrss.aspx?PostID=9008362</wfw:commentRss><description>&lt;p&gt;Sometimes you may want to install a Windows Service programmatically, but the target machine does not have InstallUtil.exe.&lt;/p&gt;  &lt;p&gt;To install a Windows Service programmatically, you can build an application to install that Windows Service.&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Add a reference to System.Configuration.Install&lt;/li&gt;    &lt;li&gt;Use this code:&lt;/li&gt; &lt;/ul&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static void &lt;/span&gt;InstallService(&lt;span style="color: blue"&gt;string &lt;/span&gt;ExeFilename)
{
    System.Configuration.Install.&lt;span style="color: #2b91af"&gt;AssemblyInstaller &lt;/span&gt;Installer = &lt;span style="color: blue"&gt;new &lt;/span&gt;System.Configuration.Install.&lt;span style="color: #2b91af"&gt;AssemblyInstaller&lt;/span&gt;(ExeFilename);
    Installer.UseNewContext = &lt;span style="color: blue"&gt;true&lt;/span&gt;;
    Installer.Install(&lt;span style="color: blue"&gt;null&lt;/span&gt;);
    Installer.Commit(&lt;span style="color: blue"&gt;null&lt;/span&gt;);
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;To uninstall:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static void &lt;/span&gt;UninstallService(&lt;span style="color: blue"&gt;string &lt;/span&gt;ExeFilename)
{
    System.Configuration.Install.&lt;span style="color: #2b91af"&gt;AssemblyInstaller &lt;/span&gt;Installer = &lt;span style="color: blue"&gt;new &lt;/span&gt;System.Configuration.Install.&lt;span style="color: #2b91af"&gt;AssemblyInstaller&lt;/span&gt;(ExeFilename);
    Installer.UseNewContext = &lt;span style="color: blue"&gt;true&lt;/span&gt;;
    Installer.Uninstall(&lt;span style="color: blue"&gt;null&lt;/span&gt;);
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9008362" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/helloworld/archive/tags/.Net+Framework/default.aspx">.Net Framework</category><category domain="http://blogs.msdn.com/helloworld/archive/tags/How+to/default.aspx">How to</category><category domain="http://blogs.msdn.com/helloworld/archive/tags/Windows+Service/default.aspx">Windows Service</category></item></channel></rss>