Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Log4net config file for mail appender,db appender,console appender and file appender
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
</configSections>
<log4net>
<appender name="ConsoleAppender" type="log4net.appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%logger%newline%method">
</conversionPattern>
</layout>
</appender>
<appender name="FileAppender" type="log4net.appender.FileAppender">
<file value="C:\MyLogs\MyLogFile.txt"/>
<appendToFile value="true"/>
<lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{ABSOLUTE} [%logger] %level - %message%newline%exception"/>
</layout>
</appender>
<appender name="RollingFileAppender" type="log4net.appender.RollingFileAppender">
<file value="C:\MyLogs\RollingFileLog.txt"/>
<appendToFile value="true"/>
<rollingStyle value="Size"/>
<maximumFileSize value="10MB"/>
<maxSizeRollBackups value="5"/>
<staticLogFileName value="true"/>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date{ABSOLUTE} [%logger] %level - %message%newline%exception"/>
</layout>
</appender>
<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
<BufferSize value="1" />
<connectionType value="MySql.Data.MySqlClient.MySqlConnection, MySql.Data, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
<connectionString value="Server=127.0.0.1;Database=logs;Uid=root;Pwd=bhhcybhhcy;" />
<commandText value="INSERT INTO errorlog (Date,Thread,Level,Logger,Message,Exception) VALUES(@log_date,@thread,@log_level,@logger,@message,@exception)" />
<commandType value="Text"/>
<!--<commandText value="dbo.procLog_Insert"/>
<commandType value ="StoredProcedure"/>-->
<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>
<parameter>
<parameterName value="@location" />
<dbType value="String" />
<size value="4000" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%location" />
</layout>
</parameter>
<parameter>
<parameterName value="@publisherId" />
<dbType value="String" />
<size value="50" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%aspnet-session{publisherId}" />
</layout>
</parameter>
</appender>
<appender name="SmtpAppender" type="log4net.Appender.SmtpExtendedAppender">
<authentication value="Basic" />
<password value="********"/>
<username value="offir.peer@gmail.com"/>
<from value="offir.peer@gmail.com" />
<to value="offir@appnext.com" />
<smtpHost value="smtp.gmail.com" />
<isBodyHtml value="true" />
<bufferSize value="10" />
<EnableSsl value="true"/>
<subject value="test logging message" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="$%date{ABSOLUTE} | %logger | %level - | %message%newline%exception%newline" />
</layout>
</appender>
<root>
<level value="DEBUG"></level>
<!--<appender-ref ref="ConsoleAppender"></appender-ref>-->
<!--<appender-ref ref="FileAppender"></appender-ref>-->
<!--<appender-ref ref="RollingFileAppender"></appender-ref>-->
<!--<appender-ref ref="AdoNetAppender"></appender-ref>-->
<appender-ref ref="SmtpAppender"></appender-ref>
</root>
</log4net>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
using System;
[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace Log4NetConsoleApplication
{
class Program
{
//private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private static readonly log4net.ILog log = log4net.LogManager.GetLogger("Amazon.S3.AmazonS3Client");
static void Main(string[] args)
{
Console.WriteLine("hello world");
log.Debug("This is my 1 error message");
log.Debug("This is my 2 error message");
log.Debug("This is my 3 error message");
log.Debug("This is my 4 error message");
log.Debug("This is my 5 error message");
var myfriend = "offir";
log.Debug(string.Format("Hello my friend {0}",myfriend));
log.Debug("This is my 7 error message");
log.Error("This is my 8 error message");
log.Error("This is my 9 error message");
log.Error("This is my 10 error message");
log.Error("This is my 10 error message");
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment