How to use Log4Net with WCF in DOT NET

Datetime:2016-08-23 04:33:45         Topic: WCF          Share        Original >>
Here to See The Original Article!!!

This post will help you to configure LOG4NET with WCF Application , Log4Net is basically a logging engine which have many advanced feature to help you completely in your logging need of your application

So lets break the implementation in step basis.

Step 1.

Download the log4Net from any authorized source like : ( or ( )

after downloading reference your LOG4Net in your project as below

Step 2.

Configure your Configuration File (Web.config) as below


 <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler"/>

 <!-- RollingFile is set to be a File Appender -->
 <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
 <file value="log4netLogging.log" />
 <appendToFile value="true" />
 <maximumFileSize value="1MB" />
 <maxSizeRollBackups value="2" />
 <datePattern value="yyyyMMdd" />
 <layout type="log4net.Layout.PatternLayout">
 <conversionPattern value="%date [%thread] %-5level %-50logger %-20method - %message%newline%newline" />
 <!-- Set root logger level to DEBUG and its only appender to RollingFile -->
 <level value="DEBUG" />
 <appender-ref ref="RollingFile" />

Step 3.

Make a class that will act as base class for your other class wherever you would like to use the logging.

public class ServiceBase
private readonly ILog log;
public ServiceBase()
//Load log4net Configuration
//Get logger
log = LogManager.GetLogger(typeof(ServiceBase));
//Start logging
log.Debug(&quot;ServiceBase Constructor calling&quot;);


Step 4.

Now Implement above created base class to your service and use the log.

public class Service1 : ServiceBase, IService1
private readonly ILog log;
public Service1() : base()
//Get logger for Service1
log = LogManager.GetLogger(typeof(Service1));

public string GetData(int value)
log.Info(&quot;Entering GetValue()&quot;);

return string.Format(&quot;You entered: {0}&quot;, value);


Now you can run and go to your project , log file will be created there

If you face any issue then you can reach out me at : :

Like this:

Like Loading...


Categories: WCF WCF