C#中使用log4net记录本地日志的方法和日志工具YALV使用_c# .net 4.0 本地日志-CSDN博客

来源: C#中使用log4net记录本地日志的方法和日志工具YALV使用_c# .net 4.0 本地日志-CSDN博客

使用log4net记录本地日志,大大方便程序调试,和异常分析

第一步,首先从Visual Studio中的Nuget包管理中搜索下载 Log4Net dll文件 如下图:

 

 

打开配置文件WinFrom或Wpf就是App.config

Web就是web.config,通过推荐配置可以使用日志工具进行日志查看,非常fashion

日志工具下载https://lukepet.github.io/YALV/

 

 

第一种配置

<?xml version=”1.0″?>
<configuration>
<startup>
<supportedRuntime version=”v4.0″ sku=”.NETFramework,Version=v4.0″/>
</startup>
<configSections>
<section name=”log4net” type=”log4net.Config.Log4NetConfigurationSectionHandler, log4net”/>
</configSections>

<log4net>
<!– 定义输出到文件中 –>
<appender name=”RollingLogFileAppender” type=”log4net.Appender.RollingFileAppender”>
<!–定义文件存放位置–>
<file value=”log\\”/>
<!–日志名称–>
<file value=”errorLog.txt”/>
<appendToFile value=”true”/>
<rollingStyle value=”Date”/>
<datePattern value=”yyyy\\yyyyMM\\yyyyMMdd’.txt'”/>
<staticLogFileName value=”false”/>

<!–日志最大可备份数–>
<maxSizeRollBackups value=”10″/>
<!–日志文件大小–>
<maximumFileSize value=”1024KB”/>
<layout type=”log4net.Layout.PatternLayout”>
<!–每条日志末尾的文字说明–>
<!–输出格式–>
<!–样例:2008-03-26 13:42:32,111 [10] INFO Log4NetDemo.MainClass [(null)] – info–>
<conversionPattern value=”%newline %n记录时间:%date %n线程ID:[%thread] %n日志级别: %-5level %n出错类:%logger property: [%property{NDC}] – %n错误描述:%message%newline %n”/>
</layout>
</appender>
<root>
<level value=”ERROR”/>
<!–文件形式记录日志–>
<appender-ref ref=”RollingLogFileAppender”/>
</root>
</log4net>
</configuration>

第二种配置

<log4net>
<logger name=”logerror”>
<level value=”ERROR” />
<appender-ref ref=”ErrorAppender” />
</logger>
<logger name=”loginfo”>
<level value=”INFO” />
<appender-ref ref=”InfoAppender” />
</logger>
<appender name=”ErrorAppender” type=”log4net.Appender.RollingFileAppender”>
<param name=”File” value=”Log\\LogError\\” />
<param name=”AppendToFile” value=”true” />
<param name=”MaxSizeRollBackups” value=”100″ />
<param name=”MaxFileSize” value=”10240″ />
<param name=”StaticLogFileName” value=”false” />
<param name=”DatePattern” value=”yyyyMMdd&quot;.txt&quot;” />
<param name=”RollingStyle” value=”Date” />
<layout type=”log4net.Layout.PatternLayout”>
<param name=”ConversionPattern” value=”%n异常时间:%d %n异常级别:%-5p%n异常内容:%m%n” />
</layout> <!–&lt; &gt; = <> %n = 回车–>
</appender>
<appender name=”InfoAppender” type=”log4net.Appender.RollingFileAppender”>
<param name=”File” value=”Log\\LogInfo\\” />
<param name=”AppendToFile” value=”true” />
<param name=”MaxFileSize” value=”10240″ />
<param name=”MaxSizeRollBackups” value=”100″ />
<param name=”StaticLogFileName” value=”false” />
<param name=”DatePattern” value=”yyyyMMdd&quot;.txt&quot;” />
<param name=”RollingStyle” value=”Date” />
<layout type=”log4net.Layout.PatternLayout”>
<param name=”ConversionPattern” value=”日志时间:%d %n日志级别:%-5p %n日志内容:%m%n%n” />
</layout>
</appender>
</log4net>

第三种配置

<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 version=”1.0″ encoding=”utf-8″ ?>

<configuration>
<configSections>
<section name=”log4net” type=”log4net.Config.Log4NetConfigurationSectionHandler, log4net”/>
</configSections>

<log4net>

<!–定义输出到文件中–>
<appender name=”RollingLogFileAppender” type=”log4net.Appender.RollingFileAppender”>
<!–定义文件存放位置–>
<file value=”log\\”/>
<appendToFile value=”true”/>
<rollingStyle value=”Date”/>
<datePattern value=”yyyy\\yyyyMM\\yyyyMMdd-HH’.xml'”/>
<staticLogFileName value=”false”/>
<param name=”MaxSizeRollBackups” value=”100″/>
<Encoding value=”UTF-8″/>
<layout type=”log4net.Layout.XmlLayoutSchemaLog4j”>
<locationInfo value=”true”/>
</layout>
</appender>
<root>
<level value=”Debug”/>
<!–文件形式记录日志–>
<appender-ref ref=”RollingLogFileAppender”/>
</root>

</log4net>

</configuration>

XML要放在configuration节点下

使用方法

private ILog _log = LogManager.GetLogger(“”);
一般常用以下几个方法:
_log.Error();
_log.Debug();
_log.Info();

————————————————

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原文链接:https://blog.csdn.net/g313105910/article/details/115796506

赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏