我正在为工作创建一个内部不活动记录器,我们最初希望这是一个 Windows 窗体应用程序,具有在后台工作的隐藏窗体,但是该程序出现在任务管理器中,我们现在希望此代码用于一个Windows服务,我想知道这是否可能?如果是这样,我需要一些帮助来实现它=]
这是应用程序的代码
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");
}
}
}
更新
错误 1 } 预期 C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 36 10 UserActivityMonitorBackGround 错误 2 方法必须有返回类型 C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 44 16 UserActivityMonitorBackGround 错误 3 预期的类、委托、枚举、接口或结构 C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 230 28 UserActivityMonitorBackGround错误 4 类型或命名空间定义,或预期文件结尾 C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 233 5 UserActivityMonitorBackGround 错误 5 类型或命名空间名称“Windows”命名空间“系统”中不存在(您是否缺少程序集引用?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 10 14 UserActivityMonitorBackGround 错误 6 类型或命名空间名称找不到“AccessEye”(您是否缺少 using 指令或程序集引用?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 12 7 UserActivityMonitorBackGround 错误 7 类型或找不到命名空间名称“EasyNetQ”(您是否缺少 using 指令或程序集引用?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 14 7 UserActivityMonitorBackGround 错误 8类型或命名空间名称“EasyNetQ”找不到(您是否缺少 using 指令或程序集引用?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 15 7 UserActivityMonitorBackGround 错误 9 类型或命名空间名称'找不到 MySql'(您是否缺少 using 指令或程序集引用?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 17 7 UserActivityMonitorBackGround 错误 10 类型或命名空间找不到名称“NLog”(您是否缺少 using 指令或程序集引用?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 18 7 UserActivityMonitorBackGround 错误 11 类型或命名空间名称“ProtoBuf”找不到(您是否缺少 using 指令或程序集引用?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 19 7 UserActivityMonitorBackGround Error 12 'UserActivityMonitorBackGround.Service1.Logger '是一个'字段',但用作'类型' C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 38 36 UserActivityMonitorBackGround 错误 13 名称 'LogManager' 不存在于当前上下文 C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 38 52 UserActivityMonitorBackGround 错误 14 找不到类型或命名空间名称“屏幕保护程序”(您是否缺少 using 指令或装配参考?)C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 39 17 UserActivityMonitorBackGround 错误 15 找不到类型或命名空间名称“Inactivity”(您是否缺少 using 指令或程序集引用?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 40 16 UserActivityMonitorBackGround 错误 16 找不到类型或命名空间名称“IAdvancedBus”(您是否缺少 using 指令或程序集引用?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 41 9 UserActivityMonitorBackGround 错误 17 找不到类型或命名空间名称“IExchange”(您是否缺少 using 指令或装配参考?)C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 42 9 UserActivityMonitorBackGround 错误 18 找不到类型或命名空间名称“Inactivity”(您是否缺少 using 指令或程序集引用?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 53 34 UserActivityMonitorBackGround 错误 19 找不到类型或命名空间名称“屏幕保护程序”(您是否缺少 using 指令或程序集引用?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 58 31 UserActivityMonitorBackGround 错误 20 当前上下文 C:\Users\reece 中不存在名称“LogManager”。cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 64 17 UserActivityMonitorBackGround 错误 21 当前上下文中不存在名称“RabbitHutch” C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround \Service1.cs 66 23 UserActivityMonitorBackGround 错误 22 当前上下文中不存在名称“Exchange” C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 67 28 UserActivityMonitorBackGround 错误 23 名称当前上下文中不存在“Exchange” C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 68 30 UserActivityMonitorBackGround 错误 24 'UserActivityMonitorBackGround.Service1'不包含“隐藏”的定义,并且找不到接受“UserActivityMonitorBackGround.Service1”类型的第一个参数的扩展方法“隐藏”(您是否缺少 using 指令或程序集引用?) C:\Users\reece。 cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 79 18 UserActivityMonitorBackGround 错误 25 当前上下文中不存在名称“TrayDisplayer” C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround \Service1.cs 80 13 UserActivityMonitorBackGround 错误 26 找不到类型或命名空间名称“LogData”(是否缺少 using 指令或程序集引用?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch \UserActivityMonitorBackGround\Service1。cs 83 38 UserActivityMonitorBackGround 错误 27 找不到类型或命名空间名称“LogService”(您是否缺少 using 指令或程序集引用?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\ Service1.cs 85 33 UserActivityMonitorBackGround 错误 28 找不到类型或命名空间名称“DbLogData”(您是否缺少 using 指令或程序集引用?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\ UserActivityMonitorBackGround\Service1.cs 87 17 UserActivityMonitorBackGround 错误 29 当前上下文 C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1 中不存在名称“AutoMapper”。cs 87 37 UserActivityMonitorBackGround 错误 30 找不到类型或命名空间名称“LogData”(是否缺少 using 指令或程序集引用?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\ Service1.cs 87 59 UserActivityMonitorBackGround 错误 31 找不到类型或命名空间名称“DbLogData”(您是否缺少 using 指令或程序集引用?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\ UserActivityMonitorBackGround\Service1.cs 87 68 UserActivityMonitorBackGround 错误 32 找不到类型或命名空间名称“EventType”(您是否缺少 using 指令或程序集引用?) C:\Users\reece.cottam\Documents\Visual Studio 2010\ OsWatch\UserActivityMonitorBackGround\Service1。cs 91 17 UserActivityMonitorBackGround 错误 33 找不到类型或命名空间名称“EventType”(是否缺少 using 指令或程序集引用?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\ Service1.cs 95 55 UserActivityMonitorBackGround 错误 34 当前上下文中不存在名称“LogDataFactory” C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 125 24 UserActivityMonitorBackGround 错误 35 名称 '当前上下文中不存在 AccessEye C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 126 30 UserActivityMonitorBackGround 错误 36 当前上下文中不存在名称“LogDataFactory” C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 132 24 UserActivityMonitorBackGround 错误 37 当前上下文中不存在名称“AccessEye” C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 133 30 UserActivityMonitorBackGround 错误 38 当前上下文中不存在名称“LogDataFactory” C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 139 24 UserActivityMonitorBackGround 错误 39 当前上下文中不存在名称 'UserStateEvents' C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 144 38 UserActivityMonitorBackGround 错误 40 名称 '当前上下文中不存在 UserStateEvents C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 147 38 UserActivityMonitorBackGround 错误 41 找不到类型或命名空间名称“AccessEye”(是您缺少 using 指令或程序集引用?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 154 36 UserActivityMonitorBackGround 错误 42 找不到类型或命名空间名称“LogData” (您是否缺少 using 指令或程序集引用?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 164 96 UserActivityMonitorBackGround 错误 43 类型或命名空间名称“消息”找不到(您是否缺少 using 指令或程序集引用?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 164 88 UserActivityMonitorBackGround 错误 44 类型或命名空间名称'找不到 EasyNetQException'(您是否缺少 using 指令或程序集引用?) C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 167 20 UserActivityMonitorBackGround 错误 45 名称 'Serializer ' 在当前上下文中不存在 C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 177 17 UserActivityMonitorBackGround 错误 46 当前上下文 C:\ 中不存在名称 'Serializer'用户\rece。cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 186 24 UserActivityMonitorBackGround 错误 47 找不到类型或命名空间名称“FormClosingEventArgs”(您是否缺少 using 指令或程序集引用?) C:\Users\ reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 190 55 UserActivityMonitorBackGround 错误 48 当前上下文中不存在名称“LogDataFactory” C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch \UserActivityMonitorBackGround\Service1.cs 199 24 UserActivityMonitorBackGround 错误 49 当前上下文 C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1 中不存在名称“UserStateEvents”。cs 200 30 UserActivityMonitorBackGround 错误 50 当前上下文中不存在名称“LogDataFactory” C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 209 24 UserActivityMonitorBackGround 错误 51 名称“UserStateEvents”当前上下文中不存在 C:\Users\reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 210 30 UserActivityMonitorBackGround 错误 52 当前上下文 C:\Users 中不存在名称“LogDataFactory” \reece.cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 218 24 UserActivityMonitorBackGround 错误 53 当前上下文 C:\Users\reece 中不存在名称“UserStateEvents”。cottam\Documents\Visual Studio 2010\OsWatch\UserActivityMonitorBackGround\Service1.cs 219 30 UserActivityMonitorBackGround