【发布时间】:2020-10-03 10:41:11
【问题描述】:
我是一名新开发人员,正在尝试使用 C# 语言在 Visual Studio 2019 中创建一个包含多个项目的解决方案。我需要创建一个 NLog.config 文件并供所有其他项目使用。因此,我创建了一个具有 Nlog.Config 文件的 Infrastructure Layer Project,并在 Infrastructure Layer Project
中进行所有配置在 Nlog.Config 文件中我写了
<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>
我用代码创建了一个名为 ApplicationBase 的类
public class ApplicationBase
{
protected Logger Log { get; private set; }
protected ApplicationBase(Type declaringType)
{
Log = LogManager.GetLogger(declaringType.FullName);
}
}
我用代码创建了另一个名为 EmpVO 的类
public class EmpVO : ApplicationBase
{
public EmpVO() : base(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
{
Log.Info("Instance created");
}
}
我创建了另一个名为 presentation layer 的项目,它是一个 Empty 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 文件。
【问题讨论】:
-
当您可以将 NLog 添加到依赖注入容器并完全忘记它时,您是否有理由做所有痛苦的手动工作? github.com/NLog/NLog/wiki/Getting-started-with-ASP.NET-Core-3
-
好吧,我需要在一个解决方案中为多个项目提供一个单一的 nlog
-
这并没有完全改变我提到的任何内容
-
我认为在这种情况下使用 NLog.Web.AspNetCore(参见 Camilo 的链接教程)确实更好(使用当前信息)。但是,我认为在这里查看未创建日志文件的原因也很重要,请参阅我的回答。
标签: c# asp.net-core nlog