log4net日志组件
一、什么是log4net组件
Log4net是基于.net开发的一款非常著名的记录日志开源组件。他最早是2001年7月由NeoWorks Limited启动的项目,基本的框架源于另外的一个非常著名的姐妹组件-log4j。Log4net记录日志的功能非常强大。它可以将日志分不同的等级,比不同的样式,将日志输出到不同的媒介(数据库、文本、控制台、邮件……)。
二、log4net的核心内容
Log4net 主要分为5个核心主键: Logger,Appender,Filter,Layout,Object Render
Logger:
主要用于记录日志的分类和控制日志的级别。它可以以多种格式输出日志信息,同时它也可以控制日志的输出级别。
Log4net 分为如下几个级别:
FATAL 毁灭级别
ERROR 错误级别
WARN 警告级别
INFO 消息级别
DEBUG 调试级别
这几种日志级别 FATAL> ERROR> WARN> INFO>DEBUG
只有日志输出级别大于或等于配置的级别才能输出日志信息
比如我的日志配置级别为INFO
那么只有log.Info() log.Warn()log.Error() log.Fatal()才能输出日志信息,Debug方式就不能输出。
Log4net中还有两种特殊的配置级别: ALL 允许所有的日志级别输出 OFF 拒绝所有的日志级别输出
Appender:
这里我们简单称之为 媒体。意思就是说日志的输出必须依赖于它,更确切的说以文本形式输出,还是控制台格式输出都必须要依靠它来控制。Log4net定义了多种媒体:
(1) AdoNetAppender 将日志记录到数据库中。可以采用SQL和存储过程两种方式
(2) AnsiColorTerminalAppender 在ANSI 窗口终端写下高亮度的日志事件
(3) AspNetTraceAppender 能用asp.net中Trace的方式查看记录的日志
(4) BufferingForwardingAppender 在输出到子Appenders之前先缓存日志事件
(5) ConsoleAppender 将日志输出到控制台
(6) EventLogAppender 将日志写到Windows Event Log
(7) FileAppender 将日志写到文件中
(8) MemoryAppender 将日志存到内存缓冲区
(9) NetSendAppender 将日志输出到Windows Messenger service.这些日志信息将在用户终端的对话框中显示
(10) RemoteSyslogAppender 通过UDP网络协议将日志写到Remote syslog service
(11) RemotingAppender 通过.NET Remoting将日志写到远程接收端
(12) RollingFileAppender 将日志以回滚文件的形式写到文件中
(13) SmtpAppender 将日志写到邮件中
(14) TraceAppender 将日志写到.NET trace 系统
(15) UdpAppender 将日志connectionless UDP datagrams的形式送到远程宿主或以UdpClient的形式广播
Filter:
Appender 是将日志以缺省值的方式输出,意思就是按照默认值的方式输出。而Filter 意思就是过滤器,使用Filter 可以将日志以不同格式来输出
Layout:
这个用于控制日志输出显示格式
Object Render:
用于按照用户自定义标准来输出日志
三、代码演示跟着来
0.在引用中添加log4net.dll
1.新建一个配置文件(这里可以配置日志写入到sql 、 txt 、 邮件,以及设置日志级别等)
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<log4net debug="false">
<!--按日期分割日志文件 一天一个-->
<appender name="LogFileAppenderByDate" type="log4net.Appender.RollingFileAppender" >
<!--是否续写-->
<param name="AppendToFile" value="true" />
<!--最小锁定模型以允许多个进程可以写入同一个文件-->
<param name="LockingModel" value="log4net.Appender.FileAppender.MinimalLock" />
<param name="StaticLogFileName" value="true" />
<!--保存路径-->
<param name="File" value="F:\Log\\" />
<param name="DatePattern" value="yyyy-MM-dd.LOG" />
<param name="StaticLogFileName" value="false" />
<param name="RollingStyle" value="Date" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="时间:%d %n级别:%level %n类名:%c%n文件:%F 第%L行%n日志内容:%m%n-----------------------------------------%n%n" />
</layout>
</appender>
<!--按日志容量分割日志文件 10KB一个-->
<appender name="LogFileAppenderBySize" type="log4net.Appender.RollingFileAppender" >
<!--是否续写-->
<param name="AppendToFile" value="true" />
<!--最小锁定模型以允许多个进程可以写入同一个文件-->
<param name="LockingModel" value="log4net.Appender.FileAppender.MinimalLock" />
<param name="StaticLogFileName" value="true" />
<!--按照文件的大小进行变换日志文件-->
<param name="RollingStyle" value="Size" />
<param name="File" value="LogData\\log.txt" />
<!--单个文件最大数量 好像只有在 按Size分割时有效-->
<param name="MaximumFileSize" value="10KB"/>
<!--保留的log文件数量 超过此数量后 自动删除之前的 好像只有在 按Size分割时有效-->
<param name="MaxSizeRollBackups" value="2" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="发生时间:%d %n事件级别:%level %n相关类名:%c%n程序文件:%F 第%L行%n日志内容:%m%n-----------------------------------------%n%n" />
</layout>
</appender>
<!--记录日志到数据库-->
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<bufferSize value="100" />
<connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<connectionString value="Data Source=LKYKJYXG-341C59\LKY;database = ITweb;Integrated security = true;" />
<commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
<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>
<parameter>
<parameterName value="@exception" />
<dbType value="String" />
<size value="2000" />
<layout type="log4net.Layout.ExceptionLayout" />
</parameter>
</appender>
<root>
<!--设置输出级别-->
<level value="INFO" />
<!--启用按日期分割-->
<appender-ref ref="LogFileAppenderByDate" />
<!--启用按容量分割-->
<!--<appender-ref ref="LogFileAppenderBySize" />-->
<!--启用保存到数据库-->
<appender-ref ref="AdoNetAppender" />
</root>
</log4net>
</configuration>
2.在Global.asax文件下写入代码
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Security; using System.Web.SessionState; using System.IO; namespace WebApplication1 { public class Global : System.Web.HttpApplication { void Application_Start(object sender, EventArgs e) { // 在应用程序启动时运行的代码 log4net.Config.XmlConfigurator.ConfigureAndWatch(new FileInfo(Server.MapPath("/log4nethh.config"))); } void Application_End(object sender, EventArgs e) { // 在应用程序关闭时运行的代码 } void Application_Error(object sender, EventArgs e) { // 在出现未处理的错误时运行的代码 } void Session_Start(object sender, EventArgs e) { // 在新会话启动时运行的代码 } void Session_End(object sender, EventArgs e) { // 在会话结束时运行的代码。 // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为 // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer // 或 SQLServer,则不会引发该事件。 } } }
3.写入LogHelper类
using System; using System.Collections.Generic; using System.Linq; using System.Web; using log4net; namespace WebApplication1 { /// <summary> /// 日志辅助类 /// </summary> public class LogHelper { private static ILog log; private static LogHelper logHelper = null; /// <summary> /// 初始化 /// </summary> /// <returns></returns> public static ILog GetInstance() { logHelper = new LogHelper(null); return log; } /// <summary> /// 初始化 /// </summary> /// <param name="configPath"></param> /// <returns></returns> public static ILog GetInstance(string configPath) { logHelper = new LogHelper(configPath); return log; } /// <summary> /// 构造函数 /// </summary> /// <param name="configPath"></param> private LogHelper(string configPath) { if (!string.IsNullOrEmpty(configPath)) { log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(configPath)); } else { log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); } } } }
4.使用Log4net的方法(下面的可以写为一个方法,哪里使用哪里调用。)
Exception exp = Server.GetLastError(); string strE = "\r\n堆栈:" + exp.StackTrace + "\r " + "Message:" + exp.Message + "\r 来源:" + exp.Source + "\r 来源:" + Request.Url; Response.Write(strE); // 在事件日志中记录异常信息 ILog log = LogHelper.GetInstance(); log.Warn(strE); Server.ClearError(); Server.Transfer("Error.aspx", false);
四、更详细的配置文件
<?xml version="1.0"?> <configuration> <configSections> <section name="log4net" type="log4net.Config.Log4NetCoigurationSectionHandler,log4net"/> </configSections> <log4net> <!--MS SQL Server--> <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender"> <bufferSize value="100" /> <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> <connectionString value="data source=[database server];initial catalog=[database name];integrated security=false;persist security info=True;User ID=[user];Password=[password]" /> <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" /> <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> <parameter> <parameterName value="@exception" /> <dbType value="String" /> <size value="2000" /> <layout type="log4net.Layout.ExceptionLayout" /> </parameter> </appender> <!--MS Access--> <appender name="AdoNetAppender_Access" type="log4net.Appender.AdoNetAppender"> <connectionString value="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\log\access.mdb;User Id=;Password=;" /> <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message]) VALUES (@log_date, @thread, @log_level, @logger, @message)" /> <parameter> <parameterName value="@log_date" /> <dbType value="String" /> <size value="255" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date" /> </layout> </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="1024" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%message" /> </layout> </parameter> </appender> <!--Oracle9i--> <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=[mydatabase];User ID=[user];Password=[password]" /> <commandText value="INSERT INTO Log (Datetime,Thread,Log_Level,Logger,Message) VALUES (:log_date, :thread, :log_level, :logger, :message)" /> <bufferSize value="128" /> <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> <!--IBM DB2--> <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender"> <bufferSize value="100" /> <connectionType value="IBM.Data.DB2.DB2Connection,IBM.Data.DB2, Version=8.1.2.1" /> <connectionString value="server=192.168.0.0;database=dbuser;user Id=username;password=password;persist security info=true" /> <commandText value="INSERT INTO myschema.Log (Date,Thread,Level,Logger,Message,Exception) VALUES (@log_date,@thread,@log_level,@logger,@message,@exception)" /> <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="500" /> <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> <parameter> <parameterName value="@exception" /> <dbType value="String" /> <size value="2000" /> <layout type="log4net.Layout.ExceptionLayout" /> </parameter> </appender> <!--SQLite--> <appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender"> <bufferSize value="100" /> <connectionType value="Finisar.SQLite.SQLiteConnection, SQLite.NET, Version=0.21.1869.3794, Culture=neutral, PublicKeyToken=c273bd375e695f9c" /> <connectionString value="Data Source=c:\\inetpub\\wwwroot\\logs\\log4net.db;Version=3;" /> <commandText value="INSERT INTO Log (Date, Level, Logger, Message) VALUES (@Date, @Level, @Logger, @Message)" /> <parameter> <parameterName value="@Date" /> <dbType value="DateTime" /> <layout type="log4net.Layout.RawTimeStampLayout" /> </parameter> <parameter> <parameterName value="@Level" /> <dbType value="String" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%level" /> </layout> </parameter> <parameter> <parameterName value="@Logger" /> <dbType value="String" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%logger" /> </layout> </parameter> <parameter> <parameterName value="@Message" /> <dbType value="String" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%message" /> </layout> </parameter> </appender> <!--AspNetTraceAppender--> <appender name="AspNetTraceAppender" type="log4net.Appender.AspNetTraceAppender" > <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <!--BufferingForwardingAppender--> <!--<appender name="BufferingForwardingAppender" type="log4net.Appender.BufferingForwardingAppender" > <bufferSize value="100"/> <appender-ref ref="ConsoleAppender" /> </appender>--> <appender name="BufferingForwardingAppender" type="log4net.Appender.BufferingForwardingAppender" > <bufferSize value="512" /> <lossy value="true" /> <evaluator type="log4net.Core.LevelEvaluator"> <threshold value="WARN"/> </evaluator> <appender-ref ref="ConsoleAppender" /> </appender> <!--ColoredConsoleAppender--> <!--<appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender"> <mapping> <level value="ERROR" /> <foreColor value="White" /> <backColor value="Red, HighIntensity" /> </mapping> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender>--> <appender name="ColoredConsoleAppender" type="log4net.Appender.ColoredConsoleAppender"> <mapping> <level value="ERROR" /> <foreColor value="White" /> <backColor value="Red, HighIntensity" /> </mapping> <mapping> <level value="DEBUG" /> <backColor value="Green" /> </mapping> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <!--ConsoleAppender--> <!--<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender>--> <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender"> <target value="Console.Error" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <!--EventLogAppender--> <!--<appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" > <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender>--> <appender name="EventLogAppender" type="log4net.Appender.EventLogAppender" > <applicationName value="MyApp" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <!--FileAppender--> <!--<appender name="FileAppender" type="log4net.Appender.FileAppender"> <file value="log-file.txt" /> <appendToFile value="true" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <appender name="FileAppender" type="log4net.Appender.FileAppender"> <file value="${TMP}\log-file.txt" /> <appendToFile value="true" /> <encoding value="unicodeFFFE" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender>--> <appender name="FileAppender" type="log4net.Appender.FileAppender"> <file value="${TMP}\log-file.txt" /> <appendToFile value="true" /> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <!--ForwardingAppender--> <appender name="ForwardingAppender" type="log4net.Appender.ForwardingAppender" > <threshold value="WARN"/> <appender-ref ref="ConsoleAppender" /> </appender> <!--MemoryAppender--> <appender name="MemoryAppender" type="log4net.Appender.MemoryAppender"> <onlyFixPartialEventData value="true" /> </appender> <!--NetSendAppender--> <appender name="MemoryAppender" type="log4net.Appender.MemoryAppender"> <onlyFixPartialEventData value="true" /> </appender> <!--NetSendAppender--> <appender name="NetSendAppender" type="log4net.Appender.NetSendAppender"> <threshold value="ERROR" /> <server value="SQUARE" /> <recipient value="nicko" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <!--OutputDebugStringAppender--> <appender name="OutputDebugStringAppender" type="log4net.Appender.OutputDebugStringAppender" > <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <!--RemotingAppender--> <!--<appender name="RemotingAppender" type="log4net.Appender.RemotingAppender" > <sink value="tcp://localhost:8085/LoggingSink" /> <lossy value="false" /> <bufferSize value="95" /> <onlyFixPartialEventData value="true" /> </appender>--> <appender name="RemotingAppender" type="log4net.Appender.RemotingAppender" > <sink value="tcp://localhost:8085/LoggingSink" /> <lossy value="true" /> <bufferSize value="200" /> <onlyFixPartialEventData value="true" /> <evaluator type="log4net.Core.LevelEvaluator"> <threshold value="ERROR"/> </evaluator> </appender> <!--RollingFileAppender--> <!--<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="log.txt" /> <appendToFile value="true" /> <rollingStyle value="Size" /> <maxSizeRollBackups value="10" /> <maximumFileSize value="100KB" /> <staticLogFileName value="true" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="logfile" /> <appendToFile value="true" /> <rollingStyle value="Date" /> <datePattern value="yyyyMMdd-HHmm" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="logfile" /> <appendToFile value="true" /> <rollingStyle value="Composite" /> <datePattern value="yyyyMMdd" /> <maxSizeRollBackups value="10" /> <maximumFileSize value="1MB" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender>--> <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="logfile.txt" /> <appendToFile value="false" /> <rollingStyle value="Size" /> <maxSizeRollBackups value="-1" /> <maximumFileSize value="50GB" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <!--SmtpAppender--> <!--<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender"> <to value="to@domain.com" /> <from value="from@domain.com" /> <subject value="test logging message" /> <smtpHost value="SMTPServer.domain.com" /> <bufferSize value="512" /> <lossy value="true" /> <evaluator type="log4net.Core.LevelEvaluator"> <threshold value="WARN"/> </evaluator> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" /> </layout> </appender> <appender name="SmtpAppender" type="log4net.Appender.SmtpAppender"> <to value="to@domain.com" /> <from value="from@domain.com" /> <subject value="test logging message" /> <smtpHost value="SMTPServer.domain.com" /> <bufferSize value="512" /> <lossy value="false" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" /> </layout> </appender>--> <appender name="SmtpAppender" type="log4net.Appender.SmtpAppender,log4net"> <to value="to@domain.com" /> <from value="from@domain.com" /> <subject value="test logging message" /> <smtpHost value="SMTPServer.domain.com" /> <bufferSize value="512" /> <lossy value="false" /> <evaluator type="log4net.Core.LevelEvaluator,log4net"> <threshold value="WARN" /> </evaluator> <layout type="log4net.Layout.PatternLayout,log4net"> <conversionPattern value="%property{log4net:HostName} :: %level :: %message %newlineLogger: %logger%newlineThread: %thread%newlineDate: %date%newlineNDC: %property{NDC}%newline%newline" /> </layout> </appender> <!--SmtpPickupDirAppender--> <appender name="SmtpPickupDirAppender" type="log4net.Appender.SmtpPickupDirAppender"> <to value="to@domain.com" /> <from value="from@domain.com" /> <subject value="test logging message" /> <pickupDir value="C:\SmtpPickup" /> <bufferSize value="512" /> <lossy value="true" /> <evaluator type="log4net.Core.LevelEvaluator"> <threshold value="WARN"/> </evaluator> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" /> </layout> </appender> <!--TraceAppender--> <appender name="TraceAppender" type="log4net.Appender.TraceAppender"> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> <!--UdpAppender--> <appender name="UdpAppender" type="log4net.Appender.UdpAppender"> <localPort value="8080" /> <remoteAddress value="224.0.0.1" /> <remotePort value="8080" /> <layout type="log4net.Layout.PatternLayout, log4net"> <conversionPattern value="%-5level %logger [%property{NDC}] - %message%newline" /> </layout> </appender> </log4net> </configuration>