繁体   English   中英

ASP.Net Core Web MVC 中未生成 NLog 文件

[英]NLog File is not Generated in ASP.Net Core Web MVC

我是新开发人员,试图创建一个解决方案,该解决方案在 Visual Studio 2019 中使用 C# 语言拥有多个项目。 我需要创建一个 NLog.config 文件并供所有其他项目使用。 所以我创建一个基础设施层项目有Nlog.Config文件,我做的所有配置,基础设施层项目

在我写的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 的项目,它是一个空的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}

另请参阅记录器规则的文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM