How to use Log4Net with WCF in DOT NET

Datetime:2016-08-23 04:33:45          Topic: WCF           Share

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 : (https://www.nuget.org/packages/log4net/) or ( https://logging.apache.org/log4net/download_log4net.cgi )

after downloading reference your LOG4Net in your project as below

Step 2.

Configure your Configuration File (Web.config) as below

<configuration>

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

<log4net>
 <!-- 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" />
 </layout>
 </appender>
 <!-- Set root logger level to DEBUG and its only appender to RollingFile -->
 <root>
 <level value="DEBUG" />
 <appender-ref ref="RollingFile" />
 </root>
 
 </log4net>
</configuration>

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
XmlConfigurator.Configure();
//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 : :https://www.facebook.com/kumar20sujeet

Like this:

Like Loading...

Related

Categories: WCF WCF





About List