log4net的四方常用方式
新年第一篇文章,希望大家喜欢。
四种常用方式:
1.控制台
2.文件
3.数据库
4.remoting
<log4net>
<root>
<level value="ALL" />
<appender-ref ref="consoleApp" />
<appender-ref ref="RemotingAppender" />
</root>
<appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="Applog.txt" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="10" />
<param name="MaximumFileSize" value="5MB" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy.MM.dd" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
<param name="Header" value=" ----------------------header-------------------------- " />
<param name="Footer" value=" ----------------------footer-------------------------- " />
</layout>
</appender>
<appender name="AdoNetAppender_Oracle" type="log4net.Appender.AdoNetAppender">
<connectionType value="System.Data.OracleClient.OracleConnection, System.Data.OracleClient, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="data source=RVGIS; User ID=topman;Password=topman" />
<commandText value="INSERT INTO S_LOG (Datetime,Thread,Log_Level,Logger,Message) VALUES (:log_date, :thread, :log_level, :logger, :message)" />
<!--在实际的运行中 buff值可以大一些-->
<bufferSize value="2" />
<parameter>
<parameterName value=":log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value=":thread" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%thread" />
</layout>
</parameter>
<parameter>
<parameterName value=":log_level" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
<parameter>
<parameterName value=":logger" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<parameter>
<parameterName value=":message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
</appender>
<appender name="consoleApp" type="log4net.Appender.ColoredConsoleAppender">
<mapping>
<level value="ERROR" />
<foreColor value="White" />
<backColor value="Red, HighIntensity" />
</mapping>
<mapping>
<level value="DEBUG" />
<backColor value="Green" />
</mapping>
<mapping>
<level value="WARN" />
<backColor value="Yellow" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<appender name="RemotingAppender" type="log4net.Appender.RemotingAppender" >
<sink value="tcp://localhost:8085/ServerObject" />
<lossy value="true" />
<bufferSize value="2" />
<onlyFixPartialEventData value="true" />
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="WARN"/>
</evaluator>
</appender>
<logger name="Log4Net.Log">
<level value= "DEBUG" />
<appender-ref ref="AdoNetAppender_Oracle" />
<appender-ref ref="rollingFile" />
<appender-ref ref="consoleApp" />
</logger>
</log4net>
<root>
<level value="ALL" />
<appender-ref ref="consoleApp" />
<appender-ref ref="RemotingAppender" />
</root>
<appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net" >
<param name="File" value="Applog.txt" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="10" />
<param name="MaximumFileSize" value="5MB" />
<param name="RollingStyle" value="Date" />
<param name="DatePattern" value="yyyy.MM.dd" />
<param name="StaticLogFileName" value="true" />
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n" />
<param name="Header" value=" ----------------------header-------------------------- " />
<param name="Footer" value=" ----------------------footer-------------------------- " />
</layout>
</appender>
<appender name="AdoNetAppender_Oracle" type="log4net.Appender.AdoNetAppender">
<connectionType value="System.Data.OracleClient.OracleConnection, System.Data.OracleClient, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="data source=RVGIS; User ID=topman;Password=topman" />
<commandText value="INSERT INTO S_LOG (Datetime,Thread,Log_Level,Logger,Message) VALUES (:log_date, :thread, :log_level, :logger, :message)" />
<!--在实际的运行中 buff值可以大一些-->
<bufferSize value="2" />
<parameter>
<parameterName value=":log_date" />
<dbType value="DateTime" />
<layout type="log4net.Layout.RawTimeStampLayout" />
</parameter>
<parameter>
<parameterName value=":thread" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%thread" />
</layout>
</parameter>
<parameter>
<parameterName value=":log_level" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%level" />
</layout>
</parameter>
<parameter>
<parameterName value=":logger" />
<dbType value="String" />
<size value="255" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger" />
</layout>
</parameter>
<parameter>
<parameterName value=":message" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%message" />
</layout>
</parameter>
</appender>
<appender name="consoleApp" type="log4net.Appender.ColoredConsoleAppender">
<mapping>
<level value="ERROR" />
<foreColor value="White" />
<backColor value="Red, HighIntensity" />
</mapping>
<mapping>
<level value="DEBUG" />
<backColor value="Green" />
</mapping>
<mapping>
<level value="WARN" />
<backColor value="Yellow" />
</mapping>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
</appender>
<appender name="RemotingAppender" type="log4net.Appender.RemotingAppender" >
<sink value="tcp://localhost:8085/ServerObject" />
<lossy value="true" />
<bufferSize value="2" />
<onlyFixPartialEventData value="true" />
<evaluator type="log4net.Core.LevelEvaluator">
<threshold value="WARN"/>
</evaluator>
</appender>
<logger name="Log4Net.Log">
<level value= "DEBUG" />
<appender-ref ref="AdoNetAppender_Oracle" />
<appender-ref ref="rollingFile" />
<appender-ref ref="consoleApp" />
</logger>
</log4net>
其中remoting的方式比较麻烦,在服务端还必须配置:
<!-- Register a section handler for the log4net section-->
<configSections >
<section name ="log4net" type ="System.Configuration.IgnoreSectionHandler" />
</configSections>
<!-- This section contains the log4net configuration settings -->
<log4net >
<appender name ="ConsoleAppender" type ="log4net.Appender.ConsoleAppender">
<layout type ="log4net.Layout.PatternLayout">
<conversionPattern value ="%date [%thread] %-5levele%logger (%property{log4net:HostName}) [%ndc] - %message%newline" />
</layout>
</appender>
<appender name ="LogFileAppender" type ="log4net.Appender.FileAppender">
<file value ="log-file.txt" />
<sppendToFile value ="true" />
<layout type ="log4net.Layout.PatternLayout">
<header value ="FILE HEADER " />
<footer value ="FILE FOOTER " />
<conversionPattern value ="%date [%thread] %-5levele%logger (%property{log4net:HostName}) [%ndc] - %message%newline" />
</layout>
</appender>
<root>
<level value ="DEBUG" />
<appender-ref ref ="LogFileAppender" />
</root>
</log4net>
<configSections >
<section name ="log4net" type ="System.Configuration.IgnoreSectionHandler" />
</configSections>
<!-- This section contains the log4net configuration settings -->
<log4net >
<appender name ="ConsoleAppender" type ="log4net.Appender.ConsoleAppender">
<layout type ="log4net.Layout.PatternLayout">
<conversionPattern value ="%date [%thread] %-5levele%logger (%property{log4net:HostName}) [%ndc] - %message%newline" />
</layout>
</appender>
<appender name ="LogFileAppender" type ="log4net.Appender.FileAppender">
<file value ="log-file.txt" />
<sppendToFile value ="true" />
<layout type ="log4net.Layout.PatternLayout">
<header value ="FILE HEADER " />
<footer value ="FILE FOOTER " />
<conversionPattern value ="%date [%thread] %-5levele%logger (%property{log4net:HostName}) [%ndc] - %message%newline" />
</layout>
</appender>
<root>
<level value ="DEBUG" />
<appender-ref ref ="LogFileAppender" />
</root>
</log4net>
<remoting的方式有点小错误,希望知道的同志给点提示!!>