繁体   English   中英

是否可以在没有模板消息的情况下使用NLog的结构化日志记录?

[英]Is it possible to use NLog's structured logging without having templated messages?

直到今天,我们仍使用NLog 4.4.12版(不使用结构化日志记录)。 但是,我们使用https://www.nuget.org/packages/NLog.StructuredLogging.Json/进行结构化日志记录。

使用此扩展的好处是您不需要模板消息(包含索引或占位符以记录其他参数/​​对象)。 该消息不包含要记录的其他对象(即匿名类型)的任何索引或占位符。

切换到支持结构化登录的NLog 4.6.5时,我们希望摆脱该附加的NuGet包。 但是,仅当使用带有实际索引/命名占位符的模板消息时,才会记录我们的附加参数。

我们的消息中没有索引或占位符会导致没有其他参数/​​对象通过JSON呈现出来。

是否有可能使用非模板消息,但是仍使用NLog的结构化日志记录我们传递给他们的其他参数,以便将它们添加到JSON中?

下面是一个示例(请注意,我们在nlog周围使用了额外的包装器)

NLog版本 :4.6.5

平台 :.Net 4.5

当前的NLog配置

// Arrange
var typeUsingLogger = typeof(NLogWrapperTest);
var nLogWrapper = new NLogWrapper(typeof(NLogWrapper));

var level = (LogLevel)Enum.Parse(typeof(LogLevel), nLevel.Name);
var message = $"{Guid.NewGuid()}"; // {{extendedLogProperties}}  {{@extendedLogProperties}} {{@purchase}} {{badplaceholder}}
var innerException = new DivideByZeroException("bla inner exception");
var exception = new ArgumentNullException("bla out exception", innerException);
var extendedLogProperties = new
{
    ClientID = 8,
    MyOtherProp = "abc",
    MySubObject = new
    {
        //nested object although not recommended
        A = 123,
        B = "yep"
    }
};

//log configuration
var logConfig = new LoggingConfiguration();

var memoryTarget = new MemoryTarget("MemoryTarget");
var jsonLayout = new JsonLayout
{
    IncludeAllProperties = true,
    Attributes =
    {
            new JsonAttribute("dateTime", "${date:universalTime=true:format=o}" ),
            new JsonAttribute("level", "${level:uppercase=true}" ),
            new JsonAttribute("logger", "${logger}" ),
            new JsonAttribute("message", "${message}" ),
            new JsonAttribute("callsite", "${callsite:className=true:methodName=true:skipFrame=0}" ),
            new JsonAttribute("exception", "${exception:format=ToString:innerFormat=ToString}" ),
            new JsonAttribute("machinename", "${machinename}" ),
            new JsonAttribute("processid", "${processid}" ),
            new JsonAttribute("threadid", "${threadid}" ),
            new JsonAttribute("threadname", "${threadname}" ),
            new JsonAttribute("application", "${application}" ),
            new JsonAttribute("aspnetSessionId", "${aspnet-sessionid}" ),
            new JsonAttribute("iisSiteName", "${iis-site-name}" ),
            new JsonAttribute("stage", "${stage}" ),
    }
};
memoryTarget.Layout = jsonLayout;
logConfig.AddTarget("memoryTarget", memoryTarget);
var memoryTargetLoggingRule = new LoggingRule("*", nLevel, memoryTarget);
logConfig.LoggingRules.Add(memoryTargetLoggingRule);

LogManager.Configuration = logConfig;

// Act
nLogWrapper.Log(level, message, typeUsingLogger, exception, extendedLogProperties);

var jsonLogMsg = memoryTarget.Logs[0];
Assert.Matches("ClientID", jsonLogMsg);

我们为什么需要它?

  • 如果没有任何替换的索引或占位符,则使消息不变就好了,这样我们就可以在日志中搜索完全相同的消息。 (不能使用new JsonAttribute("message", "${message:raw=true}"

  • 同样,通过这种方式,我们也不会最终在日志消息(替换模板消息的占位符/索引)和这些附加参数的附加JSON字段中使JSON序列化对象一次。

请查看其最佳做法: https : //github.com/justeat/NLog.StructuredLogging.Json/blob/master/README.md#best-practices

如果您问:“为什么不继续使用NuGet NLog扩展?” 答案是,当在嵌套对象的模板消息中使用{@placeholder}时,NLog的结构化日志会更好地呈现其他参数。

编辑1:我想将匿名对象的所有属性呈现在json的根目录中。 如:

{
    ...
    "ClientID": 8,
    "MyOtherProp": "abc",
    "MySubObject": {              
                    "A": 123,
                    "B": "yep"
                },
    ...
}

我认为您正在寻找logger.WithProperty

例:

var extendedLogProperties = new
{
    ClientID = 8,
    MyOtherProp = "abc",
    MySubObject = new
    {
        //nested object although not recommended
        A = 123,
        B = "yep"
    }
};

logger.WithProperty("extendedLogProperties", extendedLogProperties).Info("test message");

您可以将其序列化为JSON,XML等。

示例,带有所有属性的JSON

将所有事件属性呈现为JSON

配置:

 <target xsi:type="File" name="jsonFile" fileName="c:\temp\nlog-json-nested-${shortdate}.log">
     <layout type="JsonLayout">
         <attribute name="time" layout="${longdate}" />
         <attribute name="level" layout="${level}" />
         <attribute name="message" layout="${message}" />
         <attribute name="eventProperties" encode="false" >
             <layout type='JsonLayout' includeAllProperties="true"  maxRecursionLimit="2"/>
         </attribute>
     </layout>
 </target>

重要的是includeAllProperties="true"maxRecursionLimit="2" (默认为0 )。 请参阅Json Layout文档

这应该呈现(演示的格式很好,但是将是一行):

{
    "time": "2019-06-18 11:09:00.2349",
    "level": "Info",
    "message": "test message",
    "eventProperties": {
        "extendedLogProperties": {
            "ClientID": 8,
            "MyOtherProp": "abc",
            "MySubObject": {
                "A": 123,
                "B": "yep"
            }
        }
    }
}

笔记

因此要明确:

JSON将写在一行上,因此没有换行符。

在我的包装中,我设法实现了这样的目标:

        private void Log(
            NLog.LogLevel nlogLogLevel,
            Logger nlogLogger,
            Type typeUsingLogger,
            string message,
            Exception exception,
            IDictionary<string, object> additionalProperties = null)
        {
            var logEventInfo = new LogEventInfo(nlogLogLevel, typeUsingLogger.ToString(), null, message, new object[] { }, exception);
            if (additionalProperties != null)
            {
                foreach (var x in additionalProperties)
                {
                    if (!logEventInfo.Properties.ContainsKey(x.Key))
                    {
                        logEventInfo.Properties.Add(x.Key, x.Value);
                    }
                }
            }

            nlogLogger.Log(_logWrapperType, logEventInfo);
        }

并将includeAllProperties设置为true ,并将maxRecursionLimit设置更高(2)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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