C# NLog日志框架
标签:ace XML fat private catch mini eve tput for
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">
variable name="myvar" value="myvalue"/>
targets>
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>
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 ///
5 /// 保存info级别的信息
6 ///
7 ///
8 public static void infoWrite(string message)
9 {
10 log.Info(message);
11 }
12 ///
13 /// 保存info级别的信息
14 ///
15 /// 类名
16 /// 方法名
17 /// 日志内容
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 ///
26 /// 保存error级别信息
27 ///
28 ///
29 public static void errorWrite(string error)
30 {
31 log.Error(error);
32 }
33
34 ///
35 /// 保存error级别信息
36 ///
37 /// 类名
38 /// 方法名
39 /// 日志内容
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 ///
47 /// 保存debug级别信息
48 ///
49 ///
50 public static void debugWrite(string message)
51 {
52 log.Debug(message);
53 }
54
55 ///
56 /// 保存debug级别信息
57 ///
58 /// 类名
59 /// 方法名
60 /// 日志内容
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 ///
68 /// 删除2个月前的日志文件
69 ///
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 }
C# NLog日志框架
标签:ace XML fat private catch mini eve tput for
原文地址:https://www.cnblogs.com/BKYZFSN/p/11870655.html
评论