Skip to content

Add Serilog log's properties as Breadcrumb data #4152

@ZivFarin

Description

@ZivFarin

This line shows that a serilog log's properties are set as 'extra' for the event, but once I log an event and add properties, it is nowhere to be found.
This does not fit so much with the behavior in other sinks I have seen so far.

Is there a reason for this?
Perhaps a function in the style of ExtractBreadCrumbDataFromLogEvent below would be better?

class SentrySink : ILogEventSink
{
    public void Emit(LogEvent logEvent)
    {
        Dictionary<string, string> breadcrumbData = ExtractBreadCrumbDataFromLogEvent(logEvent);


        SentrySdk.AddBreadcrumb(
            message: logEvent.RenderMessage(),
            category: "Log",
            type: "Default",
            data: breadcrumbData,
            level: MapLevel(logEvent.Level)
        );
    }

    private static Dictionary<string, string> ExtractBreadCrumbDataFromLogEvent(LogEvent logEvent)
    {
        var breadcrumbData = new Dictionary<string, string>();


        if (logEvent.Exception is not null)
            breadcrumbData["Exception"] = logEvent.Exception.ToString().Trim('"');

        foreach (var prop in logEvent.Properties)
            breadcrumbData[prop.Key] = prop.Value.ToString().Trim('"');


        return breadcrumbData;
    }

    private static BreadcrumbLevel MapLevel(LogEventLevel level) =>
        level switch
        {
            LogEventLevel.Verbose => BreadcrumbLevel.Debug,
            LogEventLevel.Debug => BreadcrumbLevel.Debug,
            LogEventLevel.Information => BreadcrumbLevel.Info,
            LogEventLevel.Warning => BreadcrumbLevel.Warning,
            LogEventLevel.Error => BreadcrumbLevel.Error,
            LogEventLevel.Fatal => BreadcrumbLevel.Critical,
            _ => BreadcrumbLevel.Info
        };
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status

    No status

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions