net5 mvc NLog 日志配置

安装nuget包

NLog.Web.AspNetCore

 

官网配置文档

新建 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="Debug" xsi:type="file" fileName="${basedir}/Logs/Debug/${shortdate}-Debug.log" layout="----------------日志记录开始----------------${newline}【日志时间】:${longdate} [线程:${threadid}] ${newline}【日志级别】:${level:uppercase=true}${newline}【相关信息】:${callsite}${newline}${message}${newline}${onexception:${exception:format=toString}${newline}} ${newline}${newline}" />
<target name="Warn" xsi:type="file" fileName="${basedir}/Logs/Warn/${shortdate}-Warn.log" layout="----------------日志记录开始----------------${newline}【日志时间】:${longdate} [线程:${threadid}] ${newline}【日志级别】:${level:uppercase=true}${newline}【相关信息】:${callsite}${newline}${message}${newline}${onexception:${exception:format=toString}${newline}} ${newline}${newline}" />
<target name="Error" xsi:type="file" fileName="${basedir}/Logs/Error/${shortdate}-Error.log" layout="----------------日志记录开始----------------${newline}【日志时间】:${longdate} [线程:${threadid}] ${newline}【日志级别】:${level:uppercase=true}${newline}【相关信息】:${callsite}${newline}${message}${newline}${onexception:${exception:format=toString}${newline}} ${newline}${newline}" />
<target name="Info" xsi:type="file" fileName="${basedir}/Logs/Info/${shortdate}-Info.log" layout="----------------日志记录开始----------------${newline}【日志时间】:${longdate} [线程:${threadid}]${newline}【日志级别】:${level:uppercase=true}${newline}【相关信息】:${callsite}${newline}${message}${newline}${onexception:${exception:format=toString}${newline}} ${newline}${newline}" />
<target name="Fatal" xsi:type="file" fileName="${basedir}/Logs/Fatal/${shortdate}-Fatal.log" layout="----------------日志记录开始----------------${newline}【日志时间】:${longdate} [线程:${threadid}]${newline}【日志级别】:${level:uppercase=true}${newline}【相关信息】:${callsite}${newline}${message}${newline}${onexception:${exception:format=toString}${newline}} ${newline}${newline}" />
<target name="Trace" xsi:type="file" fileName="${basedir}/Logs/Trace/${shortdate}-Trace.log" layout="----------------日志记录开始----------------${newline}【日志时间】:${longdate} [线程:${threadid}]${newline}【日志级别】:${level:uppercase=true}${newline}【相关信息】:${callsite}${newline}${message}${newline}${onexception:${exception:format=toString}${newline}} ${newline}${newline}" />
</targets>
<rules>
<logger name="*" levels="Info" writeTo="Info" />
<logger name="*" levels="Warn" writeTo="Warn" />
<logger name="*" levels="Trace" writeTo="Trace" />
<logger name="*" levels="Debug" writeTo="Debug" />
<logger name="*" levels="Error" writeTo="Error" />
<logger name="*" levels="Fatal" writeTo="Fatal" />
</rules>
</nlog>

<!--${stacktrace}-->

 

 Program 文件配置

 public static void Main(string[] args)
        {
            NLog.Web.NLogBuilder.ConfigureNLog("nlog.config");
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                }).ConfigureLogging(logging =>
                {
                    logging.ClearProviders(); //移除已经注册的其他日志处理程序
                    logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);//设置最小的日志级别
                }).UseNLog();

 使用

  public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {

            _logger.LogError("xxxxxxxxxxxLogError");

            _logger.LogInformation("xxxxxxxxxxxLogInformation");

......

 

posted @ 2021-09-04 22:45  xmao-xmao  阅读(272)  评论(0)    收藏  举报