Home All Groups Group Topic Archive Search About
Author
25 Mar 2005 8:08 AM
Troy
Hello,

I'm just starting out with c# and WMI and have a few questions that seasoned
coders might be able to help me with. I'd like to write a console app that
would query a server and pull out all the errors in the system and
application log. I've started writing the class to do this but i'm stuck now
because I have no idea on how to do this... here's my code... any help is
appreciated.

using System;
using System.Collections.Generic;
using System.Text;
using System.Management;

#endregion

namespace EventlogWatcher
{
    public class EventLog
    {
        // variables to store server name and event log type
        private string serverName, logType;
        private const string user="softlnding";
        private const string password = "$0ftLand1ng";


        public EventLog(string serverName, string logType)
        {
            this.logType = logType;
            this.serverName = serverName;
        }

        private void GetLogs()
        {
            //Build an options object for the connection
            ConnectionOptions options = new ConnectionOptions();
            options.Username = user;
            options.Password = password;

            //Make a connection to a remote computer using these options
            ManagementScope scope = new
ManagementScope("\\\\Server1\\root\\cimv2", options);
            scope.Connect();

            SelectQuery query = new SelectQuery("Select * from
Win32_NTEventLogFile Where LogFileName='Application'");


        }

    }
}

Author
26 Mar 2005 12:39 AM
Dale Preston
You have done a great job on investigating the WMI connection requirements
but it isn't necessary for reading the event logs of a remote machine.
Here's how to do it.  The for loop displays all of the entries for the last
24 hours from newest to oldest.

// Create the event log object
eventLog = new System.Diagnostics.EventLog();
eventLog.MachineName = serverName;
eventLog.Log = "System";  // or "Application"  or any other log you want to
view

for(int x = eventLog.Entries.Count - 1; x >= 0; x--)
{
EventLogEntry entry = eventLog.Entries[x];
if (entry.TimeGenerated < DateTime.Now.AddDays(-1))
{
  break;
}
}

HTH

Dale Preston
MCAD, MCDBA, MCSE

Show quote
"Troy" <t***@gc.ca> wrote in message
news:eS6jLFRMFHA.1176@TK2MSFTNGP12.phx.gbl...
> Hello,
>
> I'm just starting out with c# and WMI and have a few questions that
seasoned
> coders might be able to help me with. I'd like to write a console app that
> would query a server and pull out all the errors in the system and
> application log. I've started writing the class to do this but i'm stuck
now
> because I have no idea on how to do this... here's my code... any help is
> appreciated.
>
> using System;
> using System.Collections.Generic;
> using System.Text;
> using System.Management;
>
> #endregion
>
> namespace EventlogWatcher
> {
>     public class EventLog
>     {
>         // variables to store server name and event log type
>         private string serverName, logType;
>         private const string user="softlnding";
>         private const string password = "$0ftLand1ng";
>
>
>         public EventLog(string serverName, string logType)
>         {
>             this.logType = logType;
>             this.serverName = serverName;
>         }
>
>         private void GetLogs()
>         {
>             //Build an options object for the connection
>             ConnectionOptions options = new ConnectionOptions();
>             options.Username = user;
>             options.Password = password;
>
>             //Make a connection to a remote computer using these options
>             ManagementScope scope = new
> ManagementScope("\\\\Server1\\root\\cimv2", options);
>             scope.Connect();
>
>             SelectQuery query = new SelectQuery("Select * from
> Win32_NTEventLogFile Where LogFileName='Application'");
>
>
>         }
>
>     }
> }
>
>
Author
26 Mar 2005 9:26 AM
_R
On Fri, 25 Mar 2005 18:39:39 -0600, "Dale Preston"
<dalepres@nospam.nospam> wrote:

Show quote
>Here's how to do it.  The for loop displays all of the entries for the last
>24 hours from newest to oldest.
>
>// Create the event log object
>eventLog = new System.Diagnostics.EventLog();
>eventLog.MachineName = serverName;
>eventLog.Log = "System";  // or "Application"  or any other log you want to
>view
>
>for(int x = eventLog.Entries.Count - 1; x >= 0; x--)
>{
> EventLogEntry entry = eventLog.Entries[x];
> if (entry.TimeGenerated < DateTime.Now.AddDays(-1))
> {
>  break;
> }
>}
>
>HTH

Doesn't this generate an access violation if you attempt to read
another (networked) machine's event queue?
Author
26 Mar 2005 12:32 PM
Dale Preston
Not if you're a domain administrator.

Dale

Show quote
"_R" <_*@nomail.org> wrote in message
news:tiaa411k03out3tcp23n605089pvqmn2e0@4ax.com...
> On Fri, 25 Mar 2005 18:39:39 -0600, "Dale Preston"
> <dalepres@nospam.nospam> wrote:
>
> >Here's how to do it.  The for loop displays all of the entries for the
last
> >24 hours from newest to oldest.
> >
> >// Create the event log object
> >eventLog = new System.Diagnostics.EventLog();
> >eventLog.MachineName = serverName;
> >eventLog.Log = "System";  // or "Application"  or any other log you want
to
> >view
> >
> >for(int x = eventLog.Entries.Count - 1; x >= 0; x--)
> >{
> > EventLogEntry entry = eventLog.Entries[x];
> > if (entry.TimeGenerated < DateTime.Now.AddDays(-1))
> > {
> >  break;
> > }
> >}
> >
> >HTH
>
> Doesn't this generate an access violation if you attempt to read
> another (networked) machine's event queue?
>
Author
26 Mar 2005 6:56 PM
Troy
dude you rock....

Show quote
"Dale Preston" <dalepres@nospam.nospam> wrote in message
news:OC8an$fMFHA.1268@TK2MSFTNGP14.phx.gbl...
> Not if you're a domain administrator.
>
> Dale
>
> "_R" <_*@nomail.org> wrote in message
> news:tiaa411k03out3tcp23n605089pvqmn2e0@4ax.com...
> > On Fri, 25 Mar 2005 18:39:39 -0600, "Dale Preston"
> > <dalepres@nospam.nospam> wrote:
> >
> > >Here's how to do it.  The for loop displays all of the entries for the
> last
> > >24 hours from newest to oldest.
> > >
> > >// Create the event log object
> > >eventLog = new System.Diagnostics.EventLog();
> > >eventLog.MachineName = serverName;
> > >eventLog.Log = "System";  // or "Application"  or any other log you
want
> to
> > >view
> > >
> > >for(int x = eventLog.Entries.Count - 1; x >= 0; x--)
> > >{
> > > EventLogEntry entry = eventLog.Entries[x];
> > > if (entry.TimeGenerated < DateTime.Now.AddDays(-1))
> > > {
> > >  break;
> > > }
> > >}
> > >
> > >HTH
> >
> > Doesn't this generate an access violation if you attempt to read
> > another (networked) machine's event queue?
> >
>
>

AddThis Social Bookmark Button