Spoleto.RdgSessionTracker 1.0.3

dotnet add package Spoleto.RdgSessionTracker --version 1.0.3
                    
NuGet\Install-Package Spoleto.RdgSessionTracker -Version 1.0.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Spoleto.RdgSessionTracker" Version="1.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Spoleto.RdgSessionTracker" Version="1.0.3" />
                    
Directory.Packages.props
<PackageReference Include="Spoleto.RdgSessionTracker" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Spoleto.RdgSessionTracker --version 1.0.3
                    
#r "nuget: Spoleto.RdgSessionTracker, 1.0.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Spoleto.RdgSessionTracker@1.0.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Spoleto.RdgSessionTracker&version=1.0.3
                    
Install as a Cake Addin
#tool nuget:?package=Spoleto.RdgSessionTracker&version=1.0.3
                    
Install as a Cake Tool

Spoleto.RdgSessionTracker

Spoleto.RdgSessionTracker is a .NET library for reading Remote Desktop Gateway (RD Gateway) session disconnect events from the Windows Event Log and calculating user summary working time based on session durations.

It is designed for scenarios where you need to track when users connect/disconnect through an RD Gateway and store the calculated working periods in a database or reporting system.


Features

  • Reads RD Gateway events (e.g., Event ID 303) from Microsoft-Windows-TerminalServices-Gateway log.
  • Filters events by date range (since / to).
  • Parses the event message to extract:
    • Username (DOMAIN\user)
    • Client IP address
    • Remote resource name
    • Session duration (seconds)
  • Groups events by user.
  • Calculates:
    • Total working time for the user (sum of all session durations).
    • Start of the working day = Last disconnect - Total duration.
    • End of the working day = Last disconnect.
  • Handles duplicate events for the same disconnect (different protocols, same time) — processes only the first unique event.
  • Supports reading logs locally or from a remote computer (via EventLogSession).

Example

var reader = new RdgEventReader();

// Load yesterday's events from the local or remote server
var events = reader.GetEvents(
    since: DateTime.Today.AddDays(-1),
    to: DateTime.Today,
    machineName: "sv-server" // set to null for local machine
	);
	
foreach (var event in events)
{
    Console.WriteLine($"{event.Date:yyyy-MM-dd} | {event.UserName} | ConnectTime: {event.ConnectTime} | DisconnectTime: {event.DisconnectTime} | DurationSeconds: {event.DurationSeconds}");
}	

// Calculate per-user summary sessions (start, end, total duration)
var summarySessions = reader.GetSummarySessions(
    since: DateTime.Today.AddDays(-1),
    to: DateTime.Today,
    machineName: "sv-server" // set to null for local machine
	);

foreach (var session in summarySessions)
{
    Console.WriteLine(
        $"{session.UserName}: Start={session.Start}, End={session.End}, Total={session.TotalDuration}"
    );
}

// Loads user events and merges consecutive events per user with DurationSeconds = (Disconnect - Connect).TotalSeconds
var mergedEvents = reader.GetMergedEvents(
    since: DateTime.Today.AddDays(-1),
    to: DateTime.Today,
    maxGap: TimeSpan.FromMinutes(1),
    machineName: "sv-server" // set to null for local machine
	);

foreach (var event in mergedEvents)
{
    Console.WriteLine($"{event.Date:yyyy-MM-dd} | {event.UserName} | ConnectTime: {event.ConnectTime} | DisconnectTime: {event.DisconnectTime} | DurationSeconds: {event.DurationSeconds}");
}

Event Source

The events come from:

Log Name: Microsoft-Windows-TerminalServices-Gateway/Operational Event ID: 303 — User disconnected from a network resource.

Example Event Text:

The user "DOMAIN\user", on client computer "10.0.0.10", disconnected from the following network resource: "sv-term.domain.com". Before the user disconnected, the client transferred 2403328 bytes and received 35761296 bytes. The client session duration was 2552 seconds. Connection protocol used: "UDP".

Requirements

  • Windows Server 2016+ / Windows 10+
  • RD Gateway role enabled
  • .NET 8.0 (Windows only)
  • Event log access permissions

Installation

dotnet add package Spoleto.RdgSessionTracker

License

MIT License

Product Compatible and additional computed target framework versions.
.NET net8.0-windows7.0 is compatible.  net9.0-windows was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.3 156 8/25/2025
1.0.2 64 8/22/2025
1.0.1 122 8/19/2025
1.0.0 161 8/14/2025