在这里,记录我在项目中使用log4net记录本地日志的步骤。在不会之前感觉很难,很神秘,一旦会了之后其实没那么难。其实所有的事情都是一样的,下面我就分享一下我使用log4Net的经验。
第一步:首先从Visual Studio中的Nuget包管理中搜索下载 Log4Net dll文件 如下图:
选择安装的项目(哪个类库中需要记录日志就勾选上)
第二步:打开配置文件 WinFrom就是 App.config Web就是 web.config 将以下配置信息加入
<configSections> <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <log4net> <logger name="SysRFLogger"> <level value="DEBUG" /> <appender-ref ref="SysRFAppender" /> </logger> <logger name="DebugRFLogger"> <level value="DEBUG" /> <appender-ref ref="DebugAppender" /> </logger> <logger name="MsgLogger"> <level value="DEBUG" /> <appender-ref ref="MsgAppender" /> </logger> <logger name="OperInfoLogger"> <level value="DEBUG" /> <appender-ref ref="OperInfoAppender" /> </logger> <appender name="SysRFAppender" type="log4net.Appender.RollingFileAppender"> <file value="Logssyslog.log" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <preserveLogFileNameExtension value="true" /> <datePattern value="yyyyMMdd" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <appender name="DebugAppender" type="log4net.Appender.RollingFileAppender"> <file value="LogssysDebuglog.log" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <preserveLogFileNameExtension value="true" /> <datePattern value="yyyyMMdd" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <appender name="MsgAppender" type="log4net.Appender.RollingFileAppender"> <file value="LogsMessagelog.log" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <preserveLogFileNameExtension value="true" /> <datePattern value="yyyyMMdd" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <appender name="OperInfoAppender" type="log4net.Appender.AdoNetAppender"> <bufferSize value="1" /> <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=4.0.30319.18020, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <commandText value="INSERT INTO LOG_USEROPERLOG ([ID],[Date],[User],[Flag],[OperInfo],[OperFlag],[OperResult],[Message]) VALUES (@Id,@log_date, @user, @flag, @operinfo,@operflag, @operresult, @message)" /> <parameter> <parameterName value="@Id" /> <dbType value="String" /> <size value="36" /> <layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log"> <param name="ConversionPattern" value="%property{ID}" /> </layout> </parameter> <parameter> <parameterName value="@log_date" /> <dbType value="DateTime" /> <layout type="log4net.Layout.RawTimeStampLayout" /> </parameter> <parameter> <parameterName value="@user" /> <dbType value="String" /> <size value="64" /> <layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log"> <param name="ConversionPattern" value="%property{User}" /> </layout> </parameter> <parameter> <parameterName value="@flag" /> <dbType value="Int32" /> <layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log"> <param name="ConversionPattern" value="%property{Flag}" /> </layout> </parameter> <parameter> <parameterName value="@operinfo" /> <dbType value="String" /> <size value="255" /> <layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log"> <param name="ConversionPattern" value="%property{OperInfo}" /> </layout> </parameter> <parameter> <parameterName value="@operflag" /> <dbType value="String" /> <size value="32" /> <layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log"> <param name="ConversionPattern" value="%property{OperFlag}" /> </layout> </parameter> <parameter> <parameterName value="@operresult" /> <dbType value="String" /> <size value="255" /> <layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log"> <param name="ConversionPattern" value="%property{OperResult}" /> </layout> </parameter> <parameter> <parameterName value="@message" /> <dbType value="String" /> <size value="4000" /> <layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log"> <param name="ConversionPattern" value="%property{Message}" /> </layout> </parameter> </appender> <root> <level value="DEBUG" /> <appender-ref ref="SysRFAppender" /> </root> </log4net>
以上节点具体参数这里不做解释,可以上网查询帮助文档
注意:这两段XML要放在configuration 节点下靠前
第三步:打开项目AssemblyInfo.cs文件(UI层)
加入:[assembly: log4net.Config.XmlConfigurator(ConfigFile = "web.config", Watch = true)]
注意:ConfigFile属性值是当前程序配置文件名WinForm就是App.config Web程序就是:web.config
第四步:在需要记录日志的cs文件中初始化: private ILog _log = LogManager.GetLogger("TicketFace");
一般常用以下几个方法:
_log.Error();
_log.Debug();
_log.Info();
最后运行项目然后在以上配置文件中找到路径去文件夹里看看就生成了日志文件了。
注意:这里日志是以天为单位记录 如下:
WPF中有点小区别:
第三步:打开项目AssemblyInfo.cs文件(UI层)这里的配置注释不要。
在 App.xaml.cs中的的构造函数加入:log4net.Config.XmlConfigurator.Configure();
/// <summary> /// App.xaml 的交互逻辑 /// </summary> public partial class App : Application { public App() { log4net.Config.XmlConfigurator.Configure(); } }