locked
EventLog.WriteEntry fails with error - RRS feed

  • Question

  • Greetings,

     

    The following snippet fails with an error under Vista:

    Code Snippet

    catch( System.Exception e )

    {

          System.Diagnostics.Trace.TraceError(String.Format("Failed to load export plugin '{0}'.\n{1}.",

                    fiTemp.FullName, e.InnerException.Message));

          EventLog.WriteEntry( Application.ProductName,

                     String.Format( "Failed to load export plugin '{0}'.\n{1}.",

                     fiTemp.FullName, e.InnerException.Message ),

                    EventLogEntryType.Error );

    }

     

    The error text is: "The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security.

     

    I saw the following, seemingly related post for this error:

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=324053&SiteID=1

     

    However, we are not creating an event log source, we are simply trying to write an application error event entry.

     

    There are several places in our .Net code where we do this. What is the recommended best practice for doing this under Vista?

     

    Thanks in advance,

    Greg Bradburn.

    Thursday, May 3, 2007 6:50 PM

All replies

  • According to the documentation you must first create and configure the event source before using it. Have you done that properly?
    http://msdn2.microsoft.com/en-us/library/xzwc042w.aspx

     

    What kind of event is thrown?

    Saturday, May 5, 2007 9:16 PM
  • I've got the exact same problem and I also found that article you refered to. When I try to call the Eventlog.SourceExists method I get this error:

     

    "source was not found, but some or all event logs could not be searched. Inaccessible logs: Security"

    I've tried to add some EventLogPermission but with no luck

     

    [EventLogPermission(

    SecurityAction.Demand

    , PermissionAccess=EventLogPermissionAccess.Administer

    , MachineName=".")]

    Claus Rathje

    Sunday, May 6, 2007 5:54 AM
  • I've found out that the EventLog.SourceExists requires elevated privileges. This can apparently be done with a manifest.

     

    In the document below in Step Six (How to Create an Embedded an Application Manifest with Microsoft Visual Studio®) it explains how to do that.

     

    Windows Vista Application Development Requirements for User Account Control Compatibility
    http://download.microsoft.com/download/5/6/a/56a0ed11-e073-42f9-932b-38acd478f46d/WindowsVistaUACDevReqs.doc

     

    I hope this will solve the problem. I'll return as soon as I've tried it.

     

    I also found some notes in Aaron Stebner's weblog:

    Some useful things I have learned about Windows Installer and UAC

    http://blogs.msdn.com/astebner/archive/2006/12/13/some-useful-things-i-have-learned-about-windows-installer-and-uac.aspx


    Claus

    Wednesday, May 9, 2007 9:17 AM
  • Thanks for the replies guys,

     

    You are correct that the sources must be laid down first and this can only be done with elevated permissions.

     

    We are modifying our apps to use the Microsoft.EnterpriseLibrary Application Block logging stuff so we will be defining our event log sources in the app.config files of our distributable apps.

     

    Since we will already have the sources defined there, I'm trying to write an installer class that can be used as a custom action to lay down the event log sources. The installer class will need to be able to read the entries from the app.config file for the application being installed.

     

    So far, I have not been able to figure out how to do this.

     

    I tried the following code:

    Code Snippet

    [RunInstaller(true)]

    public class MyTestInstaller : Installer

    {

       private EventLogInstaller myEventLogInstaller;

     

       public MyTestInstaller()

       {

       }

     

       public override void Install(System.Collections.IDictionary stateSaver)

       {

          base.Install(stateSaver);

     

          Configuration config = ConfigurationManager.OpenExeConfiguration(

                                  ConfigurationUserLevel.None);

          if (config != null)

          {

             foreach (ConfigurationSection section in config.Sections)

             {

                LoggingSettings loggingSettings = section as LoggingSettings;

                if (loggingSettings != null)

                {

                   foreach (FormattedEventLogTraceListenerData traceListenerData in 

                            loggingSettings.TraceListeners)

                   {

                      if (!EventLog.SourceExists(traceListenerData.Source))

                      {

                          myEventLogInstaller = new EventLogInstaller();

                          myEventLogInstaller.Source = traceListenerData.Source;

                          myEventLogInstaller.Log = traceListenerData.Log;

                          Installers.Add(myEventLogInstaller);

                      }

                   }

                }

             }

          }

       } 

    }

     

    But when run inside the installer, it seems to pick up the msiexec.exe.config, not the configuration for the assembly being installed.

     

    How can I access the app.config settings for the app being installed from within the Installer.Install() method when invoked as a custom action during the installation process?

     

    Thanks in advance,

     

    Greg.

     

    Monday, May 14, 2007 11:00 PM
  • Here is a link to a bug/suggestion report to update MSDN Documentation to reflect the required security for this:

     

    https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=293617

     

    Thursday, August 16, 2007 2:36 PM