在一些特定场景的业务需求下,日志须要写入到不一样的路径下提供日志分析。
第一种:默认Nlog能够经过日志级别来区分路径,
——优势是不须要额外配置,开箱即用
——缺点是不够灵活,若是超过级别数量,则不知足需求git
第二种:经过定义FileTarget来根据业务写入不一样的地址json
废话很少说了,直接上代码
一、建立NetCore,而且引入Nlog和NLog.Web.AspNetCore 这个就不介绍和贴图了app
二、建立nlog配置文件dom
<?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" internalLogLevel="Warn" internalLogFile="internal-nlog.txt"> <targets> <target xsi:type="File" name="file" fileName="logs/nlog-all-${shortdate}.log" layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" /> <target xsi:type="File" name="exception" fileName="logs/nlog-exception-${shortdate}.log" layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" /> <target xsi:type="File" name="trace" fileName="logs/nlog-trace-${shortdate}.log" layout="${longdate}|${logger}|${uppercase:${level}}|${message} ${exception}" encoding="utf-8" /> <target xsi:type="Null" name="blackhole" /> </targets> <rules> <logger name="*" minlevel="Trace" writeTo="file" /> <!--日志级别:Trace -》Debug-》 Information -》Warning-》 Error-》 Critical--> <!--排除系统日志--> <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" /> <logger name="*" minlevel="Trace" writeTo="trace" /> <logger name="*" minlevel="Error" maxlevel="Error" writeTo="exception" /> </rules> </nlog>
三、注册,在Starup.cs文件中ide
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } loggerFactory.AddNLog(); env.ConfigureNLog("nlog.config"); app.UseMvc(); }
public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .UseNLog();
五、为了扩展,咱们新建一个类来处理日志的写入,Process是被调用的写入方法ui
using System; using System.Text; using NLog; namespace CurLogger { public class Logger { private static Logger instance = new Logger(); private Logger() { } public static Logger GetInstance() { return instance; } /// <summary> /// 企业认证日志地址 /// </summary> public static string IdentityEnterprise; /// <summary> /// 政务认证日志地址 /// </summary> public static string IdentityGovernmentAffairs; /// <summary> /// 政务错误日志地址 /// </summary> public static string IdentityError; /// <summary> /// 随机签名日志地址 /// </summary> public static string RandomSign; /// <summary> /// 票据生成日志地址 /// </summary> public static string TicketCreate { get; set; } /// <summary> /// 票据验证日志地址 /// </summary> public static string TicketInentity { get; set; } /// <summary> /// 票据异常日志地址 /// </summary> public static string TicketError { get; set; } public void Setting(LoggerConfig config) { IdentityEnterprise = config.IdentityEnterprise; IdentityGovernmentAffairs = config.IdentityGovernmentAffairs; IdentityError = config.IdentityError; RandomSign = config.RandomSign; TicketCreate = config.TicketCreate; TicketInentity = config.TicketInentity; TicketError = config.TicketError; } NLog.Logger _logger; private Logger(NLog.Logger logger) { _logger = logger; } public Logger(string name) : this(LogManager.GetLogger(name)) { } public static Logger Default { get; private set; } static Logger() { Default = new Logger(LogManager.GetCurrentClassLogger()); } public void Process(string msg, string path) { Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); var fileTarget = NLog.LogManager.Configuration.FindTargetByName<NLog.Targets.FileTarget>("trace"); fileTarget.FileName = "logs/" + path + "/${shortdate}_log.txt"; fileTarget.Encoding = Encoding.GetEncoding("GB2312"); _logger.Info(msg); } } public class LoggerConfig { //认证企业日志地址 public string IdentityEnterprise { get; set; } //认证政务日志地址 public string IdentityGovernmentAffairs { get; set; } //认证错误日志地址 public string IdentityError { get; set; } //随机签名日志地址 public string RandomSign { get; set; } //票据生成日志地址 public string TicketCreate { get; set; } //票据验证日志地址 public string TicketInentity { get; set; } //票据异常日志地址 public string TicketError { get; set; } } }
六、地址能够经过配置文件配置,更加灵活,appsetting.jsonthis
"LogConfig": { "IdentityEnterprise": "identity/enterprise", "IdentityGovernmentAffairs": "identity/governmentaffairs", "IdentityError": "identity/error", "RandomSign": "randomsign", "TicketCreate": "ticket/create", "TicketInentity": "ticket/identity", "TicketError": "ticket/error" }
七、在Startup.cs中赋值给Logger里面的地址spa
#region Init_Log_Path Logger.GetInstance().Setting(new LoggerConfig() { IdentityEnterprise = Configuration["LogConfig:IdentityEnterprise"], IdentityGovernmentAffairs = Configuration["LogConfig:IdentityGovernmentAffairs"], IdentityError = Configuration["LogConfig:IdentityError"], RandomSign = Configuration["LogConfig:RandomSign"], TicketCreate = Configuration["LogConfig:TicketCreate"], TicketInentity = Configuration["LogConfig:TicketInentity"], TicketError = Configuration["LogConfig:TicketError"] }); #endregion
八、使用方法debug