Thursday, April 26, 2007

Ent Libs - How to use Logging block?

Did you know how easy it was to use Ent Libs. In this post i am showing you how to use Logging block. You need to install Ent Libs 2.0 if you are using .net 2.0. Ent Libs 3.0 is out for .net 3.0. You then open Enterprise library configuration tool to create a new app.config or specify your application config file to be modified. It is here you will specify how you want to log.... File, Event, Database or other different listeners provided. This tool is highly configurable so feel free to explore a little. This post will show you how to log your errors/messages to a text file. Once you are configured through this config file you just have to pretty much write 2 lines of code. it? Super Easy! :)

C# Code -
private static FlatFileTraceListener nFile;

private static void WriteLog(string sComments)
if (nFile != null)
nFile.Write(DateTime.Now.ToString() + ": ");
Your Config file should be with entries of Ent Libs and trace listeners based on the settings we did above using the Ent Libs GUI tool.
Cheers - Dipesh

No comments: