我有一个.NET核心控制台应用程序。
像这样:
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp2.1;netcoreapp3.1</TargetFrameworks>
</PropertyGroup>
我在Windows 10上运行VS 2019。
我无法获取以下布局渲染器来记录任何内容。
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:myfolder1myfolder2binDebugnetcoreapp3.1NLog.Extensions.Logging.dll
2020-02-19 12:03:38.3734 Info Loading assembly file: c:myfolder1myfolder2binDebugnetcoreapp3.1NLog.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:myfolder1myfolder2binDebugnetcoreapp3.1NLog.Extensions.Logging.dll succeeded!
2020-02-19 12:03:38.3847 Info Auto loading assembly file: c:myfolder1myfolder2binDebugnetcoreapp3.1NLog.WindowsIdentity.dll
2020-02-19 12:03:38.3847 Info Loading assembly file: c:myfolder1myfolder2binDebugnetcoreapp3.1NLog.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:myfolder1myfolder2binDebugnetcoreapp3.1NLog.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:myfolder1myfolder2binDebugnetcoreapp3.1nlog.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包
.nugetpackagesnlog4.6.8libnetstandard2.0NLog.dll
namespace NLog.LayoutRenderers
{
[LayoutRenderer("identity")]
public class IdentityLayoutRenderer : LayoutRenderer
从链接(感谢@granadaCoder(
NET Core有不同的默认值。.NET Core中的默认主体策略是无主体。
如果您想保持.NET Framework的行为,请在应用程序的Main方法中调用
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.UnauthenticatedPrincipal)
。
因此中的代码
https://github.com/NLog/NLog/blob/dev/src/NLog/LayoutRenderers/IdentityLayoutRenderer.cs
这是主要的代码:
private static IIdentity GetValue()
{
var currentPrincipal = System.Threading.Thread.CurrentPrincipal;
return currentPrincipal?.Identity;
}
当我运行下面的(稍微编辑一下,只是看看它能做什么(
var currentPrincipal = System.Threading.Thread.CurrentPrincipal;
var ident = currentPrincipal?.Identity;
ident为空。
所以这就是为什么我在日志文件中一无所获。。。。。。。。。。。。。代码解析为null。
goFigure
所以在做了一点家庭作业之后。
Thread.CurrentPrincipal故意为空。
在ASP.NET CORE中,人们会做这样的事情(我发现的典型互联网答案(
您基本上利用了完整的依赖注入大修。。。。。。。并使用它。
对于DI方法,您是否不依赖System.Threading.Thread.CurrentPrincipal"在周围";。(继续阅读以解决此问题(
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddTransient<IPrincipal>(
provider => provider.GetService<IHttpContextAccessor>().HttpContext.User);
// ...
}
使用DotNet.Core(控制台应用程序(。。。你可能需要添加一个
下方的伪代码
public void ConfigureServices(IServiceCollection services)
{
ClaimsPrincipal myprinc = new ClaimsPrincipal( /* you'll need a ClaimsIdentity and some claims here probably */ );
//or
GenericPrincipal myprinc = new GenericPrincipal ( /* you'll need GenericIdentity and some roles here probably */ );
services.AddTransient<IPrincipal>(
provider => myprinc);
// ...
}
所以这不是NLog的问题。这就是DotNetCore处理System.Threading.CurrentPrincipal的方式;不同地
这是一个很好的阅读:
https://davidpine.net/blog/principal-architecture-changes/
现在是解决方法:
我已经验证,如果我这样做:
AppDomain.CurrentDomain.SetPrincipalPolicy(System.Security.Principal.PrincipalPolicy.WindowsPrincipal);
var currentPrincipal = System.Threading.Thread.CurrentPrincipal;
var ident = currentPrincipal?.Identity;
我得到了我(过去(所期望的!
以及日志消息。
2020/02/19 12:03:40.617|INFO|MyCompany.MyObject|***|MyDomainMyUserName|****|HeyYouThere |
我需要在我的linux盒子上运行,看看会发生什么(如果我有时间的话(。。也许是一个";不存在于当前上下文中";例外
请不要使用";AppDomain.CurrentDomain.SetPrincipalPolicy";盲目地。阅读有关System.Threading.Thread.CurrentPrincipal;是老学校。DI可能是未来更好的方法。
此外(如果您选择使用(:
以下是枚举:
https://learn.microsoft.com/en-us/dotnet/api/system.security.principal.principalpolicy?view=netcore-3.1
字段
NoPrincipal 1不应为主体或标识对象创建。
的未经身份验证的主体0主体和标识对象应创建未经身份验证的实体。未经身份验证的实体Name设置为空字符串("(并且IsAuthenticated设置为false。
WindowsPrincipal 2反映与当前执行线程关联的操作系统令牌应该创建,并且关联的操作系统组应该被映射为角色。
您应该考虑每个值。你应该考虑";linux上发生了什么;问题,如果你需要的话。
这会发生吗?
"'此平台不支持Windows主体功能。'">