1、首先下载NLog框架,在vs NuGet中搜索NLog,下载安装NLog.Config
2、配置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"
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、写静态日志类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 /// 保存info级别的信息
14 /// </summary>
15 /// <param name="className">类名</param>
16 /// <param name="methodName">方法名</param>
17 /// <param name="content">日志内容</param>
18 public static void infoWrite(string className, string methodName, string content)
19 {
20 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
34 /// <summary>
35 /// 保存error级别信息
36 /// </summary>
37 /// <param name="className">类名</param>
38 /// <param name="methodName">方法名</param>
39 /// <param name="content">日志内容</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 }
54
55 /// <summary>
56 /// 保存debug级别信息
57 /// </summary>
58 /// <param name="className">类名</param>
59 /// <param name="methodName">方法名</param>
60 /// <param name="content">日志内容</param>
61 public static void debugWrite(string className, string methodName, string content)
62 {
63 string message = "className:{" + className + "}, methodName:{" + methodName + "}, content:{" + content + "}";
64 log.Debug(message);
65 }
66
67 /// <summary>
68 /// 删除2个月前的日志文件
69 /// </summary>
70 public static void deleteLogFile(string logPath)
71 {
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 //小于三个月不删除
98 continue;
99 }
100
101 try
102 {
103 File.Delete(file.FullName);
104 }
105 catch
106 {
107 throw new Exception("删除日志文件出现异常");
108 }
109
110 }
111 }
112
113 }