log4net" / /configSections log4netloggerlevel value="DEBUG"

时间:2022-06-16 08:15:02

在这里,记录我在项目中使用log4net记录本地日志的法式。在不会之前觉得很难,很神秘,,一旦会了之后其实没那么难。其实所有的工作都是一样的,下面我就分享一下我使用log4Net的经验。

第一步:首先从Visual Studio中的Nuget承打点中搜索下载 Log4Net dll文件 如下图:

log4net" / /configSections log4netloggerlevel value="DEBUG"

log4net" / /configSections log4netloggerlevel value="DEBUG"

选择安置的项目(哪个类库中需要记录日志就勾选上)

第二步:打开配置文件 WinFrom就是 App.config  Web就是 web.config 将以下配置信息插手

<configSections> <section type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" /> </configSections> <log4net> <logger> <level value="DEBUG" /> <appender-ref ref="SysRFAppender" /> </logger> <logger> <level value="DEBUG" /> <appender-ref ref="DebugAppender" /> </logger> <logger> <level value="DEBUG" /> <appender-ref ref="MsgAppender" /> </logger> <logger> <level value="DEBUG" /> <appender-ref ref="OperInfoAppender" /> </logger> <appender type="log4net.Appender.RollingFileAppender"> <file value="Logs\syslog.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 type="log4net.Appender.RollingFileAppender"> <file value="Logs\sysDebuglog.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 type="log4net.Appender.RollingFileAppender"> <file value="Logs\Messagelog.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 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 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 value="%property{User}" /> </layout> </parameter> <parameter> <parameterName value="@flag" /> <dbType value="Int32" /> <layout type="NT.MultithreadingTaskService.Log.Layout.UserOperLayOut,NT.MultithreadingTaskService.Log"> <param 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 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 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 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 value="%property{Message}" /> </layout> </parameter> </appender> <root> <level value="DEBUG" /> <appender-ref ref="SysRFAppender" /> </root> </log4net>

以上节点具体参数这里不做解释,可以上网盘问辅佐文档

注意:这两段XML要放在configuration 节点下靠前