Monday, November 15, 2010

View Event Log using WMI in C#.NET

I wrote this for my MobileExec Program to show the most recent events in the event log. You can see the results in the screenshot above. The WMI SQL query gets kind of cryptic, but when implemented can be quite powerful. Check out the code below:



   Label1.ForeColor = Color.Black;
                    Label1.Text = "Sorry! No Logs Found by that description!";
                    ConnectionOptions co = new ConnectionOptions();
                    co.Username = txtUsername.Text;
                    co.Password = txtPassword.Text;
                    co.Impersonation = ImpersonationLevel.Impersonate;
                    co.EnablePrivileges = true;
                    co.Authentication = AuthenticationLevel.PacketPrivacy;
                    string code2 = "1";
                    string code = words[2].ToString().ToLower();
                    if (code.ToLower() == "error")
                    {
                        code2 = "1";
                    }
                    if (code.ToLower() == "warning")
                    {
                        code2 = "2";
                    }
                    if (code.ToLower() == "information")
                    {
                        code2 = "3";
                    }
                    if (code.ToLower() == "success")
                    {
                        code2 = "4";
                    }
                    if (code.ToLower() == "failure")
                    {
                        code2 = "5";
                    }
                    
                    string queryStr = "select * from Win32_NTLogEvent where Logfile = '";
                    queryStr += words[1].ToString();
                    queryStr += "' and EventType = " + code2 + "";


                    System.Net.IPHostEntry h =
                System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());
                    string IPAddress = h.AddressList.GetValue(0).ToString();
                    string lm = System.Net.Dns.GetHostName().ToString();


                    if (txtUsername.Text == "" || txtPassword.Text == ""
                 || txtServer.Text == "127.0.0.1"
                 || txtServer.Text.ToLower() == "localhost"
                 || txtServer.Text.ToLower() == lm.ToLower()
                 || txtServer.Text.ToLower() == IPAddress.ToLower())
                    {
                        co.Username = null;
                        co.Password = null;
                    }


                 
                    string temp1 = txtServer.Text;
                    System.Management.ManagementScope ms =
                        new System.Management.ManagementScope("\\\\" + temp1 + "\\root\\cimv2", co);


                    ObjectQuery oQuery = new ObjectQuery(queryStr);


                    //Execute the query  
                    ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(ms, oQuery);


                    //Get the results
                    ManagementObjectCollection oReturnCollection = oSearcher.Get();


                    this.Label1.Style["text-align"] = "left";
                    Label1.ForeColor = Color.Black;
                    
                    Label1.Text = "<br />Viewing Most Recent <b>" +
                          words[1].ToString().ToLower() + "</b> log entries: " + 
                          words[2].ToString().ToLower() + "<br />";
                    Label1.Text += "-----------------------------------" + "<br>";
                       
                    foreach (ManagementObject oReturn in oReturnCollection)
                    {
                       
                        Label1.Text += "<b>Time Written:</b> " + oReturn["TimeWritten"] + "<br>";
                        Label1.Text += "<b>Event Code:</b> " + oReturn["EventCode"] + "<br>";
                        Label1.Text += "<b>Message:</b> " + oReturn["Message"] + "<br>";
                        Label1.Text += "-----------------------------------" + "<br>";
                        //oReturn.InvokeMethod("ClearEventLog", obj);
                        //oReturn.Dispose();
                    }

No comments:

Post a Comment

Generating "Always On Top" NSWindow in macOS across all detected displays

Also: Using UIKit & Cocoa Frameworks using Objective-C In m acOS or OS X , written in either Objective-C or Swift  Langues, you m...