【发布时间】:2018-06-02 17:27:21
【问题描述】:
我试图让 NLog 在我的 asp.net 核心 web api 2 中工作,它返回 json,但我想登录以进行跟踪使用等。
我已经按照以下设置了我的 nlog.config
<?xml version="1.0"?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogLevel="Warn"
internalLogFile="c:\temp\internal-nlog.txt">
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<targets>
<target name="allfile" xsi:type="File"
fileName="${basedir}\logs\GDStationaryNetCore\${shortdate}.log"
encoding="utf-8"
layout="[${longdate}][${machinename}][${level}] ${message} ${exception}" />
</targets>
<rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="allfile" />
<!--Skip Microsoft logs and so log only own logs-->
<logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
<logger name="*" minlevel="Trace" writeTo="ownFile-web" />
</rules>
</nlog>
然后我将启动时的配置部分更改为如下。
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, LoggerFactory loggerFactory)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
env.ConfigureNLog("nlog.config");
// make sure Chinese chars don't fk up
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
//add NLog to ASP.NET Core
loggerFactory.AddNLog();
//add NLog.Web
app.AddNLogWeb();
//needed for non-NETSTANDARD platforms: configure nlog.config in your project root. NB: you need NLog.Web.AspNetCore package for this.
env.ConfigureNLog("nlog.config");
app.UseMvc();
}
但是当我尝试编译时,它给出了以下内容
app.AddNLogWeb();是否过时使用 UseNLog 来代替
但是当我尝试在应用程序上找到它时。不存在希望有人能帮忙,
【问题讨论】:
标签: c# asp.net-core asp.net-web-api2 nlog