NLog 布局
编辑NLog 布局编辑
此布局实现将 NLog 事件格式化为符合 Elastic Common Schema 规范的 JSON 表示。
安装编辑
添加对 Elastic.CommonSchema.NLog 包的引用
<PackageReference Include="Elastic.CommonSchema.NLog" Version="8.6.0" />
用法编辑
以编程方式进行设置编辑
Layout.Register<EcsLayout>("EcsLayout"); // Register the ECS layout. var config = new LoggingConfiguration(); var consoleTarget = new ConsoleTarget("console") { Layout = new EcsLayout() }; // Use the ECS layout. config.AddRule(LogLevel.Debug, LogLevel.Fatal, consoleTarget); LogManager.Configuration = config; var logger = LogManager.GetCurrentClassLogger();
在以上代码片段中 Layout.Register<EcsLayout>("EcsLayout")
用 EcsLayout
向 NLog 注册。 Layout = new EcsLayout()
行然后指导 NLog 使用已注册的布局。以上示例使用控制台目标,但是你可以自由使用任何你选择的 target;也许考虑使用文件系统 target 和 Elastic Filebeat 以获得持久可靠的摄入。
使用 NLog.config 进行设置编辑
<nlog> <extensions> <add assembly="Elastic.Apm.NLog"/> <add assembly="Elastic.CommonSchema.NLog"/> </extensions> <targets> <target name="console" type="console"> <layout xsi:type="EcsLayout"> <metadata name="MyProperty" layout="MyPropertyValue" /> <!-- repeated, optional --> <label name="MyLabel" layout="MyLabelValue" /> <!-- repeated, optional --> <tag layout="MyTagValue" /> <!-- repeated, optional --> </layout> </target> </targets> <rules> <logger name="*" minLevel="Debug" writeTo="Console" /> </rules> </nlog>
EcsLayout 参数选项编辑
- 元数据选项
-
IncludeEventProperties - 将 LogEvent 属性作为元数据包含。默认值:
true
-
IncludeScopeProperties - 将 NLog 作用域上下文属性作为元数据包含。默认值:
false
- ExcludeProperties - 字符串使用逗号分隔,用于指定要排除的属性名称。
- 事件选项
- EventAction -
- EventCategory -
- EventId -
- EventKind -
- EventSeverity -
- 代理选项
- AgentId -
- AgentName -
- AgentType -
- AgentVersion -
- 进程选项
-
ProcessExecutable - 默认值:
${processname:FullName=true}
-
ProcessId - 默认值:
${processid}
-
ProcessName - 默认值:
${processname:FullName=false}
-
ProcessThreadId - 默认值:
${threadid}
-
ProcessTitle - 默认值:
${processinfo:MainWindowTitle}
- 服务器选项
- ServerAddress -
- ServerIp -
-
ServerUser - 默认值:
${environment-user}
- 主机选项
- HostId -
-
HostIp - 默认值:
${local-ip:cachedSeconds=60}
-
HostName - 默认值:
${machinename}
- 日志来源选项
-
LogOriginCallSiteMethod - 默认值:
${exception:format=method}
-
LogOriginCallSiteFile - 默认值:
${exception:format=source}
- LogOriginCallSiteLine -
- HTTP 选项
-
HttpRequestId - 默认值:
${aspnet-trace-identifier}
-
HttpRequestMethod - 默认值:
${aspnet-request-method}
-
HttpRequestBytes - 默认值:
${aspnet-request-contentlength}
-
HttpRequestReferrer - 默认值:
${aspnet-request-referrer}
-
HttpResponseStatusCode - 默认值:
${aspnet-response-statuscode}
- URL 选项
-
UrlScheme - 默认值:
${aspnet-request-url:IncludeScheme=true:IncludeHost=false:IncludePath=false}
-
UrlDomain - 默认值:
${aspnet-request-url:IncludeScheme=false:IncludeHost=true:IncludePath=false}
-
UrlPath - 默认值:
${aspnet-request-url:IncludeScheme=false:IncludeHost=false:IncludePath=true}
-
UrlPort - 默认值:
${aspnet-request-url:IncludeScheme=false:IncludeHost=false:IncludePath=false:IncludePort=true}
-
UrlQuery - 默认值:
${aspnet-request-url:IncludeScheme=false:IncludeHost=false:IncludePath=false:IncludeQueryString=true}
-
UrlUserName - 默认值:
${aspnet-user-identity}
- 跟踪选项
-
ApmTraceId - 默认值:
${ElasticApmTraceId}
- 事务选项
-
ApmTransactionId - 默认值:
${ElasticApmTransactionId}
*
ECS 感知消息模板编辑
此外,在 LogTemplateProperties.*
下可用的任何有效的 ECS 日志模板属性(例如 LogTemplateProperties.TraceId
)都受支持,并将直接设置相应的 ECS 字段。
logger.Info("The time is {TraceId}", "my-trace-id");
将覆盖生成 ECS json 文档上的 trace.id
。
EcsLayout 生成的示例输出编辑
下面给出了一个输出示例
{ "@timestamp":"2020-02-20T16:07:06.7109766+11:00", "log.level":"Info", "message":"Info \"X\" 2.2", "metadata":{ "value_x":"X", "some_y":2.2 }, "ecs":{ "version":"1.4.0" }, "event":{ "severity":6, "timezone":"AUS Eastern Standard Time", "created":"2020-02-20T16:07:06.7109766+11:00" }, "host":{ "name":"LAPTOP" }, "log":{ "logger":"Elastic.CommonSchema.NLog", "original":"Info {ValueX} {SomeY}" }, "process":{ "thread":{ "id":17592 }, "pid":17592, "name":"dotnet", "executable":"C:\\Program Files\\dotnet\\dotnet.exe" } }