Skip to content

Instantly share code, notes, and snippets.

@RhysC
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RhysC/aa8e824688a3612a4d53 to your computer and use it in GitHub Desktop.
Save RhysC/aa8e824688a3612a4d53 to your computer and use it in GitHub Desktop.
Windows service logging user session starts (note this is a project type of windows service, not all files show)
This Page Intentionally Left Blank
@ECHO OFF
REM Using the following directory is for .NET
set DOTNETFX4=%SystemRoot%\Microsoft.NET\Framework\v4.0.30319
set PATH=%PATH%;%DOTNETFX4%
echo Installing UserLoggedOnService...
echo ---------------------------------------------------
InstallUtil /i UserLoggedOnService.exe
echo ---------------------------------------------------
echo Done.
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="log4net" version="2.0.3" targetFramework="net45" />
</packages>
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
namespace UserLoggedOnService
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
}
}
}
using System;
using System.Reflection;
using System.ServiceProcess;
using log4net;
using log4net.Config;
[assembly: XmlConfigurator(ConfigFile = "Log4net.config", Watch = true)]
namespace UserLoggedOnService
{
public partial class Service1 : ServiceBase
{
private static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public Service1()
{
CanHandleSessionChangeEvent = true;
CanHandlePowerEvent = true;
InitializeComponent();
}
protected override void OnStart(string[] args)
{
Logger.Info("Service started");
base.OnStart(args);
}
protected override void OnContinue()
{
Logger.Info("OnContinue");
base.OnContinue();
}
protected override void OnPause()
{
Logger.Info("OnPause");
base.OnPause();
}
protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus)
{
Logger.Info("OnPowerEvent : " + powerStatus);
return base.OnPowerEvent(powerStatus);
}
protected override void OnSessionChange(SessionChangeDescription changeDescription)
{
Logger.Info("OnSessionChange : " + changeDescription.Reason + " : " + changeDescription.SessionId);
var user = System.Security.Principal.WindowsIdentity.GetCurrent();
Logger.Info("OnSessionChange WindowsIdentity.GetCurrent() - : " + (user != null ? user.Name : "[null]"));
Logger.Info("OnSessionChange Environment.UserName - : " + Environment.UserName);
base.OnSessionChange(changeDescription);
}
protected override void OnShutdown()
{
Logger.Info("OnShutdown");
base.OnShutdown();
}
protected override void OnStop()
{
Logger.Info("Service stopped");
}
}
}
@ECHO OFF
REM Using the following directory is for .NET
set DOTNETFX4=%SystemRoot%\Microsoft.NET\Framework\v4.0.30319
set PATH=%PATH%;%DOTNETFX4%
echo Uninstalling UserLoggedOnService...
echo ---------------------------------------------------
InstallUtil /u UserLoggedOnService.exe
echo ---------------------------------------------------
echo Done.
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;
namespace UserLoggedOnService
{
//http://www.codeproject.com/Articles/14353/Creating-a-Basic-Windows-Service-in-C
[RunInstaller(true)]
public class WindowsServiceInstaller : Installer
{
/// <summary>
/// Public Constructor for WindowsServiceInstaller.
/// - Put all of your Initialization code here.
/// </summary>
public WindowsServiceInstaller()
{
var serviceProcessInstaller = new ServiceProcessInstaller();
var serviceInstaller = new ServiceInstaller();
//# Service Account Information
serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
serviceProcessInstaller.Username = null;
serviceProcessInstaller.Password = null;
//# Service Information
serviceInstaller.DisplayName = "UserLoggedOnService";
serviceInstaller.StartType = ServiceStartMode.Automatic;
//# This must be identical to the WindowsService.ServiceBase name
//# set in the constructor of WindowsService.cs
serviceInstaller.ServiceName = "UserLoggedOnService";
this.Installers.Add(serviceProcessInstaller);
this.Installers.Add(serviceInstaller);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment