Home > WCF > How to use Log4Net with WCF in DOT NET

How to use Log4Net with WCF in DOT NET


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

1

Step 2.

Configure your Configuration File (Web.config) as below


 <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(&amp;amp;quot;ServiceBase Constructor calling&amp;amp;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(&amp;amp;quot;Entering GetValue()&amp;amp;quot;);

return string.Format(&amp;amp;quot;You entered: {0}&amp;amp;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

You can  download Source code from my GITHUB Repository from :Here

Advertisements
Categories: WCF Tags:
  1. aleem
    May 14, 2016 at 6:13 AM

    well written. nice one

  2. May 14, 2016 at 7:02 AM

    Thanks !!!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: