/// <devdoc>
        /// </devdoc>
        internal void Dispose(bool disposing) {
            try {
                if (disposing) {
                    //Dispose unmanaged and managed resources
                    if (IsOpen) {
                        Close();
                    }

                    // This is probably unnecessary
                    if (readHandle != null) {
                    readHandle.Close();
                        readHandle = null;
                    }

                    if (writeHandle != null) {
                    writeHandle.Close();
                        writeHandle = null;
                    }
                }
            }
            finally {
                messageLibraries = null;
                this.boolFlags[Flag_disposed] = true;
            }
        }
        public EventLogInternal(string logName, string machineName, string source, EventLog parent) {
            //look out for invalid log names
            if (logName == null)
                throw new ArgumentNullException("logName");
            if (!ValidLogName(logName, true))
                throw new ArgumentException(SR.GetString(SR.BadLogName));

            if (!SyntaxCheck.CheckMachineName(machineName))
                throw new ArgumentException(SR.GetString(SR.InvalidParameter, "machineName", machineName));

            EventLogPermission permission = new EventLogPermission(EventLogPermissionAccess.Write, machineName);
            permission.Demand();
            
            this.machineName = machineName;

            this.logName = logName;
            this.sourceName = source;
            readHandle = null;
            writeHandle = null;
            boolFlags[Flag_forwards] = true;
            this.parent = parent;
        }
        private void Close(string currentMachineName) {
            EventLogPermission permission = new EventLogPermission(EventLogPermissionAccess.Write, currentMachineName);
            permission.Demand();

            Debug.WriteLineIf(CompModSwitches.EventLog.TraceVerbose, "EventLog::Close");
            //Trace("Close", "Closing the event log");
            if (readHandle != null) {
                try {
                    readHandle.Close();
                }
                catch (IOException) {
                    throw SharedUtils.CreateSafeWin32Exception();
                }
                readHandle = null;
                //Trace("Close", "Closed read handle");
                Debug.WriteLineIf(CompModSwitches.EventLog.TraceVerbose, "EventLog::Close: closed read handle");
            }
            if (writeHandle != null) {
                try {
                    writeHandle.Close();
                }
                catch (IOException) {
                    throw SharedUtils.CreateSafeWin32Exception();
                }
                writeHandle = null;
                //Trace("Close", "Closed write handle");
                Debug.WriteLineIf(CompModSwitches.EventLog.TraceVerbose, "EventLog::Close: closed write handle");
            }
            if (boolFlags[Flag_monitoring])
                StopRaisingEvents(/*currentMachineName,*/ GetLogName(currentMachineName));

            if (messageLibraries != null) {
                foreach (SafeLibraryHandle handle in messageLibraries.Values)
                    handle.Close();

                messageLibraries = null;
            }
            
            boolFlags[Flag_sourceVerified] = false;
        }
        private void OpenForWrite(string currentMachineName) {
            //Cannot allocate the writeHandle if the object has been disposed, since finalization has been suppressed.
            if (this.boolFlags[Flag_disposed])
                throw new ObjectDisposedException(GetType().Name);

            Debug.WriteLineIf(CompModSwitches.EventLog.TraceVerbose, "EventLog::OpenForWrite");
            if (sourceName == null || sourceName.Length==0)
                throw new ArgumentException(SR.GetString(SR.NeedSourceToOpen));

            //Check environment before calling api
            SharedUtils.CheckEnvironment();

            SafeEventLogWriteHandle handle = SafeEventLogWriteHandle.RegisterEventSource(currentMachineName, sourceName);
            if (handle.IsInvalid) {
                Win32Exception e = null;
                if (Marshal.GetLastWin32Error() != 0) {
                    e = SharedUtils.CreateSafeWin32Exception();
                }
                throw new InvalidOperationException(SR.GetString(SR.CantOpenLogAccess, sourceName), e);
            }

            writeHandle = handle;
        }
 public EventLogInternal(string logName, string machineName, string source, EventLog parent)
 {
     this.firstCachedEntry = -1;
     this.boolFlags = new BitVector32();
     if (logName == null)
     {
         throw new ArgumentNullException("logName");
     }
     if (!ValidLogName(logName, true))
     {
         throw new ArgumentException(SR.GetString("BadLogName"));
     }
     if (!SyntaxCheck.CheckMachineName(machineName))
     {
         throw new ArgumentException(SR.GetString("InvalidParameter", new object[] { "machineName", machineName }));
     }
     new EventLogPermission(EventLogPermissionAccess.Write, machineName).Demand();
     this.machineName = machineName;
     this.logName = logName;
     this.sourceName = source;
     this.readHandle = null;
     this.writeHandle = null;
     this.boolFlags[2] = true;
     this.m_Parent = parent;
 }
 internal void Dispose(bool disposing)
 {
     try
     {
         if (disposing)
         {
             if (this.IsOpen)
             {
                 this.Close();
             }
             if (this.readHandle != null)
             {
                 this.readHandle.Close();
                 this.readHandle = null;
             }
             if (this.writeHandle != null)
             {
                 this.writeHandle.Close();
                 this.writeHandle = null;
             }
         }
     }
     finally
     {
         this.messageLibraries = null;
         this.boolFlags[0x100] = true;
     }
 }
 private void Close(string currentMachineName)
 {
     new EventLogPermission(EventLogPermissionAccess.Write, currentMachineName).Demand();
     if (this.readHandle != null)
     {
         try
         {
             this.readHandle.Close();
         }
         catch (IOException)
         {
             throw SharedUtils.CreateSafeWin32Exception();
         }
         this.readHandle = null;
     }
     if (this.writeHandle != null)
     {
         try
         {
             this.writeHandle.Close();
         }
         catch (IOException)
         {
             throw SharedUtils.CreateSafeWin32Exception();
         }
         this.writeHandle = null;
     }
     if (this.boolFlags[8])
     {
         this.StopRaisingEvents(this.GetLogName(currentMachineName));
     }
     if (this.messageLibraries != null)
     {
         foreach (Microsoft.Win32.SafeHandles.SafeLibraryHandle handle in this.messageLibraries.Values)
         {
             handle.Close();
         }
         this.messageLibraries = null;
     }
     this.boolFlags[0x200] = false;
 }
 private void OpenForWrite(string currentMachineName)
 {
     if (this.boolFlags[0x100])
     {
         throw new ObjectDisposedException(base.GetType().Name);
     }
     if ((this.sourceName == null) || (this.sourceName.Length == 0))
     {
         throw new ArgumentException(SR.GetString("NeedSourceToOpen"));
     }
     SharedUtils.CheckEnvironment();
     this.writeHandle = SafeEventLogWriteHandle.RegisterEventSource(currentMachineName, this.sourceName);
     if (this.writeHandle.IsInvalid)
     {
         Win32Exception innerException = null;
         if (Marshal.GetLastWin32Error() != 0)
         {
             innerException = SharedUtils.CreateSafeWin32Exception();
         }
         throw new InvalidOperationException(SR.GetString("CantOpenLogAccess", new object[] { this.sourceName }), innerException);
     }
 }