Net Core教程

AspNetCore使用NLog做日志记录

本文主要是介绍AspNetCore使用NLog做日志记录,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

安装包

安装nuget包NLog.Web.AspNetCoreMicrosoft.Extensions.Logging

配置文件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"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd"
      autoReload="true"
      throwExceptions="false"
      internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
	<variable name="myvar" value="myvalue" />
	<extensions>
		<add assembly="NLog.Web.AspNetCore" />
	</extensions>
	<targets>
		<!--Error保存至文件-->
		<target name="error_file" xsi:type="File" maxArchiveFiles="30"  encoding="utf-8"
            fileName="${basedir}/Logs/${date:yyyyMMdd}_Error.log"
            archiveFileName="${basedir}/Logs/${date:yyyyMMdd}_Error.{#}.log"
            archiveDateFormat="yyyyMMdd"
            archiveAboveSize="104857600"
            archiveNumbering="Sequence"
            layout="${date:yyyy-MM-dd HH\:mm\:ss} ${message} ${onexception:${exception:format=tostring} ${newline} ${stacktrace}" />
		<!--Trace保存至文件-->
		<target name="trace_file" xsi:type="File" maxArchiveFiles="30" encoding="utf-8"
            fileName="${basedir}/Logs/${date:yyyyMMdd}_Trace.log"
            archiveFileName="${basedir}/Logs/${date:yyyyMMdd}_Trace.{#}.log"
            archiveDateFormat="yyyyMMdd"
            archiveAboveSize="104857600"
            archiveNumbering="Sequence"
            layout="${date:yyyy-MM-dd HH\:mm\:ss} ${uppercase:${level}}: ${message}" />
	</targets>

	<rules>
		<logger name="*" minlevel="Info" maxlevel="Warn" writeTo="trace_file" />
		<logger name="*" minlevel="Error" writeTo="error_file" />
	</rules>
</nlog>

Program修改

正常的依赖注入使用日志

这篇关于AspNetCore使用NLog做日志记录的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!