C#,vb.net,MVC,Jquery,javascript,jscript,vbscript,html,vb,sharepoint,COM,WPF,WCF,Wwf,Asp,Asp.net,questions & answers,

Latest in Sports

Friday, September 5, 2014

Log4net - What's in it?

Log4net - What's in it?

 
It's been a trend for a while facing clients request for log4net, though we know we should ideally look at implementing things in-line with Microsoft stack by leveraging Enterprise Library, there have been heavy influencers from the established clients where they have really set a comfort zone with log4net and doesn't have to look apart.
 
It's not really hard to implement thou'.
 
The version of log4net is available within nugget(http://www.nuget.org/packages/log4net/as well as in the Apache-2.0 web link(https://logging.apache.org/log4net/download_log4net.cgi).
 
It's has the flexibility to have the below log sinks:
 
·         Event Viewer
·         File based log
·         Email
·         Database etc.
 
It also has the capability to capture the below log events:
 
·         Fatal
·         Error
·         Warn
·         Info
·         Debug etc.
 
Let us have a sneak preview on a quick implementation of logging in web application:
 
1.       Create a new web application
 
2.       Make a reference to log4net by manual referencing or through the nuggets

 Open web.config
 
         a.       Add a new section in configSections as below



b.      If you want to use file based logging add the log4net file appender as below

c.       Replace the conversionPattern with any specific pattern you may need, I have used a pattern as below:
 
<conversionPattern value="%newline -------------------------------------------------------------------------------------------------- %newline Logged under user account: %username %newline Date and Time: %date %newline Thread: [%thread] %newline Log level: %level %newline Logger: %logger %newline Message: %message %newline -------------------------------------------------------------------------------------------------- " />
 
to generate the output as below:
--------------------------------------------------------------------------------------------------
 Logged under user account: W8-User389-GDN
Date and Time: 2014-02-10 20:33:59
 Thread: [8]
 Log level: FATAL
 Logger: Log4NetWeb._Default
 Message: Hello reader, here is a test sample
 --------------------------------------------------------------------------------------------------
 
4.       Since it’s a web app - call the log4net component in the global.asax file


5.       Now to get the logging implemented on the page - follow the below steps, make it a generic component or call out on the required pages.


 Your logging is now in place! Have fun.

No comments:

Post a Comment