.net 6中使用NLog写文本日志
小白浏览:5152022-08-01 11:31:20本文累计收益:0我也要赚钱

本文讲解如何在.net core 6中使用NLog写文本日志,首先需要在项目中添加NLog.Web.AspNetCore引用。

然后准备配置文件,在项目中添加CfgFile文件夹,在其下新建NLog.config文件,用于对NLog的配置,代码如下:

<?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">

	<!-- optional, add some variables
  https://github.com/nlog/NLog/wiki/Configuration-file#variables
  -->
	<variable name="myvar" value="myvalue"/>

	<!--
  See https://github.com/nlog/nlog/wiki/Configuration-file
  for information on customizing logging rules and outputs.
   -->
	<targets>
		<!--
    add your targets here
    See https://github.com/nlog/NLog/wiki/Targets for possible targets.
    See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers.
    -->
		<target xsi:type="File" name="allfile" fileName="NLog\nlog-all-${shortdate}.log"
				layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" />
		<!--同样是将文件写入日志中,写入的内容有所差别,差别在layout属性中体现。写入日志的数量有差别,差别在路由逻辑中体现-->
		<target xsi:type="File" name="ownFile-web" fileName="NLog\nlog-my-${shortdate}.log"
				 layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" />
		<target xsi:type="Null" name="blackhole" />
		<!--
    Write events to a file with the date in the filename.
    <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
            layout="${longdate} ${uppercase:${level}} ${message}" />
    -->
	</targets>

	<rules>
		<!--<logger name="*" minlevel="Trace" writeTo="AllDatabase" />-->
		<!-- add your logging rules here -->
		<!--路由顺序会对日志打印产生影响。路由匹配逻辑为顺序匹配。-->
		<!--All logs, including from Microsoft-->
		<logger name="*" minlevel="Trace" writeTo="allfile" />
		<!--Skip Microsoft logs and so log only own logs-->
		<!--以Microsoft打头的日志将进入此路由,由于此路由没有writeTo属性,所有会被忽略-->
		<!--且此路由设置了final,所以当此路由被匹配到时。不会再匹配此路由下面的路由。未匹配到此路由时才会继续匹配下一个路由-->
		<logger name="Microsoft.*" minlevel="Trace"  final="true" />
		<!--上方已经过滤了所有Microsoft.*的日志,所以此处的日志只会打印除Microsoft.*外的日志-->
		<logger name="*" minlevel="Trace" writeTo="ownFile-web" />
		<!--
    Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace)  to "f"
    <logger name="*" minlevel="Debug" writeTo="f" />
    -->
	</rules>
</nlog>

然后在Program.cs进行注册NLog,注册代码如下:

using NLog.Web;
var builder = WebApplication.CreateBuilder(args);

#region NLog
{
    //引用NLog.Web;
    //引用
    builder.Logging.AddNLog("CfgFile/NLog.Config");
}
#endregion

最后在Controller进行调用使用,代码如下:

public class FirstController : Controller
    {
        private readonly ILogger<FirstController> _Logger;
        private readonly ILoggerFactory _LoggerFactory;
        public FirstController(ILogger<FirstController> logger,ILoggerFactory loggerFactory) {
            this._Logger = logger;
            this._Logger.LogInformation($"{this.GetType().Name}被构造了log1");
            this._LoggerFactory = loggerFactory;
            ILogger<FirstController> _Logger2 =
            this._LoggerFactory.CreateLogger<FirstController>();
            _Logger2.LogInformation($"{this.GetType().Name}被构造了log2");
        }
        public IActionResult Index()
        {
            ILogger<FirstController> _Logger3 =
            this._LoggerFactory.CreateLogger<FirstController>();
            _Logger3.LogInformation($"index被执行了");

            this._Logger.LogInformation($"index被执行了");
            return View();
        }
    }
评论列表
发表评论
+ 关注