Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 863687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:21:15+00:00 2026-05-15T09:21:15+00:00

I’m using EventLog to support a logging class in my C# application. ( Previously…

  • 0

I’m using EventLog to support a logging class in my C# application. (Previously…) Here’s a whittled down copy of that class:

class Logger
{
    private EventLog eventLog;
    private ListView listViewControl = null;
    private String logSource = "SSD1";

    public Logger(ListView _listViewControl = null, string _logFileName = null)
    {
        if (!EventLog.SourceExists("SSD1"))
            EventLog.CreateEventSource(logSource, "Application");
        eventLog = new EventLog();
        eventLog.Source = logSource;
        addListView(_listViewControl);
        logFilename = _logFileName;
    }

    public void addListView(ListView newListView)
    {
        if (eventLog.Entries.Count > 0)
        {
            foreach (EventLogEntry entry in eventLog.Entries)
            {
                listViewControl.Items.Add(buildListItem(entry));
            }
        }
    }

    public void LogInformation(string message)
    {
        LogEntry(message, EventLogEntryType.Information);
    }

    private void LogEntry(string message, EventLogEntryType logType)
    {
        eventLog.WriteEntry(message, logType);
        if (listViewControl != null)
        {
            updateListView();
        }
    }

    private void updateListView()
    {
        listViewControl.Items.Add(buildListItem(eventLog.Entries[eventLog.Entries.Count-1]));
    }

    private ListViewItem buildListItem(EventLogEntry entry)
    {
        string[] eventArray = new string[3];
        eventArray[0] = entry.Message + " (" + entry.Source +")";
        eventArray[1] = entry.EntryType.ToString();
        eventArray[2] = entry.TimeGenerated.ToString("dd/MM/yyyy - HH:mm:ss");
        return new ListViewItem(eventArray);
    }

The problem is, the ListView gets populated with the whole of the log – not just those from the specified source. Here’s a screenshot of the output:

Entries from all sources http://img341.imageshack.us/img341/6185/entriesfromalllogs.png

(In this image, the source of each entry is in brackets after the message.)

How do I get the EventLog to return only those entries from my source? Have I misunderstood EventLog completely?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T09:21:15+00:00Added an answer on May 15, 2026 at 9:21 am

    The EventLog.Source member doesn’t work as a filter. According to the MSDN documentation for EventLog

    To read from a log, specify the Log
    name and MachineName (server computer
    name) for the EventLog. It is not
    necessary to specify the Source, as a
    source is required only for writing to
    logs
    . The Entries member is
    automatically populated with the event
    log’s list of entries.

    Because you do not specify a string for the Log member of the EventLog instance, it is getting everything.

    Off the top of my head, there are a couple of ways to deal with this.

    First, modify buildListItem() to filter on your source name. This is relatively straightforward.

    Second, create your own log. Instead of logging to the Application log, create a log specifically for your application. You can do this by altering your constructor:

    public Logger(ListView _listViewControl = null, string _logFileName = null)   
    {   
        if (!EventLog.SourceExists("SSD1"))   
            EventLog.CreateEventSource("SSD1", "TomWrightApplication");   
        eventLog = new EventLog("TomWrightApplication", ".", "SSD1");
        addListView(_listViewControl);   
        logFilename = _logFileName;   
    }   
    

    All logging will now go to the TomWrightApplication log, not the generic Application log.


    Tom, I’ve got a test project that simply does the following:

    static void Main()
    {
        if (!EventLog.SourceExists("SSD1"))
            EventLog.CreateEventSource("SSD1", "SSDAppLog");
        EventLog log = new EventLog("SSDAppLog", ".", "SSD1");
        log.WriteEntry("this is a test");
    }
    

    This works successfully unless…the SSD1 source name is already registered with another log. As I understand it, the source name must be unique across all event logs. So, if you’ve already registered SSD1 with the Application log, the above code will fail when creating the new EventLog. Try using the EventLog.DeleteEventSource() to remove the SSD1 source name from the Application log (just run this one time for your system). The above code should work then (assuming you have admin privileges).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 450k
  • Answers 450k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Try this: $('#btnRecalculate').trigger('click'); More info here: http://api.jquery.com/trigger/ May 15, 2026 at 8:23 pm
  • Editorial Team
    Editorial Team added an answer found out the solution its in the web.xml change the… May 15, 2026 at 8:23 pm
  • Editorial Team
    Editorial Team added an answer It is perfectly valid to allocate memory in one thread… May 15, 2026 at 8:23 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.