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 –
- What’s Adapter Pattern
- Why we need
- Where and when to use
- 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.
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.
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 …
- Enterprise Library
- Logger.NET, etc.
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 –
Appender: In my example, I will explain about 3 appender -
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.
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!!
- Target: The interface that the client wants to use. According to our class diagram, it is ITargetAdapterForLogger interface.
- Adaptee: An implementation that needs adapting. Here, it is Log4Net.
- Adapter: The class that implements the Target interface in terms of the Adaptee. Here, it is Logger class.
- 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 -
Adding References Into Project
Now I'm going to add 3 references to the "
Rony.Utility.LogMsg.AdapterPattern" project as shown below:
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 -
Log4Net in search box of the left side panel as shown below -
Step-3: Click on
log4net option and you will see the Log4Net
install option; click on
install as shown below -
Step-4 - After successful installation, you will find that
Log4Net reference has been added in the project references folder as shown below -
Creating Required Class Files And Interface
Now creating target interface name "
ITargetAdapterForLogger.cs" and declaring the signatures of the methods and properties in it.
Logger.cs class to implement the functionalities from the
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
First option: I can add the below line into
[assembly: log4net.Config.XmlConfigurator(Watch = true, ConfigFile="log4net.config")]
Second option: I can add the below line into
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.
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.
Creating Project For Demo
Rony.Utility.LogMsg.AdapterPattern.Demo.Client is creating to see the logger result.
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".
In this config file, I have added 3 appenders.
Configuring RollingLogFileAppender Into Log4net.config
To Enable the
RollingLogFileAppender find the
<root> tag and
disable the level for debug or info etc. I have enabled all in the level as shown below -
Add the file name into the appender
<file value="Logs\RonyUtilityLog" /> as shown below -
Creating Logger Object
In the "
Program.cs" file I have created an instance of the Logger. I have called the
logger as shown below -
Now run the console application, then you will see as bellow -
Finally, go to the location of the client project, like "
…\ bin\Debug\Logs". You will find the log file as bellow -
WOW!! I have done the implementation. I’m so happy.
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 -"
Step2: Adding the email related information into the
- Set to “
true” to enable email sent for Error log, otherwise, set to
- For multiple Email Address to the recipients use ';'
<to value="firstname.lastname@example.org; email@example.com" />
- Also configure other required value for smtpHost, smtpPort, subject etc.
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
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.
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 ... ??