Monday, June 23, 2008

Database Logging with Enterprise Library 4.0

Hi All,
I am a begginer in Microsoft technologies.
I was trying enterprise library for Logging Past three days.Here is a quick walk through of what i have done in my application hope it helps at least one who is new to Enterprise library..
I had a typical scenario in which i was supposed to log the LogCategory and LogMessage in the Database(SQL Server) Using Enterprise Library 4.0.
First of All we should be careful with the .dll's while playing around.We should refer to the Dll's only in the Bin folders else will through type error and create lot of problems when u debug the application.

Add references to Logging,Logging Database and Enterprise Library Shared Assembly from Bin of the program files folder.

using Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Logging.Database;


LogEntry dblog = new LogEntry();
dblog.Message = "DB Log Message";
dblog.Categories.Add("Error");
dblog.EventId = 1;
Logger.Write(dblog);

Open the web Config File using configration tool which is available in the Bin folder.
Which will give a good graphical user interface than using the ugly Xml's in the web config.
Now Add the Logging Application Block and specify the Connection String to your need.Add trace listners corresponding to your need.Save the Web Config file.Also be careful with the Text formatter if you not specifing it in Web Config file corresponding to the DatabaseTraceListner with out which runtime it will throw you BuildFailedException(The current build operation (build key Build Key[Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter, null]) failed: The value can not be null or string or empty. Parameter name: name (Strategy type Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder.ConfiguredObjectStrategy, index 2) )Enterprise library will take care the remaining for you in logging your information into the database!!

No comments: