Showing posts with label .NET Logging Tools. Show all posts
Showing posts with label .NET Logging Tools. Show all posts

Monday, 3 August 2020

Start Stop Window Service of Remote Machine

using System;
using System.Drawing;
using System.Management;
using System.Web.Configuration;
using System.Web.UI;

namespace WebApplication2_winservice
{
    public partial class _Default : Page
    {
        string serviceName = WebConfigurationManager.AppSettings["ServiceName"].ToString();
        string machineName = WebConfigurationManager.AppSettings["MachineName"].ToString();
        string userName = WebConfigurationManager.AppSettings["UserName"].ToString();
        string password = WebConfigurationManager.AppSettings["Password"].ToString();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                StatusShow();
            }
        }
        private void StatusShow()
        {
            string serviceStatus = Convert.ToString(ViewState["Status"]);
            if (serviceStatus != "")
            {
                if (serviceStatus == "Running")
                {
                    LblServiceStatus.ForeColor = Color.Green;
                    LblServiceStatus.Text = "Deploy Service is Running!";
                }
                else
                {
                    LblServiceStatus.ForeColor =Color.Red;
                    LblServiceStatus.Text = "Deploy Service is Stopped!";
                }

            }
            else if (ServiceStatus())
            {
                LblServiceStatus.ForeColor = Color.Green;
                LblServiceStatus.Text = "Deploy Service is Running!";
            }
            else
            {
                LblServiceStatus.ForeColor = Color.Red;
                LblServiceStatus.Text = "Deploy Service is Stopped!";
            }
        }
        private void StartStopService()
        {         
            ConnectionOptions connectoptions = new ConnectionOptions();
            connectoptions.Username = userName;
            connectoptions.Password = password;

            //IP Address of the remote machine
            ManagementScope scope = new ManagementScope(@"\\" + machineName + @"\root\cimv2");
            scope.Options = connectoptions;

            //WMI query to be executed on the remote machine
            SelectQuery query = new SelectQuery("select * from Win32_Service where name = '" + serviceName + "'");

            using (ManagementObjectSearcher searcher = new
                        ManagementObjectSearcher(scope, query))
            {
                ManagementObjectCollection collection = searcher.Get();
                foreach (ManagementObject service in collection)
                {

                    if (service["Started"].Equals(true))
                    {
                        //Stop the service                       
                        service.InvokeMethod("StopService", null);
                        ViewState["Status"] = "Running";
                    }
                    if (service["Started"].Equals(false))
                    {
                        //Start the service                     
                        service.InvokeMethod("StartService", null);
                        ViewState["Status"] = "Stopped";
                    }

                }
            }
            Response.Redirect("Default.aspx");
        }
        private bool ServiceStatus()
        {
            bool status = false;
           
            ConnectionOptions connectoptions = new ConnectionOptions();
            connectoptions.Username = userName;
            connectoptions.Password = password;

            //IP Address of the remote machine
            ManagementScope scope = new ManagementScope(@"\\" + machineName + @"\root\cimv2");
            scope.Options = connectoptions;

            //WMI query to be executed on the remote machine
            SelectQuery query = new SelectQuery("select * from Win32_Service where name = '" + serviceName + "'");

            using (ManagementObjectSearcher searcher = new
                        ManagementObjectSearcher(scope, query))
            {
                ManagementObjectCollection collection = searcher.Get();
                foreach (ManagementObject service in collection)
                {

                    if (service["Started"].Equals(true))
                    {
                        status = true;
                    }
                    if (service["Started"].Equals(false))
                    {
                        status = false;
                    }
                }
            }
            return status;
        }
        protected void btnStartStop_Click(object sender, EventArgs e)
        {
            StartStopService();
        }
    }
}

Wednesday, 2 August 2017

.NET Logging Tools, plugins, open source



.NET Logging Tools, plugins, open source

 

There are multiple tools and plugins available for .net application logging, based on its license, easy to use, easy pluggable, least impact on performance and best fit into .net environment, below are the tools listed on priority wise.  

ELMAH
ELMAH (Error Logging Modules and Handlers) is an application-wide error logging facility that is completely pluggable. 

Log4Net
The Apache log4net library is a tool to help the programmer output log statements to a variety of output targets. Log4net is a port of the excellent Apache log4j framework to the Microsoft.NET run time. 

Microsoft Enterprise Library
Logging Application Block. Developers can use this application block to include logging functionality for a wide range of logging targets in their applications. This release adds asynchronous logging capabilities.

Smart Inspect
Rich Logging & Tracing Track messages, errors, objects, database results & more. Logging support to any .NET desktop application, ASP.NET server project or multi-tier database solution.