在这里,记录我在项目中使用log4net记录本地日志的步骤。在不会之前感觉很难,很神秘,一旦会了之后其实没那么难。其实所有的事情都是一样的,下面我就分享一下我使用log4Net的经验。
第一步:首先从Visual Studio中的Nuget包管理中搜索下载 Log4Net dll文件 如下图:
选择安装的项目(哪个类库中需要记录日志就勾选上)
第二步:打开配置文件 WinFrom就是 App.config Web就是 web.config 将以下配置信息加入
1 <configSections>
2 <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
3 </configSections>
4 <log4net>
5 <logger name="SysRFLogger">
6 <level value="DEBUG" />
7 <appender-ref ref="SysRFAppender" />
8 </logger>
9 <logger name="DebugRFLogger">
10 <level value="DEBUG" />
11 <appender-ref ref="DebugAppender" />
12 </logger>
13 <logger name="MsgLogger">
14 <level value="DEBUG" />
15 <appender-ref ref="MsgAppender" />
16 </logger>
17 <logger name="OperInfoLogger">
18 <level value="DEBUG" />
19 <appender-ref ref="OperInfoAppender" />
20 </logger>
21 <appender name="SysRFAppender" type="log4net.Appender.RollingFileAppender">
22 <file value="Logs\syslog.log" />
23 <appendToFile value="true" />
24 <rollingStyle value="Date" />
25 <preserveLogFileNameExtension value="true" />
26 <datePattern value="yyyyMMdd" />
27 <layout type="log4net.Layout.PatternLayout">
28 <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
29 </layout>
30 </appender>
31 <appender name="DebugAppender" type="log4net.Appender.RollingFileAppender">
32 <file value="Logs\sysDebuglog.log" />
33 <appendToFile value="true" />
34 <rollingStyle value="Date" />
35 <preserveLogFileNameExtension value="true" />
36 <datePattern value="yyyyMMdd" />
37 <layout type="log4net.Layout.PatternLayout">
38 <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
39 </layout>
40 </appender>
41 <appender name="MsgAppender" type="log4net.Appender.RollingFileAppender">
42 <file value="Logs\Messagelog.log" />
43 <appendToFile value="true" />
44 <rollingStyle value="Date" />
45 <preserveLogFileNameExtension value="true" />
46 <datePattern value="yyyyMMdd" />
47 <layout type="log4net.Layout.PatternLayout">
48 <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
49 </layout>
50 </appender>
51 <appender name="OperInfoAppender" type="log4net.Appender.AdoNetAppender">
52 <bufferSize value="1" />
53 <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=4.0.30319.18020, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
54 <commandText value="INSERT INTO LOG_USEROPERLOG ([ID],[Date],[User],[Flag],[OperInfo],[OperFlag],[OperResult],[Message]) VALUES (@Id,@log_date, @user, @flag, @operinfo,@operflag, @operresult, @message)" />
55 <parameter>
56 <parameterName value="@Id" />
57 <dbType value="String" />
58 <size value="36" />
59 <layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log">
60 <param name="ConversionPattern" value="%property{ID}" />
61 </layout>
62 </parameter>
63 <parameter>
64 <parameterName value="@log_date" />
65 <dbType value="DateTime" />
66 <layout type="log4net.Layout.RawTimeStampLayout" />
67 </parameter>
68 <parameter>
69 <parameterName value="@user" />
70 <dbType value="String" />
71 <size value="64" />
72 <layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log">
73 <param name="ConversionPattern" value="%property{User}" />
74 </layout>
75 </parameter>
76 <parameter>
77 <parameterName value="@flag" />
78 <dbType value="Int32" />
79 <layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log">
80 <param name="ConversionPattern" value="%property{Flag}" />
81 </layout>
82 </parameter>
83 <parameter>
84 <parameterName value="@operinfo" />
85 <dbType value="String" />
86 <size value="255" />
87 <layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log">
88 <param name="ConversionPattern" value="%property{OperInfo}" />
89 </layout>
90 </parameter>
91 <parameter>
92 <parameterName value="@operflag" />
93 <dbType value="String" />
94 <size value="32" />
95 <layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log">
96 <param name="ConversionPattern" value="%property{OperFlag}" />
97 </layout>
98 </parameter>
99 <parameter>
100 <parameterName value="@operresult" />
101 <dbType value="String" />
102 <size value="255" />
103 <layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log">
104 <param name="ConversionPattern" value="%property{OperResult}" />
105 </layout>
106 </parameter>
107 <parameter>
108 <parameterName value="@message" />
109 <dbType value="String" />
110 <size value="4000" />
111 <layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log">
112 <param name="ConversionPattern" value="%property{Message}" />
113 </layout>
114 </parameter>
115 </appender>
116 <root>
117 <level value="DEBUG" />
118 <appender-ref ref="SysRFAppender" />
119 </root>
120 </log4net>
以上节点具体参数这里不做解释,可以上网查询帮助文档
注意:这两段XML要放在configuration 节点下靠前
第三步:打开项目AssemblyInfo.cs文件(UI层)
加入:[assembly: log4net.Config.XmlConfigurator(ConfigFile = "web.config", Watch = true)]
第四步:在需要记录日志的cs文件中初始化: private ILog _log = LogManager.GetLogger("TicketFace");
一般常用以下几个方法:
_log.Error();
_log.Debug();
_log.Info();
最后运行项目然后在以上配置文件中找到路径去文件夹里看看就生成了日志文件了。
注意:这里日志是以天为单位记录 如下: