Feb 25 2008

Your need permission to do that...

Category: Tips and TricksJoeGeeky @ 12:36

Here is a common mistake I see time and time again...  Creating EventSources for logging EventLog entries requires administrative rights on most systems.  These days the only time apps generally have the required permission is during the MSI installation process. It is during application installation that you want to register EventLog Event Sources, because doing this at runtime can often lead to SecurityExceptions.  The mechanism to accomplish this is a custom Installer class.  This is pretty easy to implement and can open the door to solutions to other such problems (e.x. Installing Performance Counters, WMI registration, etc...).  Here is a sample:

using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.Diagnostics;
/// <summary>
/// Facilitates the installation and 
/// deinstallation of Registry 
/// Logging Event Sources
/// in the Windows EventLog
/// </summary>
/// <remarks>
/// This is called by the Setup custom 
/// actions item for install and 
/// uninstall to perform required 
/// actions
/// </remarks>
[RunInstaller(true)]
public sealed class MyRegistryInstaller
    : Installer
{
    /// <summary>
    /// Establishes an application, using 
    /// the specified Source, as a valid 
    /// event source for writing entries 
    /// to a log on the local computer. 
    /// This method can also create a new 
    /// custom log on the local computer. 
    /// </summary>
    /// <param name="stateSaver">
    /// An IDictionary used to save 
    /// information needed to perform a 
    /// commit, rollback, or uninstall 
    /// operation.
    /// </param>
    public override void Install(IDictionary stateSaver)
    {
        base.Install(stateSaver);
        EventLog.CreateEventSource("MyEventSource", "Application");
        Context.LogMessage("Installed EventLog Source");
    }
    /// <summary>
    /// Removes the event source 
    /// registration from the event log 
    /// of the local computer.
    /// </summary>
    /// <param name="savedState">
    /// An IDictionary used to save 
    /// information needed to perform a 
    /// commit, rollback, or uninstall 
    /// operation.
    /// </param>
    public override void Uninstall(IDictionary savedState)
    {
        base.Uninstall(savedState);
        if (EventLog.SourceExists("MyEventSource"))
            EventLog.DeleteEventSource("MyEventSource");
        Context.LogMessage("EventLog Source uninstalled");
    }
}

Tags:

Comments

1.
Ilene Peterson Ilene Peterson United States says:

I truly admire what you have done here. It is charming to see you verbalize from the heart and your lucidity on this crucial subject can be easily ascertained. Extraordinary post and will look forward to your next update.

2.
Beatrice Ottman Beatrice Ottman United States says:

I just couldnt leave your website before saying that I really enjoyed the quality information you offer to your visitors.

3.
Barton Jack Barton Jack United Kingdom says:

Do you earn decent money from this blog or are you doing it just for fun?

4.
logan sam logan sam People's Republic of China says:

<P>this really assists, now i receive the problems and i donot know how to figure out,
i research yahoo and found your blog,
thanks once more</P><P>just one thing, can i post this entry on my blog? i will add the source and credit to your site.</P><P>regards!</P>

5.
jack z jack z People's Republic of China says:

<P>this really assists, now i encounter the problems and i donot know how to puzzle out,
i search yahoo and discovered your blog,
thanks once more</P><P>just one thing, may i post this entry on my site? i will add the source and credit to your site.</P><P>regards!</P>

6.
Ignacio Castelan Ignacio Castelan United Kingdom says:

It appears that you have placed a lot of effort into your article and I require more of these on the net these days. I sincerely got a kick out of your post. I don't really have much to say in response, I only wanted to comment to reply wonderful work.

7.
Ali Gros Ali Gros People's Republic of China says:

wow, awesome post, so do you think i can implement this strategy? and it should work&nbsp; right?

8.
Natalie Natalie United States says:

You had solid ideas there. I made a search on the issue and found most peoples will agree with your blog.

9.
Alba Kuhs Alba Kuhs United Kingdom says:

I thought it was going to be some boring old post, but it really compensated for my time. I will post a link to this page on my blog. I am sure my visitors will find that very useful.

10.
Corliss Menucci Corliss Menucci United Kingdom says:

I just couldnt leave your website before saying that I really enjoyed the quality information you offer to your visitors... Will be back often to check up on new stuff you post!

Comments are closed