I'm creating an internal inactivity logger for work, we initially wanted this to be a windows form application with a hidden windows form that works in the background, however the program appears in the task manager and we now want this code to be used in a windows service, I was wondering if this is possible? if so I'd like some help implementing that =]
this is the code for the Application
using System;
using System.IO;
using System.Windows.Forms;
using System.ComponentModel;
using AccessEye;
using System.Linq;
using EasyNetQ;
using EasyNetQ.Topology;
using Microsoft.Win32;
using MySql.Data.MySqlClient;
using NLog;
using ProtoBuf;
using System.Threading;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Runtime.InteropServices;
using System.Reflection;
namespace LogProgram
{
public partial class AppForm : Form
{
public static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private Screensaver watcher;
public Inactivity inactivity;
IAdvancedBus bus;
IExchange exchange;
public AppForm()
{
InitializeComponent();
ConfigureForm();
// todo: should be in setting
int pollingInterval = 5000;
inactivity = new Inactivity(pollingInterval);
inactivity.Inactive += inactivity_Inactive;
inactivity.Active += inactivity_Active;
inactivity.InactivityThresholdMs = 5 * 1000; // todo: should be in setting
inactivity.Start();
watcher = new Screensaver(pollingInterval);
watcher.ScreensaverOff += watcher_ScreensaverOff;
watcher.ScreensaverOn += watcher_ScreensaverOn;
watcher.Start();
SystemEvents.SessionEnding += SystemEvents_SessionEnding;
SystemEvents.SessionSwitch += SystemEvents_SessionSwitch;
LogManager.ThrowExceptions = true;
// todo: connection string should be in setting
bus = RabbitHutch.CreateBus("host=as01.access.local;virtualHost=DEV-Reece;username=reece;password=reece").Advanced;
exchange = Exchange.DeclareTopic("UserActivity", true, false, null);
var fanout = Exchange.DeclareFanout("FanoutExchange", true, false, null);
fanout.BindTo(exchange, new[] { "#" });
}
public void ConfigureForm()
{
this.Hide();
TrayDisplayer.Visible = false;
}
public void WriteLogDataToDb(LogData data)
{
using (var db = new LogService.UserActivityDataContext())
{
DbLogData logData = AutoMapper.Mapper.Map<LogData, DbLogData>(data);
int t = (int)data.EventType;
EventType eventType = db.EventTypes.FirstOrDefault(r => r.Id == t);
if (eventType == null)
{
eventType = db.EventTypes.Add(new EventType
{
Event = GetEnumDescriptionAttributeValue(data.EventType),
Id = (int)data.EventType
});
db.SaveChanges();
}
logData.EventTypeId = eventType.Id;
db.LogEvents.Add(logData);
db.SaveChanges();
}
}
public static string GetEnumDescriptionAttributeValue(Enum value)
{
var fieldInfo = value.GetType().GetField(value.ToString());
var attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
return attributes.Length > 0 ? attributes[0].Description : value.ToString();
}
private void AppForm_Load(object sender, EventArgs e)
{
}
void watcher_ScreensaverOn(object sender, EventArgs e)
{
var data = LogDataFactory.CollectData();
data.EventType = AccessEye.UserStateEvents.ScreensaverOn;
PublishLogData(data);
}
void watcher_ScreensaverOff(object sender, EventArgs e)
{
var data = LogDataFactory.CollectData();
data.EventType = AccessEye.UserStateEvents.ScreensaverOff;
PublishLogData(data);
}
void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
{
var data = LogDataFactory.CollectData();
switch (e.Reason)
{
case SessionSwitchReason.SessionLock:
data.EventType = UserStateEvents.Lock;
break;
case SessionSwitchReason.SessionUnlock:
data.EventType = UserStateEvents.Unlock;
break;
}
PublishLogData(data);
}
public void PublishLogData(AccessEye.LogData LogData)
{
WriteLogDataToDb(LogData);
if (!bus.IsConnected) return;
try
{
using (var publishChannel = bus.OpenPublishChannel())
{
publishChannel.Publish(exchange, LogData.EventType.ToString(), new Message<LogData>(LogData));
}
}
catch (EasyNetQException)
{
//todo: handle
}
}
public static byte[] Serialize<T>(T instance)
{
using (var stream = new MemoryStream())
{
Serializer.Serialize(stream, instance);
return stream.ToArray();
}
}
private static T DeSerialize<T>(byte[] data)
{
using (var stream = new MemoryStream(data))
{
return Serializer.Deserialize<T>(stream);
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
bus.Dispose();
}
public void inactivity_Active(object sender, EventArgs e)
{
inactivity.Stop();
var data = LogDataFactory.CollectData();
data.EventType = UserStateEvents.Active;
PublishLogData(data);
inactivity.Start();
}
public void inactivity_Inactive(object sender, EventArgs e)
{
inactivity.Stop();
var data = LogDataFactory.CollectData();
data.EventType = UserStateEvents.Inactive;
PublishLogData(data);
inactivity.Start();
}
public void SystemEvents_SessionEnding(object sender, EventArgs e)
{
var data = LogDataFactory.CollectData();
data.EventType = UserStateEvents.Logoff;
PublishLogData(data);
Logger.Trace("Logged off");
}
}
}
UPDATE
Error 1 } expected C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 36 10 UserActivityMonitorBackGround Error 2 Method must have a return type C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 44 16 UserActivityMonitorBackGround Error 3 Expected class, delegate, enum, interface, or struct C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 230 28 UserActivityMonitorBackGround Error 4 Type or namespace definition, or end-of-file expected C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 233 5 UserActivityMonitorBackGround Error 5 The type or namespace name 'Windows' does not exist in the namespace 'System' (are you missing an assembly reference?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 10 14 UserActivityMonitorBackGround Error 6 The type or namespace name 'AccessEye' could not be found (are you missing a using directive or an assembly reference?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 12 7 UserActivityMonitorBackGround Error 7 The type or namespace name 'EasyNetQ' could not be found (are you missing a using directive or an assembly reference?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 14 7 UserActivityMonitorBackGround Error 8 The type or namespace name 'EasyNetQ' could not be found (are you missing a using directive or an assembly reference?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 15 7 UserActivityMonitorBackGround Error 9 The type or namespace name 'MySql' could not be found (are you missing a using directive or an assembly reference?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 17 7 UserActivityMonitorBackGround Error 10 The type or namespace name 'NLog' could not be found (are you missing a using directive or an assembly reference?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 18 7 UserActivityMonitorBackGround Error 11 The type or namespace name 'ProtoBuf' could not be found (are you missing a using directive or an assembly reference?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 19 7 UserActivityMonitorBackGround Error 12 'UserActivityMonitorBackGround.Service1.Logger' is a 'field' but is used like a 'type' C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 38 36 UserActivityMonitorBackGround Error 13 The name 'LogManager' does not exist in the current context C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 38 52 UserActivityMonitorBackGround Error 14 The type or namespace name 'Screensaver' could not be found (are you missing a using directive or an assembly reference?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 39 17 UserActivityMonitorBackGround Error 15 The type or namespace name 'Inactivity' could not be found (are you missing a using directive or an assembly reference?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 40 16 UserActivityMonitorBackGround Error 16 The type or namespace name 'IAdvancedBus' could not be found (are you missing a using directive or an assembly reference?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 41 9 UserActivityMonitorBackGround Error 17 The type or namespace name 'IExchange' could not be found (are you missing a using directive or an assembly reference?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 42 9 UserActivityMonitorBackGround Error 18 The type or namespace name 'Inactivity' could not be found (are you missing a using directive or an assembly reference?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 53 34 UserActivityMonitorBackGround Error 19 The type or namespace name 'Screensaver' could not be found (are you missing a using directive or an assembly reference?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 58 31 UserActivityMonitorBackGround Error 20 The name 'LogManager' does not exist in the current context C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 64 17 UserActivityMonitorBackGround Error 21 The name 'RabbitHutch' does not exist in the current context C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 66 23 UserActivityMonitorBackGround Error 22 The name 'Exchange' does not exist in the current context C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 67 28 UserActivityMonitorBackGround Error 23 The name 'Exchange' does not exist in the current context C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 68 30 UserActivityMonitorBackGround Error 24 'UserActivityMonitorBackGround.Service1' does not contain a definition for 'Hide' and no extension method 'Hide' accepting a first argument of type 'UserActivityMonitorBackGround.Service1' could be found (are you missing a using directive or an assembly reference?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 79 18 UserActivityMonitorBackGround Error 25 The name 'TrayDisplayer' does not exist in the current context C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 80 13 UserActivityMonitorBackGround Error 26 The type or namespace name 'LogData' could not be found (are you missing a using directive or an assembly reference?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 83 38 UserActivityMonitorBackGround Error 27 The type or namespace name 'LogService' could not be found (are you missing a using directive or an assembly reference?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 85 33 UserActivityMonitorBackGround Error 28 The type or namespace name 'DbLogData' could not be found (are you missing a using directive or an assembly reference?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 87 17 UserActivityMonitorBackGround Error 29 The name 'AutoMapper' does not exist in the current context C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 87 37 UserActivityMonitorBackGround Error 30 The type or namespace name 'LogData' could not be found (are you missing a using directive or an assembly reference?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 87 59 UserActivityMonitorBackGround Error 31 The type or namespace name 'DbLogData' could not be found (are you missing a using directive or an assembly reference?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 87 68 UserActivityMonitorBackGround Error 32 The type or namespace name 'EventType' could not be found (are you missing a using directive or an assembly reference?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 91 17 UserActivityMonitorBackGround Error 33 The type or namespace name 'EventType' could not be found (are you missing a using directive or an assembly reference?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 95 55 UserActivityMonitorBackGround Error 34 The name 'LogDataFactory' does not exist in the current context C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 125 24 UserActivityMonitorBackGround Error 35 The name 'AccessEye' does not exist in the current context C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 126 30 UserActivityMonitorBackGround Error 36 The name 'LogDataFactory' does not exist in the current context C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 132 24 UserActivityMonitorBackGround Error 37 The name 'AccessEye' does not exist in the current context C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 133 30 UserActivityMonitorBackGround Error 38 The name 'LogDataFactory' does not exist in the current context C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 139 24 UserActivityMonitorBackGround Error 39 The name 'UserStateEvents' does not exist in the current context C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 144 38 UserActivityMonitorBackGround Error 40 The name 'UserStateEvents' does not exist in the current context C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 147 38 UserActivityMonitorBackGround Error 41 The type or namespace name 'AccessEye' could not be found (are you missing a using directive or an assembly reference?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 154 36 UserActivityMonitorBackGround Error 42 The type or namespace name 'LogData' could not be found (are you missing a using directive or an assembly reference?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 164 96 UserActivityMonitorBackGround Error 43 The type or namespace name 'Message' could not be found (are you missing a using directive or an assembly reference?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 164 88 UserActivityMonitorBackGround Error 44 The type or namespace name 'EasyNetQException' could not be found (are you missing a using directive or an assembly reference?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 167 20 UserActivityMonitorBackGround Error 45 The name 'Serializer' does not exist in the current context C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 177 17 UserActivityMonitorBackGround Error 46 The name 'Serializer' does not exist in the current context C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 186 24 UserActivityMonitorBackGround Error 47 The type or namespace name 'FormClosingEventArgs' could not be found (are you missing a using directive or an assembly reference?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 190 55 UserActivityMonitorBackGround Error 48 The name 'LogDataFactory' does not exist in the current context C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 199 24 UserActivityMonitorBackGround Error 49 The name 'UserStateEvents' does not exist in the current context C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 200 30 UserActivityMonitorBackGround Error 50 The name 'LogDataFactory' does not exist in the current context C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 209 24 UserActivityMonitorBackGround Error 51 The name 'UserStateEvents' does not exist in the current context C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 210 30 UserActivityMonitorBackGround Error 52 The name 'LogDataFactory' does not exist in the current context C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 218 24 UserActivityMonitorBackGround Error 53 The name 'UserStateEvents' does not exist in the current context C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 219 30 UserActivityMonitorBackGround