在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
第一部分:默认Logger支持一、project.json添加日志包引用,并在cmd窗口使用 dotnet restore 命令还原包文件。1 { 2 "version": "1.0.0-*", 3 "buildOptions": { 4 "debugType": "portable", 5 "emitEntryPoint": true 6 }, 7 "dependencies": { 8 "Microsoft.NETCore.App": { 9 "type": "platform", 10 "version": "1.0.0" 11 }, 12 "Microsoft.AspNetCore.Server.Kestrel": "1.0.0", 13 "Microsoft.AspNetCore.Mvc": "1.0.0", 14 "Microsoft.Extensions.Logging": "1.0.0", 15 "Microsoft.Extensions.Logging.Console": "1.0.0", 16 "Microsoft.Extensions.Logging.Debug": "1.0.0", 17 "Microsoft.Extensions.Logging.Filter": "1.0.0" 18 }, 19 "frameworks": { 20 "netcoreapp1.0": { 21 "imports": "dnxcore50" 22 } 23 } 24 } 二、修改Startup.cs文件,添加命令行窗口和调试窗口的日志记录功能。1 using Microsoft.AspNetCore.Builder; 2 using Microsoft.Extensions.DependencyInjection; 3 using Microsoft.Extensions.Logging; 4 5 namespace WebApiFrame 6 { 7 public class Startup 8 { 9 public void ConfigureServices(IServiceCollection services) 10 { 11 // 注入MVC框架 12 services.AddMvc(); 13 } 14 15 public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) 16 { 17 // 添加日志支持 18 loggerFactory.AddConsole(); 19 loggerFactory.AddDebug(); 20 21 // 添加MVC中间件 22 app.UseMvc(); 23 } 24 } 25 } 三、分别使用VS Code的调试功能和命令行的 dotnet run 命令启动程序,浏览器访问 http://localhost:5000/api/users/1 地址后,可以查看到窗口日志打印信息。
第二部分:Logger日志级别日志级别从低到高一共六级,默认情况下,控制台上输出的日志会采取下面的格式: 我们在DemoController.cs控制器里演示如何设置和输出对应级别的日志。 一、修改Startup.cs内容,设置日志级别。1 using Microsoft.AspNetCore.Builder; 2 using Microsoft.Extensions.DependencyInjection; 3 using Microsoft.Extensions.Logging; 4 5 namespace WebApiFrame 6 { 7 public class Startup 8 { 9 public void ConfigureServices(IServiceCollection services) 10 { 11 // 注入MVC框架 12 services.AddMvc(); 13 } 14 15 public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) 16 { 17 // 添加日志支持 18 19 // 设置日志最小级别Warning 20 loggerFactory.AddConsole(LogLevel.Warning); 21 22 //loggerFactory.AddDebug(); 23 24 // 添加MVC中间件 25 app.UseMvc(); 26 } 27 } 28 } 二、修改UsersController.cs文件内容,添加日志输出。1 using System; 2 using Microsoft.AspNetCore.Mvc; 3 using Microsoft.Extensions.Logging; 4 using WebApiFrame.Models; 5 6 namespace WebApiFrame.Controllers 7 { 8 9 [Route("api/[controller]")] 10 public class UsersController : Controller 11 { 12 private ILogger<UsersController> _logger; 13 14 public UsersController(ILogger<UsersController> logger){ 15 _logger = logger; 16 } 17 18 [HttpGet("{id}")] 19 public IActionResult Get(int id) 20 { 21 // 演示日志输出 22 _logger.LogInformation("This is Information Log!"); 23 _logger.LogWarning("This is Warning Log!"); 24 _logger.LogError("This is Error Log!"); 25 26 var user = new User() { Id = id, Name = "Name:" + id, Sex = "Male" }; 27 return new ObjectResult(user); 28 } 29 30 [HttpPost] 31 public IActionResult Post([FromBody] User user){ 32 if(user == null){ 33 return BadRequest(); 34 } 35 36 // TODO:新增操作 37 user.Id = new Random().Next(1, 10); 38 return CreatedAtAction("Get", new { id = user.Id }, user); 39 } 40 41 [HttpPut("{id}")] 42 public IActionResult Put(int id, [FromBody] User user){ 43 if(user == null){ 44 return BadRequest(); 45 } 46 47 // TODO: 更新操作 48 return new NoContentResult(); 49 } 50 51 [HttpDelete("{id}")] 52 public void Delete(int id){ 53 // TODO: 删除操作 54 55 } 56 } 57 } 三、cmd窗口执行 dotnet run 命令,浏览器访问 http://localhost:5000/api/users/1 地址,查看cmd窗口日志输出。
或者,使用Filter设置日志级别,重新执行访问并查看日志输出。1 public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) 2 { 3 // 添加日志支持 4 5 // 设置日志最小输出级别为Error 6 loggerFactory.WithFilter(new FilterLoggerSettings() 7 { 8 // 设置以命名空间开头的日志的最小输出级别 9 { "Microsoft", LogLevel.Warning }, 10 { "WebApiFrame", LogLevel.Error } 11 }).AddConsole(); 12 13 //loggerFactory.AddDebug(); 14 15 // 添加MVC中间件 16 app.UseMvc(); 17 }
第三部分:NLogNLog是一个简单灵活的.Net日志记录类库。相比Log4Net来说,配置要简单许多。 一、project.json添加NLog包引用,并使用 dotnet restore 命令还原包文件。1 { 2 "version": "1.0.0-*", 3 "buildOptions": { 4 "debugType": "portable", 5 "emitEntryPoint": true 6 }, 7 "dependencies": { 8 "Microsoft.NETCore.App": { 9 "type": "platform", 10 "version": "1.0.0" 11 }, 12 "Microsoft.AspNetCore.Server.Kestrel": "1.0.0", 13 "Microsoft.AspNetCore.Mvc": "1.0.0", 14 "Microsoft.Extensions.Logging": "1.0.0", 15 "Microsoft.Extensions.Logging.Console": "1.0.0", 16 "Microsoft.Extensions.Logging.Debug": "1.0.0", 17 "Microsoft.Extensions.Logging.Filter": "1.0.0", 18 "NLog.Extensions.Logging": "1.0.0-rtm-alpha2" 19 }, 20 "frameworks": { 21 "netcoreapp1.0": { 22 "imports": "dnxcore50" 23 } 24 } 25 } 二、添加NLog配置文件nlog.config。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="Warn" 6 internalLogFile="internal-nlog.txt"> 7 8 <!-- define various log targets --> 9 <targets> 10 <!-- write logs to file --> 11 <target xsi:type="File" name="allfile" fileName="nlog-all-${shortdate}.log" 12 layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" /> 13 14 15 <target xsi:type="File" name="ownFile-web" fileName="nlog-own-${shortdate}.log" 16 layout="${longdate}|${logger}|${uppercase:${level}}| ${message} ${exception}" /> 17 18 <target xsi:type="Null" name="blackhole" /> 19 </targets> 20 21 <rules> 22 <!--All logs, including from Microsoft--> 23 <logger name="*" minlevel="Trace" writeTo="allfile" /> 24 25 <!--Skip Microsoft logs and so log only own logs--> 26 <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" /> 27 <logger name="*" minlevel="Trace" writeTo="ownFile-web" /> 28 </rules> 29 </nlog> 三、修改Startup.cs文件内容,添加NLog支持。1 using Microsoft.AspNetCore.Builder; 2 using Microsoft.Extensions.DependencyInjection; 3 using Microsoft.Extensions.Logging; 4 using NLog.Extensions.Logging; 5 6 namespace WebApiFrame 7 { 8 public class Startup 9 { 10 public void ConfigureServices(IServiceCollection services) 11 { 12 // 注入MVC框架 13 services.AddMvc(); 14 } 15 16 public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) 17 { 18 // 添加日志支持 19 //loggerFactory.AddConsole(); 20 //loggerFactory.AddDebug(); 21 22 // 添加NLog日志支持 23 loggerFactory.AddNLog(); 24 25 // 添加MVC中间件 26 app.UseMvc(); 27 } 28 } 29 } 四、重新执行访问并查看日志输出。生成的日志文件和内容
第四部分、其他相关内容最后,同时放开三种日志输出方式,不修改控制器里的任何代码,可以发现将同时以三种方式记录相同的日志内容。 1 public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) 2 { 3 // 添加日志支持 4 loggerFactory.AddConsole(); 5 loggerFactory.AddDebug(); 6 7 // 添加NLog日志支持 8 loggerFactory.AddNLog(); 9 10 // 添加MVC中间件 11 app.UseMvc(); 12 }
|
请发表评论