ASP.NET Core 2.0 配置NLog日志组件

1).添加nlog.config配置文件

   <?xml version="1.0" encoding="utf-8" ?>

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"

      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

      autoReload="true"

      internalLogLevel="Warn"

      internalLogFile="internal-nlog.txt">

 

  <!-- Load the ASP.NET Core plugin -->

  <extensions>

    <add assembly="NLog.Web.AspNetCore"/>

  </extensions>

 

  <!-- define various log targets -->

  <targets>

    <!-- write logs to file -->

    <target xsi:type="File" name="allfile" fileName="nlog-all-${shortdate}.log"

                 layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" />

 

 

    <target xsi:type="File" name="ownFile-web" fileName="nlog-own-${shortdate}.log"

             layout="${longdate}|${logger}|${uppercase:${level}}|  ${message} ${exception}" />

 

    <target xsi:type="Null" name="blackhole" />

  </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>

 

2).使用NuGet安装相关日志组件

  NLog.Extensions.Logging

  NLog.Web.AspNetCore

 

3).将nlog.config文件配置到环境中并进行日志注册

        

        public void Configure(IApplicationBuilder app, IHostingEnvironment env,ILoggerFactory loggerFactory)

        {

            //添加日志配置路径

            env.ConfigureNLog("nlog.config");

            //注册WEB日志

            loggerFactory.AddNLog();

            app.AddNLogWeb();

 

            //注册控制台日志

            loggerFactory.AddConsole(Configuration.GetSection("Logging")); //控制台日志添加

            loggerFactory.AddDebug();

 

 

            if (env.IsDevelopment())

            {

                app.UseDeveloperExceptionPage();

            }

            else

            {

                app.UseExceptionHandler("/Home/Error");

            }

 

            //添加静态文件中间件

            app.UseStaticFiles();

            //异常中间件

            app.UseExceptionHandler();

 

            app.UseMvc(routes =>

            {

                //默认路由

                routes.MapRoute(

                    name: "default",

                    template: "{controller=Home}/{action=Login}");

            });

        }

 

   4).使用日志记录方法

     085909_Q50q_567785.png

085916_fMQr_567785.png

 

 

转载于:https://my.oschina.net/guanxinsui/blog/1556426

.Net Core2.1+NLog+数据库连接 <?xml version="1.0" encoding="utf-8" ?> <nlog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" autoReload="true" throwExceptions="true" internalLogLevel="warn" internalLogFile="logfiles/internal-nlog.txt"> <targets> <target xsi:type="Null" name="blackhole" /> <target name="database" xsi:type="Database" dbProvider="System.Data.SqlClient" connectionString="Data Source=127.0.0.1;Initial Catalog=MiddleData;User ID=lzhu;Password=bl123456;" > <!-- create table NLog ( Id int identity, Application nvarchar(50) null, Logged datetime null, Level nvarchar(50) null, Message nvarchar(512) null, Logger nvarchar(250) null, Callsite nvarchar(512) null, Exception nvarchar(512) null, constraint PK_NLOG primary key (Id) ) --> <commandText> insert into nlog ( Application, Logged, Level, Message, Logger, CallSite, Exception ) values ( @Application, @Logged, @Level, @Message, @Logger, @Callsite, @Exception ); </commandText> <parameter name="@application" layout="NLogTestDemo" /> <parameter name="@logged" layout="${date}" /> <parameter name="@level" layout="${level}" /> <parameter name="@message" layout="${message}" /> <parameter name="@logger" layout="${logger}" /> <parameter name="@callSite" layout="${callsite:filename=true}" /> <parameter name="@exception" layout="${exception:tostring}" /> </target> </targets> <rules> <!--Skip Microsoft logs and so log only own logs--> <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" /> <logger name="NLogTestDemo.*" minlevel="Info" writeTo="database" /> </rules> </nlog>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值