ASP.NET CORE 2.1中使用Nlog笔记

1:新建解决方案

 

2:在Nuget上面引用

 

 

 3:修改Program.cs文件

 

 

 

 4:新增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">
    <targets>
        <target name="defaultlog" xsi:type="File" keepFileOpen="false" encoding="utf-8" fileName="${basedir}/logs/${level}${shortdate}.log" maxArchiveFiles="100" layout="${longdate}|${level:uppercase=true}|${logger}|${message}" />
        <!--fileName值——表示在程序运行目录,分日志级别按天写入日志文件-->
        <!--maxArchiveFiles值——日志文件最大数量,超出则删除最早的文件-->
        <!--layout值——日志内容格式:时间+日志级别+LoggerName+日志内容-->  
  </targets>
    <rules>
        <!--支持将任意级别、任意LoggerName的日志写入target:defaultlog-->
        <!--其中*就表示任意,可以改为"项目命名空间.*",则只输出对应命名空间下的日志。在Info级别尤为明显-->
        <logger name="*" minlevel="trace" writeTo="defaultlog" />
  </rules>
</nlog>

 5:在Controller中的使用

 

 

 

 

6:生成的文件在当前解决方案bin目录下面(\bin\Debug\netcoreapp2.1\logs)

 

转载于:https://www.cnblogs.com/hbh123/p/11611018.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
.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、付费专栏及课程。

余额充值