CoAkka Logger · 1.2.3 · NuGet

Use CoAkka Logger with C# / .NET

Install the package, read a record and check the result before connecting Logger to an application.

Beginner

Install the package

Use the .NET SDK and runtime for a net8.0 console project. Add the exact NuGet package in a new directory.

Terminal
dotnet --version
dotnet new console --framework net8.0
dotnet add package CoAkka.Logger --version 1.2.3

Beginner

Read the first record

Save as Program.cs.

Program.cs
using CoAkka.Logger;

using var logger = LoggerHost.Start(new LoggerStartSpec("csharp-logger-basic", QueueCapacity: 8));
var info = LoggerHost.ReadInfo();

var category = "samples.logger.csharp.basic";
var message = "{\"event\":\"hello\",\"language\":\"csharp\"}";
if (!logger.TryInfo(category, message, out var sequence))
{
    throw new InvalidOperationException("expected INFO log to be accepted");
}

var record = logger.AwaitNext(1000) ?? throw new InvalidOperationException("expected one drained logger record");
if (record.Sequence != sequence || record.Category != category || record.Message != message)
{
    throw new InvalidOperationException($"unexpected record: {record}");
}

var stats = logger.Stats();
Console.WriteLine($"coakka_logger_info abi={info.AbiVersion} version={info.RuntimeVersion} git={info.GitCommit}");
Console.WriteLine($"coakka_logger_record sequence={record.Sequence} level=info category={record.Category} message={record.Message}");
Console.WriteLine($"coakka_logger_stats emitted={stats.EmittedCount} delivered={stats.DeliveredCount} dropped={stats.DroppedCount}");
Terminal
dotnet run
SystemName
Names the Logger instance.
MinLevel
Sets which levels can be admitted.
AwaitNext(1000)
Bounds how long the reader waits. Match the returned sequence to the accepted write.
coakka_logger_record sequence=1 level=info ...
coakka_logger_stats emitted=1 delivered=1 dropped=0

Advanced

Handle queue pressure

Replace the first example in the same project before running this separate exercise.

Create a separate program for this episode. Set the queue to 2, submit 8 enabled records and start reading only after the submissions. Read the queue-full result before counting a refusal.

Save as Program.cs.

Program.cs
using CoAkka.Logger;

using var logger = LoggerHost.Start(new LoggerStartSpec("csharp-logger-pressure", QueueCapacity: 2));
var attempts = 8;
var accepted = 0;
var rejected = 0;

for (var i = 0; i < attempts; i++)
{
    if (logger.TryInfo("samples.logger.csharp.pressure", $"{{\"event\":\"pressure\",\"i\":{i}}}", out _))
    {
        accepted++;
    }
    else
    {
        rejected++;
    }
}

for (var i = 0; i < accepted; i++)
{
    _ = logger.AwaitNext(1000) ?? throw new InvalidOperationException("expected accepted record");
}

var stats = logger.Stats();
if (accepted != 2 || rejected != 6 || stats.DroppedCount != 6)
{
    throw new InvalidOperationException($"unexpected pressure result accepted={accepted} rejected={rejected} dropped={stats.DroppedCount}");
}

Console.WriteLine($"coakka_logger_pressure attempts={attempts} accepted={accepted} rejected={rejected} capacity={stats.QueueCapacity} highWatermark={stats.QueueHighWatermark} language=csharp");
Console.WriteLine($"coakka_logger_stats emitted={stats.EmittedCount} delivered={stats.DeliveredCount} dropped={stats.DroppedCount} language=csharp");
Terminal
dotnet run

QueueCapacity = 2. TryInfo returns false when the queue is full.

coakka_logger_pressure attempts=8 accepted=2 rejected=6 capacity=2 highWatermark=2
coakka_logger_stats emitted=2 delivered=2 dropped=6

Read counters and plan shutdown · Find an error by symptom