.Net Core 中使用NLog作为日志中间件

⒈安装相关依赖

  NLog

  NLog.Web.AspNetCore

⒉在项目的根目录中创建NLog配置文件

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
 3       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4       autoReload="true"
 5       internalLogLevel="Info"
 6       internalLogFile="c:\temp\internal-nlog.txt">
 7 
 8   <!-- 启用asp.net核心布局渲染器- -->
 9   <extensions>
10     <add assembly="NLog.Web.AspNetCore"/>
11   </extensions>
12 
13   <!-- 要写入的目标 -->
14   <targets>
15     <!-- 将日志写入到文件中  -->
16     <target xsi:type="File" name="allfile" fileName="c:\temp\nlog-all-${shortdate}.log"
17             layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
18 
19     <!-- 另一个文件日志,只有自己的日志。使用一些ASP.NET核心渲染器 -->
20     <target xsi:type="File" name="ownFile-web" fileName="c:\temp\nlog-own-${shortdate}.log"
21             layout="${longdate}|${event-properties:item=EventId_Id}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
22   </targets>
23 
24   <!-- 从记录器名称映射到目标的规则 -->
25   <rules>
26     <!--所有日志,包括来自Microsoft的日志-->
27     <logger name="*" minlevel="Trace" writeTo="allfile" />
28 
29     <!--跳过非关键的Microsoft日志,因此只记录自己的日志-->
30     <logger name="Microsoft.*" maxlevel="Info" final="true" />
31     <!-- BlackHole without writeTo -->
32     <logger name="*" minlevel="Trace" writeTo="ownFile-web" />
33   </rules>
34 </nlog>

⒊更改配置文件属性

⒋修改Program.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.IO;
 4 using System.Linq;
 5 using System.Threading.Tasks;
 6 using Microsoft.AspNetCore;
 7 using Microsoft.AspNetCore.Hosting;
 8 using Microsoft.Extensions.Configuration;
 9 using Microsoft.Extensions.Logging;
10 using NLog.Web;
11 
12 namespace AutoMapperCore
13 {
14     public class Program
15     {
16         public static void Main(string[] args)
17         {
18             var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
19             try
20             {
21                 logger.Debug("init main");
22                 CreateWebHostBuilder(args).Build().Run();
23             }
24             catch (Exception e)
25             {
26                 logger.Error(e, "Stopped program because of exception");
27                 throw;
28             }
29             finally
30             {
31                 NLog.LogManager.Shutdown();
32             }
33             
34         }
35 
36         public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
37             WebHost.CreateDefaultBuilder(args)
38                 .UseStartup<Startup>()
39             .ConfigureLogging(logging =>
40             {
41                 logging.ClearProviders();
42                 logging.SetMinimumLevel(Microsoft.Extensions.Logging.LogLevel.Trace);
43 
44             }).UseNLog();
45     }
46 }

⒌配置appsettings.json

1 {
2   "Logging": {
3     "LogLevel": {
4       "Default": "Trace",
5       "Microsoft": "Information"
6     }
7   },
8   "AllowedHosts": "*"
9 }

⒍在代码中注入ILogger写日志

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Threading.Tasks;
 5 using AutoMapper;
 6 using AutoMapperTest.Entities;
 7 using Microsoft.AspNetCore.Mvc;
 8 using Microsoft.Extensions.Logging;
 9 
10 namespace AutoMapperCore.Controllers
11 {
12     public class UsersController : Controller
13     {
14         private readonly IMapper _mapper;
15         private readonly ILogger<UsersController> _logger;
16         public UsersController(IMapper mapper, ILogger<UsersController> logger)
17         {
18             this._mapper = mapper;
19             this._logger = logger;
20         }
21         public IActionResult Index()
22         {
23             UsersInputDto input = new UsersInputDto()
24             {
25                 id = 1, firstname = "fan", lastname = "qi", uname = "fanqisoft", pwd = "admin", enabled = 1
26             };
27             _logger.LogInformation("Dto转换实体对象成功!");
28             Users users = _mapper.Map<Users>(input);
29             return View();
30         }
31     }
32 }

 

posted @ 2019-05-01 22:24  SpringCore  阅读(1376)  评论(0编辑  收藏  举报