ASP.Net 核心 Web MVC 中未生成 NLog 文件
NLog File is not Generated in ASP.Net Core Web MVC
我是一名新开发人员,试图在 Visual Studio 2019 年使用 C# 语言创建一个包含多个项目的解决方案。我需要创建一个 NLog.config 文件并供所有其他项目使用。因此,我创建了一个 基础架构层项目 ,其中包含 Nlog.Config 文件,并在 基础架构层项目[=15 中进行了所有配置=]
在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 的 class,代码为
public class ApplicationBase
{
protected Logger Log { get; private set; }
protected ApplicationBase(Type declaringType)
{
Log = LogManager.GetLogger(declaringType.FullName);
}
}
我创建了另一个名为 EmpVO 的 class,代码为
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 文件供所有其他项目使用。
<logger>
中的 name
属性是名称过滤器。所以目前您只能将日志从 LogRules 记录器路由到您的目标。
否则从 name="*"
开始:
<logger name="*" minlevel="Debug" writeTo="LogFile" />
如果不清楚您的记录器名称是什么,您也可以使用 ${logger}
进行记录
我是一名新开发人员,试图在 Visual Studio 2019 年使用 C# 语言创建一个包含多个项目的解决方案。我需要创建一个 NLog.config 文件并供所有其他项目使用。因此,我创建了一个 基础架构层项目 ,其中包含 Nlog.Config 文件,并在 基础架构层项目[=15 中进行了所有配置=]
在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 的 class,代码为
public class ApplicationBase
{
protected Logger Log { get; private set; }
protected ApplicationBase(Type declaringType)
{
Log = LogManager.GetLogger(declaringType.FullName);
}
}
我创建了另一个名为 EmpVO 的 class,代码为
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 文件供所有其他项目使用。
<logger>
中的 name
属性是名称过滤器。所以目前您只能将日志从 LogRules 记录器路由到您的目标。
否则从 name="*"
开始:
<logger name="*" minlevel="Debug" writeTo="LogFile" />
如果不清楚您的记录器名称是什么,您也可以使用 ${logger}