Docs menu · Customize Entries
DocsLlamaLogger.Core SDK

Customize Entries

LlamaLogger.Core 26.1.2

Before entries are sent, you can customize them using the CustomizeLogEntry event. This is an opportunity to modify information, add custom data or attachments, or cancel the entry.

The event runs for every entry, including text messages (where e.LogEntry.Exception is null), synchronously on the thread that logged it. Keep the handler fast.

Ignore all exceptions of a particular type

In this example, we will ignore all ValidationExceptions:

using System.ComponentModel.DataAnnotations;

LlamaLoggerClient client = new LlamaLoggerClient(apikey, version);
client.AddIgnoredExceptionType<ValidationException>();

Only that exact type is ignored; exceptions derived from it are still logged. Use RemoveIgnoredExceptionType<T>() to start logging a type again.

Ignore exceptions per-occurrence

In this example, we will ignore an InvalidOperationException where the message contains the word "test":

LlamaLoggerClient client = new LlamaLoggerClient(apikey, version);
client.CustomizeLogEntry += Client_CustomizeLogEntry;

void Client_CustomizeLogEntry(object? sender, LlamaLogger.Core.CustomEventArgs.CustomizeLogEntryEventArgs e)
{
    if (e.LogEntry.Exception is InvalidOperationException ex && ex.Message.Contains("test"))
        e.Cancel = true; //The entry is discarded and not sent anywhere
}

Setting e.Cancel = true discards the entry for every sink, including custom sinks.

Modify Information

In this example, we are removing the occurrence of a particular password from text messages and exception messages:

LlamaLoggerClient client = new LlamaLoggerClient(apikey, version);
client.CustomizeLogEntry += Client_CustomizeLogEntry;

void Client_CustomizeLogEntry(object? sender, LlamaLogger.Core.CustomEventArgs.CustomizeLogEntryEventArgs e)
{
    if (e.LogEntry.LogText != null)
        e.LogEntry.LogText = e.LogEntry.LogText.Replace("Pa$$word", "***REMOVED***");

    if (e.LogEntry.Exception != null && e.LogEntry.Exception.Message.Contains("Pa$$word"))
        e.LogEntry.Exception = new Exception(e.LogEntry.Exception.Message.Replace("Pa$$word", "***REMOVED***"));
}

The exception is converted to text after this event runs, so change e.LogEntry.Exception (not ExceptionString). Replacing the exception this way removes its stack trace and inner exceptions, so only do it when the message contains sensitive data.