【发布时间】:2020-02-21 08:21:38
【问题描述】:
我有一个 .NET Core 控制台应用程序。
像这样:
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp2.1;netcoreapp3.1</TargetFrameworks>
</PropertyGroup>
我在带有 VS 2019 的 Windows 10 上运行。
我无法让下面的布局渲染器记录任何内容。
https://github.com/NLog/NLog/wiki/Identity-layout-renderer
${identity:authType=Boolean:separator=String:name=Boolean
:isAuthenticated=Boolean}
我的 nlog.config
<?xml version="1.0" encoding="utf-8" ?>
<!-- XSD manual extracted from package NLog.Schema: https://www.nuget.org/packages/NLog.Schema-->
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xsi:schemaLocation="NLog NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogFile="MyApp.NLog.INTERNAL.log"
internalLogLevel="Info" >
<!-- the targets to write to -->
<targets>
<!-- write logs to file -->
<target xsi:type="File" name="target1" fileName="MyApp.NLog.${shortdate}.log"
layout="${date}|${level:uppercase=true}|${logger}|***|${identity:authType=true:separator=/:name=true:isAuthenticated=true}|****|${message} ${exception:format=toString,Data}|${all-event-properties}" />
</targets>
<!-- rules to map from logger name to target -->
<rules>
<logger name="*" minlevel="Trace" writeTo="target1" />
</rules>
</nlog>
示例日志::(
2020/02/19 12:03:40.617|INFO|MyCompany.MyObject|***||****|HeyYouThere |
***||**** 之间没有任何关系:(
和nlog内部日志输出(MyApp.NLog.INTERNAL.log)(没有错误)
2020-02-19 12:03:38.3612 Info Auto loading assembly file: c:\myfolder1\myfolder2\bin\Debug\netcoreapp3.1\NLog.Extensions.Logging.dll
2020-02-19 12:03:38.3734 Info Loading assembly file: c:\myfolder1\myfolder2\bin\Debug\netcoreapp3.1\NLog.Extensions.Logging.dll
2020-02-19 12:03:38.3847 Info NLog.Extensions.Logging, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c. File version: 1.6.1.1203. Product version: 1.6.1.
2020-02-19 12:03:38.3847 Info Auto loading assembly file: c:\myfolder1\myfolder2\bin\Debug\netcoreapp3.1\NLog.Extensions.Logging.dll succeeded!
2020-02-19 12:03:38.3847 Info Auto loading assembly file: c:\myfolder1\myfolder2\bin\Debug\netcoreapp3.1\NLog.WindowsIdentity.dll
2020-02-19 12:03:38.3847 Info Loading assembly file: c:\myfolder1\myfolder2\bin\Debug\netcoreapp3.1\NLog.WindowsIdentity.dll
2020-02-19 12:03:38.3938 Info NLog.WindowsIdentity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c. File version: 4.6.8.10751. Product version: 4.6.8.
2020-02-19 12:03:38.3938 Info Auto loading assembly file: c:\myfolder1\myfolder2\bin\Debug\netcoreapp3.1\NLog.WindowsIdentity.dll succeeded!
2020-02-19 12:03:38.3938 Info Message Template Auto Format enabled
2020-02-19 12:03:38.4652 Info Adding target FileTarget(Name=target1)
2020-02-19 12:03:38.5111 Info Found 39 configuration items
2020-02-19 12:03:38.5614 Info Configuration file change detected! Reloading in 1000ms...
2020-02-19 12:03:38.5614 Info Configuration file change detected! Reloading in 1000ms...
2020-02-19 12:03:39.5688 Info Reloading configuration...
2020-02-19 12:03:39.5688 Info Configuring from an XML element in c:\myfolder1\myfolder2\bin\Debug\netcoreapp3.1\nlog.config...
2020-02-19 12:03:39.5688 Info Message Template Auto Format enabled
2020-02-19 12:03:39.5688 Info Adding target FileTarget(Name=target1)
2020-02-19 12:03:39.5688 Info Closing old configuration.
2020-02-19 12:03:39.5987 Info Found 39 configuration items
Nuget 包
<ItemGroup>
<PackageReference Include="NLog" Version="4.6.8" />
<PackageReference Include="NLog.Extensions.Logging" Version="1.6.1" />
<PackageReference Include="NLog.WindowsIdentity" Version="4.6.8" />
</ItemGroup>
据我所知,我好像有 nuget 包
.nuget\packages\nlog\4.6.8\lib\netstandard2.0\NLog.dll
namespace NLog.LayoutRenderers
{
[LayoutRenderer("identity")]
public class IdentityLayoutRenderer : LayoutRenderer
【问题讨论】:
-
如果你使用
${environment-user}会发生什么?另见github.com/NLog/NLog/wiki/Environment-User-Layout-Renderer -
对于环境用户,我确实得到了环境变量中“USERNAME”值的内容。但是您的链接很有帮助。 “在 Windows 上,Environment.UserName 属性包含对 Windows GetUserName 函数的调用。在 Unix 平台上,UserName 属性包含对 getpwuid_r 函数的调用。”感谢您的提示!