标签:oar log oge range 读取 row evel core xmlns
1:添加Nlog的组件
直接在NuGet里面搜索NLog.Web.AspNetCore
2:然后就是配置服务
public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } //public static IHostBuilder CreateHostBuilder(string[] args) => // Host.CreateDefaultBuilder(args) // .ConfigureWebHostDefaults(webBuilder => // { // webBuilder.UseStartup<Startup>(); // webBuilder.UseNLog(); // }); public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }).UseNLog(); //鼠标放在UseNLog() 会显示是IHostBuilder的拓展方法IHostBuilder.UseNlog() }
3:配置文件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" throwConfigExceptions="true" internalLogLevel="info" internalLogFile="E:\log\internal-nlog.txt"> <!--autoReload:修改后自动加载,可能会有延迟--> <!--throwConfigExceptions:NLog日志系统抛出异常--> <!--internalLogLevel:内部日志的级别--> <!--internalLogFile:内部日志保存路径,日志的内容大概就是NLog的版本信息,配置文件的地址等等--> <!--输出日志的配置,用于rules读取--> <targets> <!--write logs to file--> <!--将日志写入文件中,fileName可以指定日志生成的路径--> <target xsi:type="File" name="allfile" fileName="D:\Log\nlog-all-${shortdate}.log" layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" /> <!--同样是将文件写入日志中,写入的内容有所差别,差别在layout属性中体现。写入日志的数量有差别,差别在路由逻辑中体现--> <target xsi:type="File" name="ownFile-web" fileName="D:\Log\nlog-my-${shortdate}.log" layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" /> <target xsi:type="Null" name="blackhole" /> </targets> <rules> <!--路由顺序会对日志打印产生影响。路由匹配逻辑为顺序匹配。--> <!--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" /> </rules> </nlog>
4:记录输出
[ApiController] [Route("[controller]")] public class WeatherForecastController : ControllerBase { private static readonly string[] Summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" }; private readonly ILogger<WeatherForecastController> _logger; private readonly IConfiguration Configuration; public WeatherForecastController(ILogger<WeatherForecastController> logger, IConfiguration configuration) { _logger = logger; Configuration = configuration; } [HttpGet] public IEnumerable<WeatherForecast> Get() { _logger.LogError("这是错误信息"); _logger.LogDebug("这是调试信息"); _logger.LogInformation("这是提示信息"); try { throw new Exception("BOBOBOBOBBBBBBBBBBexection"); } catch (Exception ex) { _logger.LogInformation("这是提示信息"+ ex.Message); } var rng = new Random(); return Enumerable.Range(1, 5).Select(index => new WeatherForecast { Date = DateTime.Now.AddDays(index), TemperatureC = rng.Next(-20, 55), Summary = Summaries[rng.Next(Summaries.Length)] }) .ToArray(); } }
5:效果
标签:oar log oge range 读取 row evel core xmlns
原文地址:https://www.cnblogs.com/ZkbFighting/p/14703489.html