如何使用 nlog 和 asp.net core rc2 获得可用的 ${callsite}
How to get a usable ${callsite} using nlog and asp.net core rc2
我在 asp.net 核心 rc2 中使用 NLog。 ("NLog.Extensions.Logging": "1.0.0-rc2-final-2016-05-21") 我很难在布局中使用 ${callsite} 标记获取日志记录事件的位置。我更喜欢这个而不是在消息中键入它,因为我想将它呈现到它自己的数据库列中。
我的创业公司是这样的:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddNLog();
env.ConfigureNLog("NLog.config");
我的 NLog 配置:
<target xsi:type="File" name="infolog" fileName="${basedir}/logs/info-${shortdate}.log"
layout="${longdate}|${level}|${callsite:className=true:methodName=true}|${machinename}|${message}" />
这样调用:
Logger.LogInformation("Information: User > Get");
唉,输出总是:
2016-06-20 16:00:03.4458|Info|Microsoft.Extensions.Logging.Logger.Log ...
有没有办法使用 Microsoft 日志记录扩展和 NLog 获取写入日志的 class/method 名称?
我刚刚在使用 Owin + NLog 时遇到了这个问题,我发现你需要告诉 NLog “忽略”那些日志记录包装器,这可以通过使用 LogManager.AddHiddenAssembly 来完成,在我的例子中,我只需要将它添加到我的 Startup class:
LogManager.AddHiddenAssembly(typeof(NLogFactory).Assembly);
LogManager.AddHiddenAssembly(typeof(LoggerExtensions).Assembly);
你的情况,我觉得应该是:
LogManager.AddHiddenAssembly(typeof(Microsoft.Extensions.Logging.Logger.Log).Assembly);
我还没有测试过,但它可以为您指明正确的方向。
希望对您有所帮助。
我有办法。
更新包到最新,1.0.0-rtm-alpha3
检查依赖关系。如果您看到 4.3.4,则需要对其进行调整。
- 找到包 .nuspec 文件。
- 在记事本中打开文件。
编辑文件,将NLog版本改为4.3.5
恢复包。这将更新项目中的依赖项。它现在应该可以正常工作了。
我在 asp.net 核心 rc2 中使用 NLog。 ("NLog.Extensions.Logging": "1.0.0-rc2-final-2016-05-21") 我很难在布局中使用 ${callsite} 标记获取日志记录事件的位置。我更喜欢这个而不是在消息中键入它,因为我想将它呈现到它自己的数据库列中。
我的创业公司是这样的:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddNLog();
env.ConfigureNLog("NLog.config");
我的 NLog 配置:
<target xsi:type="File" name="infolog" fileName="${basedir}/logs/info-${shortdate}.log"
layout="${longdate}|${level}|${callsite:className=true:methodName=true}|${machinename}|${message}" />
这样调用:
Logger.LogInformation("Information: User > Get");
唉,输出总是:
2016-06-20 16:00:03.4458|Info|Microsoft.Extensions.Logging.Logger.Log ...
有没有办法使用 Microsoft 日志记录扩展和 NLog 获取写入日志的 class/method 名称?
我刚刚在使用 Owin + NLog 时遇到了这个问题,我发现你需要告诉 NLog “忽略”那些日志记录包装器,这可以通过使用 LogManager.AddHiddenAssembly 来完成,在我的例子中,我只需要将它添加到我的 Startup class:
LogManager.AddHiddenAssembly(typeof(NLogFactory).Assembly);
LogManager.AddHiddenAssembly(typeof(LoggerExtensions).Assembly);
你的情况,我觉得应该是:
LogManager.AddHiddenAssembly(typeof(Microsoft.Extensions.Logging.Logger.Log).Assembly);
我还没有测试过,但它可以为您指明正确的方向。
希望对您有所帮助。
我有办法。 更新包到最新,1.0.0-rtm-alpha3
检查依赖关系。如果您看到 4.3.4,则需要对其进行调整。
- 找到包 .nuspec 文件。
- 在记事本中打开文件。
编辑文件,将NLog版本改为4.3.5
恢复包。这将更新项目中的依赖项。它现在应该可以正常工作了。