Skip to content

bbrandt/seq-api

 
 

Repository files navigation

Seq HTTP API Client

Build status

This library includes:

  • C# representations of the entities exposed by the Seq HTTP API
  • Helper classes for interacting with the API

It's useful for querying events and working with configuration data - everything you can do using the Seq web UI, you can do programmatically via the API.

If you want to write events to Seq, use one of the logging framework clients, such as Serilog.Sinks.Seq or Seq.Client.Slab instead.

Getting Started

Install from NuGet:

Install-Package Seq.Api

Create a SeqConnection with your server URL:

var connection = new SeqConnection("http://localhost:5341");

Navigate the "resource groups" exposed as properties of the connnection:

var installedApps = await connection.Apps.ListAsync();

To authenticate, the SeqConnection constructor accepts an apiKey parameter (make sure the API key permits user-level access) or, if you want to log in with personal credentials you can await connection.Users.Login(username, password).

For a more complete example, see the seq-tail app included in the source;

Reading Events

Seq internally limits the resources a query is allowed to consume. The query methods on SeqConnection.Events include a status with each result set - a Partial status indicates that further results must be retrieved.

The snippet below demonstrates paging through results to retrieve the complete set.

string lastReadEventId = null;

while(true)
{
  var resultSet = await connection.Events.InSignalAsync(
      filter: "Environment == \"Test\"",
      render: true,
      afterId: lastReadEventId);
      
  foreach (var evt in resultSet.Events)
    Console.WriteLine(evt.RenderedMessage);

  if (resultSet.Statistics.Status != ResultSetStatus.Partial)
    break;
    
  lastReadEventId = resultSet.Statistics.LastReadEventId;
}

If the result set is expected to be small, ListAsync() will buffer results and return a complete list:

var resultSet = await connection.Events.ListAsync(
    filter: "Environment == \"Test\"",
    render: true,
    count: 1000);
  
foreach (var evt in resultSet)
  Console.WriteLine(evt.RenderedMessage);

All methods that retrieve events require a count. The API client defaults this value to 30 if not specified.

Working with the Basic Client

The SeqApiClient class implements the low level interactions with the API's entities and links. It's one step up from System.Net.HttpClient - you may be able to use it in cases not supported by the high-level wrapper.

Create a SeqApiClient with your server URL:

var client = new SeqApiClient("http://localhost:5341");

Get the root resource and use it to retrieve one or more of the resource groups:

var root = await client.GetRootAsync();
var events = await client.GetAsync<ResourceGroup>(root, "EventsResources");

(Available resource groups, like events, users and so-on, can be seen in the root document's Links collection.)

Use the client to navigate links from entity to entity:

var matched = await client.List<EventEntity>(
  events,
  "Items",
  new Dictionary<string, object>{{"count", 10}, {"render", true}});

foreach (var match in matched)
  Console.WriteLine(matched.RenderedMessage);

Status

This library is under active development.

  • The entity types etc. are complete: they're the same ones Seq uses internally.
  • The helper classes such as SeqConnection and SeqApiClient are evolving and may change in response to feedback (and PRs!).

About

HTTP API client for the Seq .NET event server

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%