Cysharp / ZLogger

Zero Allocation Text/Structured Logger for .NET with StringInterpolation and Source Generator, built on top of a Microsoft.Extensions.Logging.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to use customize structured logging

richard0326 opened this issue · 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)
        };
    };
}

This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 7 days.