Setup - gonace/Cronitor GitHub Wiki

Install the package and give the library your API key.

Install

dotnet add package Cronitor

Or download it from NuGet. The package targets .NET 8, 9 and 10, and .NET Standard 2.0.

Your API key is on your account settings page.

Choosing a setup

The library has four clients, one for each part of the Cronitor API:

Client Interface Used for
TelemetriesClient ITelemetriesClient Telemetry
MonitorsClient IMonitorsClient Monitors and Activity
IssuesClient IIssuesClient Issues
NotificationsClient INotificationsClient Notifications

There are three ways to get them:

Setup How Gives you
Static class CronitorClient.Configure("apiKey") or .UseCronitor() CronitorClient.Telemetries, .Monitors, .Issues, .Notifications, and the telemetry methods directly on CronitorClient
Dependency injection .ConfigureCronitor() The four interfaces, injected into your classes
Creating clients yourself new TelemetriesClient("apiKey") One client, wherever you create it

The static class and dependency injection are independent: .ConfigureCronitor() doesn't configure the static class, and .UseCronitor() doesn't register anything for injection. Call both if you want both.

Static class

Configure the static CronitorClient once, at startup, then use it anywhere in your application:

CronitorClient.Configure("apiKey");

await CronitorClient.TickAsync("monitorKey");
var monitor = await CronitorClient.Monitors.GetAsync("monitorKey");

CronitorClient used to be called Cronitor. That name still works but is obsolete: it shares its name with the namespace Cronitor, so it can only be called as Cronitor.Cronitor.

Using the static class before it's configured throws NotConfiguredException. Calling Configure again replaces the configuration.

To log telemetry events that can't be delivered, pass an ILoggerFactory, see Errors and logging:

CronitorClient.Configure("apiKey", loggerFactory);

Static class with the Generic Host

With the .NET Generic Host (Microsoft.Extensions.Hosting), .UseCronitor() configures the static class and connects it to the host's logging. It's in the namespace Cronitor.Extensions.

using Cronitor.Extensions;

await Host.CreateDefaultBuilder()
    .UseCronitor("apiKey")
    .Build()
    .RunAsync();

To read the API key from configuration, pass a function instead:

using Cronitor.Extensions;

await Host.CreateDefaultBuilder()
    .UseCronitor((context) => context.Configuration.GetValue<string>("Cronitor:ApiKey"))
    .Build()
    .RunAsync();
{
  "Cronitor": {
    "ApiKey": "apiKey"
  }
}

An empty API key throws ArgumentNullException: with a string, when .UseCronitor() is called, with a function, when the host is built.

In ASP.NET Core, call it on builder.Host:

using Cronitor.Extensions;

var builder = WebApplication.CreateBuilder(args);
builder.Host.UseCronitor((context) => context.Configuration.GetValue<string>("Cronitor:ApiKey"));

Dependency injection

.ConfigureCronitor() registers ITelemetriesClient, IMonitorsClient, IIssuesClient and INotificationsClient in the service container, as transient services. The ITelemetriesClient uses the host's logging. Like .UseCronitor(), it takes the API key as a string or as a function:

using Cronitor.Extensions;

await Host.CreateDefaultBuilder()
    .ConfigureCronitor((context) => context.Configuration.GetValue<string>("Cronitor:ApiKey"))
    .Build()
    .RunAsync();

Then inject the interfaces you need:

public class SomeJob
{
    private readonly ITelemetriesClient _telemetriesClient;
    private readonly IMonitorsClient _monitorsClient;

    public SomeJob(ITelemetriesClient telemetriesClient, IMonitorsClient monitorsClient)
    {
        _telemetriesClient = telemetriesClient;
        _monitorsClient = monitorsClient;
    }
}

In ASP.NET Core, call it on builder.Host:

using Cronitor.Extensions;

var builder = WebApplication.CreateBuilder(args);
builder.Host.ConfigureCronitor((context) => context.Configuration.GetValue<string>("Cronitor:ApiKey"));

.UseCronitor() and .ConfigureCronitor() extend IHostBuilder. Host.CreateApplicationBuilder() returns a HostApplicationBuilder, which isn't one; there, register the clients yourself:

var builder = Host.CreateApplicationBuilder(args);
var apiKey = builder.Configuration.GetValue<string>("Cronitor:ApiKey");

builder.Services.AddTransient<ITelemetriesClient>(provider =>
    new TelemetriesClient(apiKey, provider.GetRequiredService<ILoggerFactory>()));
builder.Services.AddTransient<IMonitorsClient>(_ => new MonitorsClient(apiKey));

Creating clients yourself

Every client can be created with an API key, without a host or the static class:

var telemetriesClient = new TelemetriesClient("apiKey");
var monitorsClient = new MonitorsClient("apiKey");
var issuesClient = new IssuesClient("apiKey");
var notificationsClient = new NotificationsClient("apiKey");

TelemetriesClient also takes an ILoggerFactory, see Errors and logging:

var telemetriesClient = new TelemetriesClient("apiKey", loggerFactory);

The clients are IDisposable. They share their network connections, so creating one per job is fine; dispose it when you're done, or keep one for the lifetime of the application.

⚠️ **GitHub.com Fallback** ⚠️