打印到控制台
日志保存到NLog目录下
每天保存一个文件夹
按类名保存日志文件 日志内容标注日志等级
- 代码中配置方式
var config = new NLog.Config.LoggingConfiguration(); // 定义文件日志目标 var timeLevelClassFileTarget = new NLog.Targets.FileTarget("timeLevelClassFile") { FileName = "NLog/${shortdate}/${logger}.log", Layout = "${longdate} ${level:uppercase=true} ${logger} ${message} ${exception:format=tostring}" }; config.AddTarget("timeLevelClassFile", timeLevelClassFileTarget); // 定义控制台日志目标 var consoleTarget = new NLog.Targets.ColoredConsoleTarget("console") { Layout = "${date:format=HH\\:mm\\:ss} ${level:uppercase=true} ${logger} ${message} ${exception:format=tostring}", UseDefaultRowHighlightingRules = false }; consoleTarget.RowHighlightingRules.Add(new NLog.Targets.ConsoleRowHighlightingRule("level == LogLevel.Debug", NLog.Targets.ConsoleOutputColor.Gray, NLog.Targets.ConsoleOutputColor.Black)); consoleTarget.RowHighlightingRules.Add(new NLog.Targets.ConsoleRowHighlightingRule("level == LogLevel.Info", NLog.Targets.ConsoleOutputColor.Green, NLog.Targets.ConsoleOutputColor.Black)); consoleTarget.RowHighlightingRules.Add(new NLog.Targets.ConsoleRowHighlightingRule("level == LogLevel.Warn", NLog.Targets.ConsoleOutputColor.Yellow, NLog.Targets.ConsoleOutputColor.Black)); consoleTarget.RowHighlightingRules.Add(new NLog.Targets.ConsoleRowHighlightingRule("level == LogLevel.Error", NLog.Targets.ConsoleOutputColor.Red, NLog.Targets.ConsoleOutputColor.Black)); consoleTarget.RowHighlightingRules.Add(new NLog.Targets.ConsoleRowHighlightingRule("level == LogLevel.Fatal", NLog.Targets.ConsoleOutputColor.Red, NLog.Targets.ConsoleOutputColor.White)); config.AddTarget("console", consoleTarget); // 定义规则:将所有信息级别及以上的日志写入文件和控制台 config.LoggingRules.Add(new NLog.Config.LoggingRule("*", NLog.LogLevel.Info, timeLevelClassFileTarget)); config.LoggingRules.Add(new NLog.Config.LoggingRule("*", NLog.LogLevel.Info, consoleTarget)); NLog.LogManager.Configuration = config; var Logger = NLog.LogManager.GetCurrentClassLogger(); Logger.Info("info"); Logger.Debug("Debug"); Logger.Error("Error"); Logger.Warn("Warn"); Logger.Fatal("Fatal");
- appsettings配置方式
"NLog": { "throwConfigExceptions": true, "targets": { "timeLevelClassFile": { "type": "File", "fileName": "NLog/${shortdate}/${logger}.log", "layout": "${longdate} ${level:uppercase=true} ${logger} ${message} ${exception:format=tostring}" }, "console": { "type": "ColoredConsole", "layout": "${date:format=HH\\:mm\\:ss} ${level:uppercase=true} ${logger} ${message} ${exception:format=tostring}", "UseDefaultRowHighlightingRules": false, "rowHighlightingRules": [ { "condition": "level == LogLevel.Debug", "foregroundColor": "Gray" }, { "condition": "level == LogLevel.Info", "foregroundColor": "Green" }, { "condition": "level == LogLevel.Warn", "foregroundColor": "Yellow" }, { "condition": "level == LogLevel.Error", "foregroundColor": "Red" }, { "condition": "level == LogLevel.Fatal", "foregroundColor": "Red", "backgroundColor": "White" } ] } }, "rules": [ { "logger": "*", "minLevel": "Information", "writeTo": "timeLevelClassFile" }, { "logger": "*", "minLevel": "Information", "writeTo": "console" } ] },
- 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"> <targets> <target name="timeLevelClassFile" xsi:type="File" fileName="NLog/${shortdate}/${logger}.log" layout="${longdate} ${level:uppercase=true} ${logger} ${message} ${exception:format=tostring}" /> <target name="console" xsi:type="ColoredConsole" layout="${date:format=HH\\:mm\\:ss} ${level:uppercase=true} ${logger} ${message} ${exception:format=tostring}"> <highlight-row foregroundColor="Gray" condition="level == LogLevel.Debug" /> <highlight-row foregroundColor="Green" condition="level == LogLevel.Info" /> <highlight-row foregroundColor="Yellow" condition="level == LogLevel.Warn" /> <highlight-row foregroundColor="Red" condition="level == LogLevel.Error" /> <highlight-row foregroundColor="Red" backgroundColor="White" condition="level == LogLevel.Fatal" /> </target> </targets> <rules> <logger name="*" minlevel="Info" writeTo="timeLevelClassFile" /> <logger name="*" minlevel="Info" writeTo="console" /> </rules> </nlog>
标签:level,C#,ConsoleOutputColor,LogLevel,自用,new,NLog,Targets From: https://www.cnblogs.com/Echoxxx/p/18107515