How to search custom data

Custom data is not searchable by default. Sometimes it makes sense that errors can be searched from values logged as part of custom data. For now, this feature is supported through the use of variable naming, but we may extend this to a configuration option through the API or UI as well.

To make a custom variable and its value searchable through the UI (as well as through the API), name the variable with the prefix X-ELMAHIO-SEARCH-. The variable will become searchable through the name added after the prefix.

Examples:

Elmah.ErrorLog.GetDefault(null);
var logger = Elmah.Io.ErrorLog.Client;
logger.OnMessage += (sender, args) =>
{
    if (args.Message.Data == null) args.Message.Data = new List<Item>();
    args.Message.Data.Add(new Item { Key = "X-ELMAHIO-SEARCH-author", Value = "Walter Sobchak" });
};

builder.Services.AddElmahIo(o =>
{
    o.OnMessage = message =>
    {
        if (message.Data == null) message.Data = new List<Item>();
        message.Data.Add(new Item { Key = "X-ELMAHIO-SEARCH-author", Value = "Walter Sobchak" });
    };
});

using (LogContext.PushProperty("X-ELMAHIO-SEARCH-author", "Walter Sobchak"))
{
    logger.Error("You see what happens, Larry?");
}

var errorMessage = new LogEventInfo(LogLevel.Error, "", "You see what happens, Larry?");
errorMessage.Properties.Add("X-ELMAHIO-SEARCH-author", "Walter Sobchak");
log.Error(errorMessage);

var properties = new PropertiesDictionary();
properties["X-ELMAHIO-SEARCH-author"] = "Walter Sobchak";
log.Logger.Log(new LoggingEvent(new LoggingEventData
{
    Level = Level.Error,
    TimeStampUtc = DateTime.UtcNow,
    Properties = properties,
    Message = "You see what happens, Larry?",
}));

var scope = new Dictionary<string, object> { { "X-ELMAHIO-SEARCH-author", "Walter Sobchak" } };
using (logger.BeginScope(scope}))
{
    logger.LogError("You see what happens, Larry?");
}

The examples will make author searchable using this query:

data.author:"Walter Sobchak"

Observe how X-ELMAHIO-SEARCH- is replaced with the data. prefix when indexed in elmah.io.

Adding searchable properties is available when logging exceptions too:

try
{
    // ...
}
catch (NullReferenceException e)
{
    e.Data.Add("X-ELMAHIO-SEARCH-author", "Walter Sobchak");
    // Log the exception or throw e to use this catch for decorating the exception
}

To avoid someone filling up our cluster with custom data, only the first three variables containing X-ELMAHIO-SEARCH- are made searchable. Also, variables with a value containing more than 256 characters are not indexed.


This article was brought to you by the elmah.io team. elmah.io is the best error management system for .NET web applications. We monitor your website, alert you when errors start happening, and help you fix errors fast.

See how we can help you monitor your website for crashes Monitor your website