DocumentationDiscussions
DocumentationDiscussions

Writing Seq Apps in C#

Seq apps can be written as simple C# classes packaged into standard NuGet .nupkg files.

πŸ“˜

Example with full source code available

See the Seq.App.HttpRequest project for a complete example of a real-world Seq app.

The example below shows a snippet from the Formatted Email app, with full source available on GitHub.

Creating a new App Project

Apps are implemented in regular .NET 4.5 class libraries.

1394

Once the project has been created, install the Seq.Apps NuGet package.

dotnet add package Seq.Apps

Declaring the main app class

The logic of the app lives in a subclass of SeqApp.

[SeqApp("Formatted Email",
    Description = "Uses a provided template to send events as formatted email.")]
public class EmailApp : SeqApp, ISubscribeTo<LogEventData>
{
    [SeqAppSetting(
        DisplayName = "From address",
        HelpText = "The account from which the email is being sent.")]
    public string From { get; set; }

Writing the OnAsync() method

The rest of the plugin is just as straight-forward: a few more fields, an On method, and some formatting helpers:

public async Task OnAsync(Event<LogEventData> evt)
{
    var body = string.IsNullOrWhiteSpace(BodyTemplate) ?
        FormatDefaultBody(evt) :
        FormatTemplate(BodyTemplate, evt);
 
    var subject = FormatTemplate(SubjectTemplate, evt);
 
    var client = new SmtpClient(Host, Port ?? 25);
    if (!string.IsNullOrWhiteSpace(Username))
        client.Credentials = new NetworkCredential(Username, Password);
 
    await client.SendAsync(From, To, subject, body);
}

Debugging

The Seq.Apps.Testing package provides a convenient local F5 experience for testing and debugging Seq apps.

Packaging

To package an app without third-party libraries, a simple nuget pack on the .csproj file is adequate.

If your app uses other libraries, these must be included as files in the package rather than specified as dependencies, because at run-time Seq will not load files from other packages.

The [Seq.App.HttpRequest project]

includes a modified CSPROJ file and build script that demonstrate how to create an app package with all required files included.

🚧

Don't package the Seq.Apps.dll or Serilog.dll assemblies into your app. They're loaded automatically by the Seq server.