Link to home
Start Free TrialLog in
Avatar of whorsfall
whorsfallFlag for Australia

asked on

C# EntryWrittenEventHandler Question

Hi,

How can I unbox and C# object reference and covert it to "System.Diagnostics.EventLogInternal". Specifically I want to be able to get the source.log parameter.
(I want to have an event watcher for multiple logs).

Thanks,

Ward.

Here is my code below:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;

namespace Event_Tracer
{
    public partial class MainForm : Form
    {
        EventLog myLog;
        private static MainForm _instance;

        public MainForm()
        {
            InitializeComponent();
            _instance = this;
        }

        private void Button_Scan_Click(object sender, EventArgs e)
        {
                string watchLog = "Application";
                myLog = new EventLog(watchLog);
                // set event handler
                myLog.EntryWritten += new EntryWrittenEventHandler(OnEntryWritten);
                myLog.EnableRaisingEvents = true;

        }

        private void fnDisplay_Event(EventLogEntry e,object source)
        {

            dataGridView_events.Invoke(new Action(() =>
            {
                dataGridView_events.Rows.Add(source,e.TimeGenerated, e.EntryType,e.InstanceId,e.Message);
            }));

        }

        private static void OnEntryWritten(object source, EntryWrittenEventArgs e)
        {

          // I want to be able to look at source.log

            EventLogEntry eventLogEntry = e.Entry;                        
            
            _instance.fnDisplay_Event(eventLogEntry,source);
        }

        private void Button_Stop_Click(object sender, EventArgs e)
        {
            myLog.EnableRaisingEvents = false;
            myLog.EntryWritten -= OnEntryWritten;

        }

        private void Button_Write_Event_Click(object sender, EventArgs e)
        {
            using (EventLog eventLog = new EventLog("Application"))
            {
                eventLog.Source = "Application";
                eventLog.WriteEntry("Log message example", EventLogEntryType.Information, 101, 1);
            }
        }

        private void Button_Exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

Open in new window

Avatar of kaufmed
kaufmed
Flag of United States of America image

What do you mean by "unbox"? The terms "box" and "unbox" have a very specific meaning in .NET, and I don't see anything in the above that indicates that is to which you are referring.
Avatar of whorsfall

ASKER

Hi kaufmed,


Bug thanks for your response. Sorry for not being clearer - how I can get access to the property.

source.Log

If I try a cast like the following.

I get the error

var test = (EventLogInternal)source;

Severity      Code      Description      Project      File      Line      Suppression State
Error      CS0122      'EventLogInternal' is inaccessible due to its protection level      

Thanks,

Ward

User generated image
That's an internal class. You won't be able to use it/cast with it.
Not sure if this will be helpful, as I'm not entirely sure what info you need back in the DataGridView.  But if you're just looking to get the source.Log property, this is already inherently known, because you are creating an event handler for this specific EventLog called "Application" which is the same info in source.Log. However, because you inherently know which event log it is you should be able to pull whatever properties you need from the EventLog object and pass them through to the DataGridView.

For the other other logs, just create another event handler method specific to that EventLog.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial