Click here to Skip to main content
14,668,361 members
Articles » Development Lifecycle » Design and Architecture » Application Design
Posted 5 May 2017


12 bookmarked

Application Design - A Bad Day With Adapter Pattern and Log4Net

Rate this:
4.71 (11 votes)
Please Sign up or sign in to vote.
4.71 (11 votes)
6 May 2017CPOL
This topic will cover the concept of the adapter pattern & how to implement logger using log4net. It will also cover what’s adapter pattern, why need, where & when to use; what’s Log4Net, implementation of logger using adapter pattern, how to implement and add custom SMTP appender & configuration.

Application Design

Readers Of This Topics - Application Architect, Developers

What will you learn from the topics

If you already know about the adapter pattern then this is not for you. Anyway, I am guessing you have an initial knowledge about GOF design pattern. No? If no, don’t worry about it. Now, I will cover the following stuff –

Adapter -

  • What’s Adapter Pattern
  • Why we need
  • Where and when to use

Log4Net -

  • What’s Log4Net
  • Implementation of logger using adapter pattern
  • How to implement and add custom SMTP appender
  • Configuration of log4net.config

Let's Drilldown The Basic Concept

My Problem and Realization

I am working on a project and I need a third party library for my project. Why? Let’s explain, I’m implementing an online shopping cart for selling Beers. Customers will buy the products from my site. So, if anything happen during the payment stuff then I need to know the errors log. I mean anytime if something goes wrong to my application, then I need to know why that is happening; so that, I can figure out the problem quickly and if I know the reason of the problem, then it’s easy to fix the bug or error etc.

Therefore, I have heard that logger can fix my problem. So, I need a logger.

I am smart and I don’t have time to implement logger by my own. There are many third party logger. So, I am planning to use third party logger. Finally, I added one. But the problem is that if I say something to the third party logger, it don’t understand what I am saying. Even, I don’t have the control to modify their source code.

Image 1

So, I need someone on the middle to communicate with me and the third party logger.

Why Need Adapter Pattern

I am asking you- why not adapter pattern? Can you able to solve my problem without that middle-man. If you can, you are smart. But I’m not, I am lazy to implement by my own.

So, if I need somebody on the middle to communicate with my functionality and 3rd party library where I have no control to change it, then definitely I will use adapter pattern.  I don’t know what you will do for your problem; it’s your headache. 

What’s Adapter Pattern

It acts like a communication helper bridge between two incompatible interfaces.

It is just like a wrapper. Wait a minute! Wrapper?? I hear - Façade is also a wrapper then what’s the difference?

Façade VS. Adapter Pattern

Façade hides the complexity of the subsystem. So, it wraps the multiple objects where Adapter wraps a single object.

More …

Wait a minute! I have another question what’s the difference between Bridge and Adapter Pattern or Decorator and Adapter?

You have asked two questions. Now it’s your turn to think …

Logger Tools

  • Log4Net
  • NLog
  • Enterprise Library
  • Logger.NET, etc.

What’s Log4Net

It is a good logging library and most widely used. You can log your error related messages to the database or into the file or send error log via email. It is configurable and easy to use.  

Level: It has 5 level that you can call from your code. The level are –

  • Warn
  • Debug
  • Error
  • Fatal
  • Info.

Level Configuration:

Image 2

Appender: In my example, I will explain about 3 appender -

  • CustomSmtpAppender
  • RollingLogFileAppender
  • EventLogAppender

Image 3

Log4Net VS. NLog      

  • Asynchronous appender is difficult for Log4Net; even, it does not work well with threading and task.
  • NLog is good comparing to these kind of problems.

What're We Going To Do

Now I don't need to compare which logger is best. I've aready decided that I will useLog4Net to my application. Therefore, I’m going to implement a logger using Log4Net.

From the designing point of view, I am considering adapter pattern.

Image 4

Note that- in this diagram, Logger class is tightly coupled with Log4Net. Because, I’m not thinking about the future. But in future, if you want to change from Log4Net to NLog or something then you can make it loosely coupled in your implementation. Think!!

Image 5

Adapter Pattern

Image 6

  1. Target: The interface that the client wants to use. According to our class diagram, it is ITargetAdapterForLogger interface.
  2. Adaptee: An implementation that needs adapting. Here, it is Log4Net.
  3. Adapter: The class that implements the Target interface in terms of the Adaptee. Here, it is Logger class.
  4. Request: An operation that the client need. Here, it is ClientDemo.

Implementing Logger Using Adapter Pattern

Creating Project For Logger

Creating the “Rony.Utility.LogMsg.AdapterPattern” project as shown below -

Image 7

Adding References Into Project

Now I'm going to add 3 references to the "Rony.Utility.LogMsg.AdapterPattern" project as shown below:

  • System.Deployment
  • System.Web
  • System.Windows.Forms

Image 8

I need these 3 references because I have reference dependency in my project.

How To Add Log4Net To Project

Step-1: Right click (from mouse) on the project > Click on “Manage NuGet Packages” option as shown below -

Image 9

Step-2: Type Log4Net in search box of the left side panel as shown below -

Image 10

Step-3: Click on log4net option and you will see the Log4Net install option; click on install as shown below -

Image 11

Step-4 - After successful installation, you will find that Log4Net reference has been added in the project references folder as shown below -

Image 12

Creating Required Class Files And Interface

ITargetAdapterForLogger Interface

Now creating target interface name "ITargetAdapterForLogger.cs" and declaring the signatures of the methods and properties in it.

Image 13

Logger Class

Creating the Logger.cs class to implement the functionalities from the ITargetAdapterForLogger interface.

Image 14

Showing Log4Net.Config File Information

I need this "log4net.config" file because the configuration parameters are given into this file. There are two options to let the project know that I have a log4net.config file.

First option: I can add the below line into AssemblyInfo.cs file.

[assembly: log4net.Config.XmlConfigurator(Watch = true, ConfigFile="log4net.config")]Image 15

Second option: I can add the below line into Logger.cs file.

Image 16

Remember that- don't apply both options; use only one option. Otherwise, you will get compilation error.

I’m choosing the second option; because, if I need to change the name of the config file, then I will forget that I have added config information into the AssemblyInfo.

In this Rony.Utility.LogMsg.AdapterPattern project, I have added a log4net.config file. But you don’t need to add it. I add this because, if I create the client project, then I will copy the config from there to here; otherwise, I will forget to add this file into the client project.

CustomSmtpAppender Model Class

"log4net.Appender.BufferingAppenderSkeleton" will be inherited into this class; I need this file because I want to send error related log files via email.

Image 17

Creating Project For Demo

Rony.Utility.LogMsg.AdapterPattern.Demo.Client is creating to see the logger result.

Image 18

Now I’m adding the "Rony.Utility.LogMsg.AdapterPattern.Demo.Client" as a reference to this project.

Adding log4net.config File

Step1: Add the config file into your project.

Step2: Select the log4net.config. Now go to the properties and select the "Copy If Newer" from the "Copy to Output Directory".

Image 19

In this config file, I have added 3 appenders.

  • CustomSmtpAppender
  • RollingLogFileAppender
  • EventLogAppender

Configuring RollingLogFileAppender Into Log4net.config

To Enable the RollingLogFileAppender find the <root> tag and enable or disable the level for debug or info etc. I have enabled all in the level as shown below -

Image 20

Add the file name into the appender <file value="Logs\RonyUtilityLog" /> as shown below -

Image 21

Creating Logger Object

In the "Program.cs" file I have created an instance of the Logger. I have called the logger as shown below -

Image 22

Demo Time!!

Now run the console application, then you will see as bellow -

Image 23

Finally, go to the location of the client project, like "…\ bin\Debug\Logs". You will find the log file as bellow -

Image 24

WOW!! I have done the implementation. I’m so happy.

Image 25

If you don’t need CustomSmtpAppender and EventLogAppender then this is end for you. I have added the project and source code; find the attachment.

Configuring CustomSmtpAppender Into Log4net.config

Step1: If you want to use CustomSmtpAppender then don't forget to add your Assembly name "Rony.Utility.LogMsg.AdapterPattern" and the namespace of the file name of the CustomSmtpAppender like bellow -"Rony.Utility.LogMsg.AdapterPattern.CustomSmtpAppender"

Image 26

Step2: Adding the email related information into the CustomSmtpAppender

  • Set to “true” to enable email sent for Error log, otherwise, set to false.

<enableEmailSent value="true"/>

  • For multiple Email Address to the recipients  use ';'

    <to value=";" />  

  • Also configure other required value for smtpHost, smtpPort, subject etc.

Image 27

Configuring the EventLogAppender Into Log4net.config

Two options are given bellow for ASP.NET configuration if you host into IIS:

  • Change the app pool: Run the application pool as LocalSystem
  • Or set an application name, add a registry key

Run the application pool as LocalSystem

Double click on the Application pools> ASP.NET v4.0 Classic>

go to the right for the Action panel>Addvance Application Pool>Advance Settings>Process Model>Identity> Change ApplicationPoolIdentity to LocalSystem

Image 28

Set an application name, add a registry key

Step1: Click on Start and

Step2: in the search box, or Run window, type the following:

regedit and then press Enter.

Image 29

Step3: Go to


Step -4: Create a registry key name YourAppSystem and it would be


Step5: Create a string value inside this Name it EventMessageFile and set its value to


Note that YourAppSystem is the ApplicationName into the EventLogAppender.


I like to utilize the pattern and principles into my projects. If I have knowledge, but I can’t utilize it to my projects, then I don’t need that useless knowledge.

What about you ... ??



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Habibur Rony
United States United States
Lazy software engineer, don’t believe in hard work.

Comments and Discussions

BugLog4net is throwing an exception in your Demo Pin
George Swan7-May-17 7:37
MemberGeorge Swan7-May-17 7:37 
GeneralRe: Log4net is throwing an exception in your Demo Pin
Habibur Rony7-May-17 8:38
MemberHabibur Rony7-May-17 8:38 
GeneralStarts good, but then... Pin
Klaus Luedenscheidt6-May-17 18:35
MemberKlaus Luedenscheidt6-May-17 18:35 
GeneralRe: Starts good, but then... Pin
Habibur Rony7-May-17 2:14
MemberHabibur Rony7-May-17 2:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.