这篇文章主要介绍了C#中如何使用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="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 name="DebugAppender" 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 name="MsgAppender" 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 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)]
第四步:在需要记录日志的cs文件中初始化: private ILog _log = LogManager.GetLogger("TicketFace");
一般常用以下几个方法:
_log.Error();
_log.Debug();
_log.Info();
最后运行项目然后在以上配置文件中找到路径去文件夹里看看就生成了日志文件了。
注意:这里日志是以天为单位记录 如下:
感谢你能够认真阅读完这篇文章,希望小编分享的“C#中如何使用log4net记录本地日志”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。