如何在Windows Server上编写VPN日志记录的侦听器?我想写一个应用程序,每当有人VPN进入我的Windows Server时都会提醒我。我能够编写侦听服务器登录的代码。所以我想写这样的代码,但对于VPN登录:
protected override void OnSessionChange(SessionChangeDescription e)
{
switch (e.Reason)
{
case SessionChangeReason.SessionLock:
break;
case SessionChangeReason.SessionLogon:
break;
case SessionChangeReason.SessionUnlock:
break;
case SessionChangeReason.ConsoleConnect:
break;
case SessionChangeReason.ConsoleDisconnect:
break;
case SessionChangeReason.RemoteConnect:
break;
case SessionChangeReason.RemoteDisconnect:
break;
}
}
答案 0 :(得分:1)
如果您使用的是Windows VPN,则可以使用两个选项。
默认情况下,日志文件保存在%windir%\system32\Logfiles\RRAS
中。您可以在那里解析日志文件。每个会话将有4行:用户名,登录时间和IP地址。当它发生时,注销将是第4个条目。您可以使用RRAS MMC更改日志记录目录>属性>日志记录。
另一个选项是您可以使用EventLog
使用并查找RRAS Audits
个条目,将Log
设置为Security
并将Source
设置为{RemoteAccess
1}}。查看其他属性的文档以确定您需要的属性。
您可以枚举所有条目:
EventLog log = EventLog.GetEventLogs()
.First(o => o.Log == "Security" && o.Source=="RemoteAccess");
foreach (EventLogEntry entry in log.Entries)
{
Console.WriteLine("\tEntry: " + entry.Message);
}
或者,您可以添加事件处理程序以进行实时监控:。
private void Log_EntryWritten(object sender, EntryWrittenEventArgs e)
{
string message = e.Entry.Message;
Console.WriteLine(message);
}
public void MonitorVPNLogs()
{
EventLog log = EventLog.GetEventLogs()
.First(o => o.Log == "Security" && o.Source=="RemoteAccess");
log.EnableRaisingEvents = true;
log.EntryWritten += Log_EntryWritten;
}