<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>.toString() &#187; LOG4NET</title>
	<atom:link href="http://www.dottostring.com/tag/log4net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dottostring.com</link>
	<description>Programming is our passion</description>
	<lastBuildDate>Sun, 04 Oct 2009 15:57:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to use Log4net in ASP.NET Application</title>
		<link>http://www.dottostring.com/2008/11/5-how-to-use-log4net-logging-aspnet-application/</link>
		<comments>http://www.dottostring.com/2008/11/5-how-to-use-log4net-logging-aspnet-application/#comments</comments>
		<pubDate>Sun, 02 Nov 2008 09:44:05 +0000</pubDate>
		<dc:creator>babarjehangir</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[VB.NET]]></category>
		<category><![CDATA[C#.NET]]></category>
		<category><![CDATA[FILE BASED LOGGING]]></category>
		<category><![CDATA[LOG4NET]]></category>
		<category><![CDATA[LOGGING]]></category>

		<guid isPermaLink="false">http://www.dottostring.com/?p=5</guid>
		<description><![CDATA[Logging is an essential part of any application. Following is a simple step by step guide to integrate Log4net in your ASP.NET Web Application.
Create a new website

open Microsoft Visual Studio 2005 or 2008, then select File &#8211; &#62; New &#8211; &#62; Website
Enter your desired application path and name for the website.
Select your preferred language ( [...]]]></description>
			<content:encoded><![CDATA[<p>Logging is an essential part of any application. Following is a simple step by step guide to integrate <a title="Log4net" href="http://logging.apache.org/log4net/index.html" target="_blank">Log4net</a> in your ASP.NET Web Application.</p>
<h3><span style="font-weight: normal;">Create a new website</span></h3>
<ul>
<li>open Microsoft Visual Studio 2005 or 2008, then select File &#8211; &gt; New &#8211; &gt; Website</li>
<li>Enter your desired application path and name for the website.</li>
<li>Select your preferred language ( however, in this example we will use C# )</li>
</ul>
<div id="attachment_6" class="wp-caption aligncenter" style="width: 510px"><img class="size-full wp-image-6" title="Figure: New Website " src="http://www.dottostring.com/wp-content/uploads/2008/11/newweb.jpg" alt="An Illustration of Adding a New Website in Microsoft Visual Studio 2005 / 2008" width="500" height="330" /><p class="wp-caption-text">An Illustration of adding a new Website in Microsoft Visual Studio  2008</p></div>
<h3><span style="font-weight: normal;">Include Log4net in your website</span></h3>
<ul>
<li>Download <a title="Log4net" href="http://logging.apache.org/log4net/download.html" target="_blank">Log4net</a>.</li>
<li>extract the downloaded folder and look for &lt;log4net_folder&gt;/bin/net/2.0/Log4net.dll</li>
<li>Right-Click the project name and Add a Reference to Log4net.dll</li>
</ul>
<div id="attachment_8" class="wp-caption aligncenter" style="width: 510px"><img class="size-full wp-image-8" title="log4net" src="http://www.dottostring.com/wp-content/uploads/2008/11/log4net.jpg" alt="An Illustration of adding Log4net.dll to your website" width="500" height="371" /><p class="wp-caption-text">An Illustration of adding Log4net.dll to your website</p></div>
<h3><span style="font-weight: normal;">Log4net configuration</span></h3>
<ul>
<li>Add a config file to your project with the name log4net.config</li>
<li>Copy paste the following log4net configuration settings to log4net.config file.</li>
</ul>
<pre name="code" class="xml">
&lt;?xml version="1.0" encoding="utf-8" ?&gt;
&lt;log4net&gt;
	&lt;root&gt;
		&lt;level value="DEBUG" /&gt;
		&lt;appender-ref ref="RollingLogFileAppender"/&gt;
	&lt;/root&gt;
	&lt;appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender"&gt;
		&lt;file value="logsapp.log" /&gt;
		&lt;appendToFile value="true" /&gt;
		&lt;rollingStyle value="Size" /&gt;
		&lt;maxSizeRollBackups value="10" /&gt;
		&lt;maximumFileSize value="100KB" /&gt;
		&lt;staticLogFileName value="true" /&gt;
		&lt;layout type="log4net.Layout.PatternLayout"&gt;
			&lt;conversionPattern value="%d [%t]%-5p %c [%x] - %m%n" /&gt;
		&lt;/layout&gt;
	&lt;/appender&gt;
	&lt;appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender"&gt;
		&lt;file value="logsapp.log" /&gt;
		&lt;appendToFile value="false" /&gt;
		&lt;datePattern value="-dddd" /&gt;
		&lt;rollingStyle value="Date" /&gt;
		&lt;layout type="log4net.Layout.PatternLayout"&gt;
			&lt;conversionPattern value="%d [%t]%-5p %c [%x] - %m%n" /&gt;
		&lt;/layout&gt;
	&lt;/appender&gt;
&lt;/log4net&gt;
</pre>
<ul>
<li>Create a folder <strong>logs </strong>in your project directory</li>
</ul>
<div id="attachment_13" class="wp-caption aligncenter" style="width: 510px"><img class="size-full wp-image-13" title="log4netconfig" src="http://www.dottostring.com/wp-content/uploads/2008/11/log4netconfig.jpg" alt="An Illustration of log4net.config file" width="500" height="412" /><p class="wp-caption-text">An Illustration of log4net.config file</p></div>
<h3><span style="font-weight: normal;">Initializing Log4net</span></h3>
<ul>
<li>Add Global.asax to your project and then add the following function to it.</li>
</ul>
<pre name="code" class="c#">
protected void ConfigureLogging()
{
	string logFile = HttpContext.Current.Request.PhysicalApplicationPath + "log4net.config";
	if (System.IO.File.Exists(logFile))
	{
		log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(logFile));
	}
}
</pre>
<ul>
<li>make a call to this function in Application_Start Event.</li>
</ul>
<pre name="code" class="c#">
void Application_Start(object sender, EventArgs e)
{
	// Code that runs on application startup
	ConfigureLogging();
}
</pre>
<h3><span style="font-weight: normal;">Logging Errors and Debugging Information</span></h3>
<ul>
<li>now we are ready to log</li>
<li>declare a log4net.ILog variable in your page which needs to log.</li>
</ul>
<pre name="code" class="c#">
#region Logger Setup
	protected static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
#endregion</pre>
<ul>
<li>Following code in Page_Load demonstrates how to log Debug Information and Errors.</li>
</ul>
<pre name="code" class="c#">
#region Logger Setup
	protected static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
#endregion
protected void Page_Load(object sender, EventArgs e)
{
	try
	{
		log.Debug("In Page Load Function");
		log.Debug("Going to generate divide by zero");
		int a = 0;
		int b = 10 / a;
	}
	catch (Exception ex)
	{
		log.Error(ex.Message, ex);
	}
}
</pre>
<ul>
<li>Run you website and see the results in the logs folder we created earlier. It should contain the file app.log with contents like</li>
</ul>
<pre name="code">
2008-11-02 15:13:09,093 [4]DEBUG _Default [(null)] - In Page Load Function
2008-11-02 15:13:09,140 [4]DEBUG _Default [(null)] - Going to generate divide by zero
2008-11-02 15:13:09,140 [4]ERROR _Default [(null)] - Attempted to divide by zero.
System.DivideByZeroException: Attempted to divide by zero.
at _Default.Page_Load(Object sender, EventArgs e) in d:WorkTestsLoggingSampleDefault.aspx.cs:line 26</pre>
<ul>
<li>thats it, your application is logging properly.</li>
<li>VB.NET developers would need little syntax conversions.</li>
<li>here is the sample code to download: <a href="http://www.dottostring.com/wp-content/uploads/2008/11/loggingsample.zip">Logging Sample</a></li>
</ul>
<script type="text/javascript">
  addthis_url    = 'http%3A%2F%2Fwww.dottostring.com%2F2008%2F11%2F5-how-to-use-log4net-logging-aspnet-application%2F';
  addthis_title  = 'How+to+use+Log4net+in+ASP.NET+Application';
  addthis_pub    = 'erfaan';
</script><script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" ></script>
]]></content:encoded>
			<wfw:commentRss>http://www.dottostring.com/2008/11/5-how-to-use-log4net-logging-aspnet-application/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>
