public IEnumerable <EventLogEntry> GetEntries()
        {
            EventLogPermission evtPermission = new EventLogPermission(EventLogPermissionAccess.Administer, MachineName);

            evtPermission.Demand();
            return(Log?.Entries.Cast <EventLogEntry>().Where(evt => evt.Source == SourceName));
        }
        public void ClearLog()
        {
            EventLogPermission evtPermission = new EventLogPermission(EventLogPermissionAccess.Administer, MachineName);

            evtPermission.Demand();
            if (!IsNonCustomLog())
            {
                Log?.Clear();
            }
        }
        public void WriteToLog(string message, EventLogEntryType type, CategoryType category, EventIDType eventID, byte[] rawData)
        {
            if (Log == null)
            {
                throw (new ArgumentNullException(nameof(Log), "This Event Log has not been opened or has been closed."));
            }
            EventLogPermission evtPermission = new EventLogPermission(EventLogPermissionAccess.Write, MachineName);

            evtPermission.Demand();
            Log.WriteEntry(message, type, (int)eventID, (short)category, rawData);
        }
Example #4
0
        public void WriteToLog(string message, EventLogEntryType type,
                               CategoryType category, EventIDType eventID)
        {
            if (Log == null)
            {
                throw new ArgumentNullException(nameof(Log),
                                                "This Event Log has not been opened or has been closed.");
            }

            EventLogPermission eventLogPermission =
                new EventLogPermission(EventLogPermissionAccess.Write, MachineName);

            eventLogPermission.Demand();

            // If you get a SecurityException here, see the notes at the
            // top of the class
            Log.WriteEntry(message, type, (int)eventID, (short)category);
        }
Example #5
0
        private void Start()
        {
            if (Started)
            {
                return;
            }
            // Attach Listener to each EventLog
            var p = new EventLogPermission(EventLogPermissionAccess.Administer, ".");

            p.Demand();
            var mlogs = EventLog.GetEventLogs();

            foreach (var el in mlogs)
            {
                try
                {
                    el.EnableRaisingEvents = true;
                    el.EntryWritten       += EventHook;
                }
                catch (InvalidOperationException ex)
                {
                    if (Configuration.Verbose)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
                catch (SecurityException ex)
                {
                    if (Configuration.Verbose)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }
            }
            if (Configuration.Verbose)
            {
                Console.WriteLine("Service Started...");
            }
            Started = true;
        }