Docs menu · Custom Sinks
DocsLlamaLogger.Core SDK

Custom Sinks

LlamaLogger.Core 26.1.2

Sometimes you need to log entries to more than one place, such as the console, a local file, or another logging system. You can do this with a class that implements the ISink interface. The built-in Llama Logger sink is always added to the client first; each sink you add then receives every entry that meets its minimum logging level.

In the sample below, we provide a simple ISink implementation that logs to the console.

using LlamaLogger.Core;
using LlamaLogger.Core.Interfaces;

public class ConsoleSink : ISink
{
    public bool EnableLogging { get; set; } = true; //Sinks only receive entries while this is true
    public LoggingLevel MinimumLoggingLevel { get; set; } = LoggingLevel.Error;

    public bool IsBusy => false;

    public void Log(LlamaLogEntry entry)
    {
        if (!string.IsNullOrEmpty(entry.LogText))
            Console.WriteLine(entry.LogText);

        if (entry.Exception != null)
            Console.WriteLine(entry.Exception.Message);
    }
}

Register the sink when you create the client:

LlamaLoggerClient client = new LlamaLoggerClient(apikey, version);
client.AddSink(new ConsoleSink());

Things to know when writing a sink

  • Log is called synchronously on the thread that logged the entry. If your sink does slow work, such as file or network I/O, queue it inside the sink and return quickly.
  • Return true from IsBusy while your sink still has queued work. Flushing the Queue waits for every sink, so return false once you're done.
  • Exceptions thrown by your sink are caught so they can't crash your application or stop other sinks.
  • entry.ExceptionString contains the full exception details as JSON, if you need more than entry.Exception.
  • Calling client.SetLoggingLevel(...) or client.EnableLogging(...) changes MinimumLoggingLevel and EnableLogging on every sink, including yours.
  • Remove a sink with client.RemoveSink(sink).