datalust / seq-extensions-logging

Add centralized log collection to ASP.NET Core apps with one line of code.
https://datalust.co/seq
Apache License 2.0
84 stars 12 forks source link

JsonSafeString isn't working as expected #53

Closed radiohe4d closed 1 year ago

radiohe4d commented 1 year ago

So I've been using Seq successfully now for months and have only now needed to pass a raw JSON string to the logs.

When passing an object and using the @, the logs in the Seq dashboard have shown the JSON formatted and highlighting like so:

I followed this ticket #51 to add this Nuget package and pass the raw JSON using the new JsonSafeString(json) struct.

I used the exact code from the ticket, but it doesnt appear to be formatting the log within the Seq dashboard as I expect.

var json = "{\"A\": 42}";
_logger.LogInformation("The answer is {Answer}", new JsonSafeString(json));

Any help would be much appreciated as I cannot figure out why it wont format like the example in ticket 51.

Current Setup:

Seq Server Instance:

Language

C# .NET 6.0

Nuget Packages:

Program.cs

builder
    .Host
    .ConfigureLogging(lc => lc.ClearProviders())
    .UseSerilog((ctx, lc) => lc
        .ReadFrom.Configuration(ctx.Configuration)
        .Enrich.WithCorrelationId()
        .Enrich.FromLogContext()
        .Enrich.WithExceptionDetails(
            new DestructuringOptionsBuilder()
                .WithDefaultDestructurers()
                .WithDestructurers(new[] { new DbUpdateExceptionDestructurer() }))
    );

appsettings.json

"Serilog": {
    "MinimumLevel": {
        "Default": "Debug",
        "Override": {
            "Microsoft": "Warning",
            "Microsoft.Hosting.Lifetime": "Information"
        }
    },
    "Using": [
        "Serilog.Sinks.Console",
        "Serilog.Sinks.Seq"
    ],
    "WriteTo": [
        { "Name": "Console" },
        {
            "Name": "Seq",
            "Args": {
                "serverUrl": "<OMITTED>",
                "apiKey": "<OMITTED>"
            }
        }
    ],
    "Properties": {
        "_Application": "<OMITTED>"
    }
},
nblumhardt commented 1 year ago

@radiohe4d thanks for the note!

You're using Serilog to log to Seq, but JsonSafeString is part of the alternative Seq.Extensions.Logging package, which is a different pipeline altogether.

Serilog doesn't have a similar notion, in part because it doesn't assume that the back-end output format is JSON (or even anything textual).

Seq.Extensions.Logging can do it because at this point and the foreseeable future, it's specifically targeting Seq's HTTP ingestion API.

You might consider switching from Serilog across to S.E.L, but unless you need to do this a lot, you're better off just logging the JSON string, and using FromJson on the Seq side, e.g.:

FromJson(Answer).A > 40

Alternatively, you could implement an enricher in the Serilog pipeline to parse and transform marked JSON values (using a type like JsonSafeString) into LogEventPropertyValue, though it's likely to be a bit tedious.

Let me know if this helps, Nick

radiohe4d commented 1 year ago

Thanks for getting back so quick Nick!

That's where I've made the mistake, I thought S.E.L was a drop in extension, but now that you mention it, it does make more sense that its a replacement for Serilog.

You've given a few great options for next steps, so ill go have a dig around and see what works best.

If I come up with anything useful that could help others I'll drop it back here, but happy for this ticket to be closed for now.

Thanks again 👍

nblumhardt commented 1 year ago

Thanks @radiohe4d 👍