问题描述
我是新开发人员,试图在Visual Studio 2019中创建具有C#语言的多个项目的解决方案。我需要创建一个NLog.config文件,并由所有其他项目使用。因此,我创建了一个具有Nlog.Config文件的 Infrastructure Layer Project ,并在 Infrastructure Layer Project
中进行了所有配置。<targets>
<target xsi:type="File" name="LogFile" fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${message}" />
</targets>
<rules>
<logger name="LogRules" minlevel="Debug" writeto="LogFile" />
</rules>
public class ApplicationBase
{
protected Logger Log { get; private set; }
protected ApplicationBase(Type declaringType)
{
Log = LogManager.GetLogger(declaringType.FullName);
}
}
public class EmpVO : ApplicationBase
{
public EmpVO() : base(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
{
Log.Info("Instance created");
}
}
我创建另一个名为表示层的项目,这是一个空web ASP.net Core 3.0,带有start.cs文件
我写了
public void ConfigureServices(IServiceCollection services)
{
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app,IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
EmpVO emp = new EmpVO();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/",async context =>
{
await context.Response.WriteAsync("Hello World!");
});
});
}
该解决方案可以正常工作且没有错误,并且可以毫无错误地构建它。但是无法生成初始的NLog文件。
所以我应该怎么做才能使其生成NLog并具有一个NLog文件供所有其他项目使用。
解决方法
name
中的<logger>
属性是名称过滤器。因此,当前您仅将日志从LogRules记录器路由到目标。
否则以name="*"
开始:
<logger name="*" minlevel="Debug" writeTo="LogFile" />
如果不清楚您的记录器名称是什么,您也可以使用${logger}