ZLogger icon indicating copy to clipboard operation
ZLogger copied to clipboard

How to use customize structured logging

Open richard0326 opened this issue 2 years ago • 0 comments

Hello I would like to customize structedLogging Is it possible??

Or can I make custom log processor?? your example doesn't work MessageTest.cs It output nothing...

And I would like to use Payload in custom log processor.

this code is using Prefix, Suffix

builder.AddZLoggerConsole(options =>
{
    options.PrefixFormatter = (writer, info) =>
    {
        var strList = new List<string>()
        {
            $"\"CategoryName\":\"{info.CategoryName}\"",
            $"\"LogLevel\":\"{info.LogLevel}\"",
            $"\"Timestamp\":\"{info.Timestamp.DateTime.ToLocalTime()}\"",
            $"\"Exception\":\"{info.Exception}\"",
        };

        var outStr = String.Join(", ", strList);
        
        ZString.Utf8Format(writer, "{0}, \"Message\":\"", "{" + outStr);
    };
}

this code is using structedLogging I would like to erase EventID or some values I don't use.

builder.AddZLoggerConsole(options =>
{
    options.PrefixFormatter = (writer, info) =>
    {
        options.EnableStructuredLogging = true;

        options.StructuredLoggingFormatter = (writer, info) =>
        {
            //writer.WriteString(applicationKey, applicationValue);
            info.WriteToJsonWriter(writer);
        };

        options.JsonSerializerOptions = new JsonSerializerOptions
        {
            WriteIndented = false,
            IgnoreNullValues = true,
            Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
        };
    };
}

richard0326 avatar Sep 05 '22 07:09 richard0326