Wednesday, January 20, 2010

My First Look at EnterpriseLibrary.Logging

Last week I got opportunity to work with the logging module of our application.It was done by one of my colleague and he told that it is done using Logging module of .Net EnterpriseLibrary.
After looking around the code, I was surprised on the lines of code below which does the logging.Rest is all taken care by configuration entries in web.config.

using (new Tracer("My Tracer"))
{
LogEntry le = new LogEntry() { Message = message };
Logger.Write(le);
}



When I looked into the web.config I was able to see some entries in a section named loggingConfiguration




<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />



The above code should be inside configSectionsTag.The rest is a new section as follows added after the config sections.




<loggingConfiguration name="Logging Application Block" tracingEnabled="false"
defaultCategory="" logWarningsWhenNoCategoriesMatch="true">
<listeners>
<add fileName="log.log"
header="-------------Log Message---------------------"
rollFileExistsBehavior="Increment"
rollInterval="None"
rollSizeKB="1024"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
traceOutputOptions="None"
filter="All"
type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
name="FlatFileListener" />
</listeners>
<specialSources>
<allEvents switchValue="All" name="All Events" />
<notProcessed switchValue="All" name="Unprocessed Category" />
<errors switchValue="All" name="Logging Errors &amp; Warnings">
<listeners>
<add name="FlatFileListener" />
</listeners>
</errors>
</specialSources>
</loggingConfiguration>



I hope the config is self explanatory.It uses a flat file writer which writes into the file log.log present in the root directory.

I usually afraid of these config entries which cannot be memorized by a developer.If you are also afraid of the config entries use the Enterprise Library Configuration Tool.


This is just a sample.There lot more things which we can do with the logging application block such as format the text which is writing into the file.More properties such as priority,category etc…,Variety of logging  locations such as database,email etc…If I get some more extra time I will surely blog about the same.More details can be found in the below links.



http://bloggingabout.net/blogs/dennis/archive/2009/07/21/quickstart-tutorial-into-enterprise-library-logging.aspx


http://www.codeguru.com/csharp/.net/net_framework/systemnamespace/article.php/c11281/


http://www.codeproject.com/KB/architecture/GetLoggingWithEntLib.aspx



NB : Don’t forget to download the CAB and add reference to the following dlls when  you start a new project.




  • Microsoft.Practices.EnterpriseLibrary.Common.dll


  • Microsoft.Practices.EnterpriseLibrary.Logging.dll


  • Microsoft.Practices.ObjectBuilder2.dll



I have uploaded a sample here.

No comments: