简体   繁体   中英

How to disable escaping forward-slash symbol in nested JsonLayout in NLog

I need to log a http request which is sending from .Net Core app to external service. It works fine, but if json properties contains a / character, NLog output it as \\/ I suppose that the problem is encoding and I check this questions:

NLog: logging an object serialized to JSON

How to omit escaping "/" in Nlog.Web.AspNetCore

But those answers fix this behavior for particular JsonAttribute. How turn off Encode flag for all nested JsonLayout?

Code sample:

static void Main(string[] args)
{
    var loggingConfig = new LoggingConfiguration();
    loggingConfig.AddRule(LogLevel.Trace, LogLevel.Fatal, new ConsoleTarget
        {
            Layout = CreateJsonLayout(),
            Encoding = Encoding.UTF8,
        });
    LogManager.Configuration = loggingConfig;

    _logger = LogManager.GetLogger("*");

    var requestMessage = new HttpRequestMessage(HttpMethod.Post, new Uri("http://localhost:8080/some/path"));
    var content = new SomeClass
    {
        customerInfo = "customers/some_customers",
        boolProp = true,
        extras = "data/extras"
    };

    var eventProperties = new
    {
        Method = requestMessage.Method,
        RequestUri = requestMessage.RequestUri,
        Content = content
    };

    _logger.Log(LogLevel.Info)
        .Properties(eventProperties.AsDictionary())
        .Message("some message")
        .Write();
}

private static JsonLayout CreateJsonLayout() => new JsonLayout
{
    Attributes =
    {
        new JsonAttribute("level", "${level:upperCase=true}") { Encode = false, EscapeUnicode = false },
        new JsonAttribute("message", "${replace:searchFor=\"+:replaceWith=':regex=true:inner=${message}}") { Encode = false, EscapeUnicode = false },
        new JsonAttribute("eventProperties", 
            new JsonLayout
            {
                IncludeAllProperties = true,
                RenderEmptyObject = false,
                MaxRecursionLimit = 5,
                ExcludeProperties = new HashSet<string>(new[] { "CallerLineNumber", "CallerFilePath", "CallerMemberName" }),

            }, false) { EscapeUnicode = false }
    }
};
public class SomeClass
{
    public bool boolProp { get; set; }

    public string customerInfo { get; set; }

    public string extras { get; set; }
}
internal static class ObjectExtensions
{
    public static IDictionary AsDictionary(this object source)
    {
        return source.GetType().GetProperties().ToDictionary
        (
            propInfo => propInfo.Name,
            propInfo => propInfo.GetValue(source)
        );
    }
}

NLog 4.6.8 has been released and allows you to configure EscapeForwardSlash

new JsonAttribute("eventProperties", 
    new JsonLayout
    {
        IncludeAllProperties = true,
        RenderEmptyObject = false,
        MaxRecursionLimit = 5,
        EscapeUnicode = false,
        EscapeForwardSlash = false,
    }, false)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM