1, first download NLog framework, in vs NuGet NLog in search, download and install NLog.Config
2, NLog.Config configuration file, I used the following configuration
<?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" xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd" autoReload="true" throwExceptions="false" internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log"> <!-- optional, add some variables https://github.com/nlog/NLog/wiki/Configuration-file#variables --> <variable name="myvar" value="myvalue"/> <!-- See https://github.com/nlog/nlog/wiki/Configuration-file for information on customizing logging rules and outputs. --> <targets> <!-- add your targets here See https://github.com/nlog/NLog/wiki/Targets for possible targets. See https://github.com/nlog/NLog/wiki/Layout-Renderers for the possible layout renderers. --> <!-- Write events to a file with the date in the filename. --> <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log" layout="${longdate} ${uppercase:${level}} ${message}" encoding="UTF-8"/> <target xsi:type="Console" name="c" layout="${longdate} ${uppercase:${level}} ${message}" encoding="UTF-8"/> </targets> <rules> <!-- add your logging rules here --> <!-- Write all events with minimal level of Debug (So Debug, Info, Warn, Error and Fatal, but not Trace) to "f" --> <logger name="*" minlevel="Debug" writeTo="f"/> <logger name="*" minlevel="Debug" writeTo="c"/> </rules> </nlog>
3, write static logging class LogUtil
1 public class LogUtils 2 { 3 private static readonly Logger log = LogManager.GetLogger("*"); 4 /// <summary> 5 /// 保存info级别的信息 6 /// </summary> 7 /// <param name="message"></param> 8 public static void infoWrite(string message) 9 { 10 log.Info(message); 11 } 12 /// <Summary> 13 is /// information stored info level 14 /// </ Summary> 15 /// <param name = "className"> Class Name </ param> 16 /// <param name = "methodName"> method name </ param> . 17 /// <param name = "content"> log contents </ param> 18 is public static void infoWrite ( String className, String methodName, String content) . 19 { 20 is String Message = " className:{" + className + "}, methodName:{" + methodName + "}, content:{" + content + "}"; 21 log.Info(message); 22 } 23 24 25 /// <summary> 26 /// 保存error级别信息 27 /// </summary> 28 /// <param name="error"></param> 29 public static void errorWrite(string error) 30 { 31 log.Error(error); 32 } 33 is 34 is /// <Summary> 35 /// Save error level information 36 /// </ Summary> 37 [ /// <param name = "className"> Class Name </ param> 38 is /// <param name = "methodName"> method name </ param> 39 /// <param name = "content"> log contents </ param> 40 public static void errorWrite ( String className, String methodName,string content) 41 { 42 string message = "className:{" + className + "}, methodName:{" + methodName + "}, content:{" + content + "}"; 43 log.Error(message); 44 } 45 46 /// <summary> 47 /// 保存debug级别信息 48 /// </summary> 49 /// <param name="message"></param> 50 public static void debugWrite(string message) 51 { 52 log.debug (Message); 53 is } 54 is 55 /// <Summary> 56 is /// Save debug level information 57 is /// </ Summary> 58 /// <param name = "className"> Class Name </ param > 59 /// <param name = "methodName"> method name </ param> 60 /// <param name = "content"> log contents </ param> 61 is public static void debugWrite ( String className, String methodName,string content) 62 { 63 is String Message = " className: { " + className + " }, methodName: { " + methodName + " }, Content: { " + Content + " } " ; 64 log.debug (Message); 65 } 66 67 // / <Summary> 68 /// log file before a deletion of 2 months 69 /// </ Summary> 70 public static void deleteLogFile ( String LogPath) 71 is { 72 if (!Directory.Exists(logPath)) 73 { 74 return; 75 } 76 DirectoryInfo folder = new DirectoryInfo(logPath); 77 FileInfo[] files = folder.GetFiles("*.log"); 78 if (files == null) 79 { 80 return; 81 } 82 83 foreach (FileInfo file in files) 84 { 85 //文件创建时间 86 DateTime fileCreateTime = file.LastWriteTime; 87 //当前时间 88 DateTime now = DateTime.Now; 89 int createMonth = fileCreateTime.Month; 90 int nowMonth = now.Month; 91 92 int distance = nowMonth - createMonth; 93 distance = distance >= 0 ? distance : (distance + 12); 94 95 if (distance < 3) 96 { 97 // less than three months without deleting 98 Continue ; 99 } 100 101 the try 102 { 103 File.Delete (file.FullName); 104 } 105 the catch 106 { 107 the throw new new Exception ( " delete the log files abnormal " ); 108 } 109 110 } 111 } 112 113 }