Docs menu · Custom Sinks
DocsLlamaLogger.Core SDK
Custom Sinks
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
Logis 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
truefromIsBusywhile your sink still has queued work. Flushing the Queue waits for every sink, so returnfalseonce you're done. - Exceptions thrown by your sink are caught so they can't crash your application or stop other sinks.
entry.ExceptionStringcontains the full exception details as JSON, if you need more thanentry.Exception.- Calling
client.SetLoggingLevel(...)orclient.EnableLogging(...)changesMinimumLoggingLevelandEnableLoggingon every sink, including yours. - Remove a sink with
client.RemoveSink(sink).