I would like to share the way to configure the logging application block with a sample web application to log the errors to the Event Listener and to a log file.
Once you have installed the Enterprise Library 5.0 Beta1 into your machine make sure to make it as strong named it by applying a key using the Visual Studio.
After that add a reference to the logging application block assembly Microsoft.Practices.EnterpriseLibrary.Logging.dll in your project.
the next step is to configure the Logging Application Block using the Enterprise Library tool located under Program Files => Microsoft patterns & practices => Enterprise Library 5.0 Beta 1 - February 2010 => Enterprise Library Configuration.
so the Configuration tool open like below :
Open your web.conf or app.conf file using the above tool as below:
by default it will list you the Application Settings and the Database settings and it does not contain anything and if you need to configure something on that feel free to add those values.
As we need to test the logging application blocks, please add the Logging Application Blocks by clicking Blocks => Add Logging Settings as below.
so it will lists you the settings as below :
Enter a name for your Event Log listener as below:
Under All Events settings make sure to add your recently created listener into the list as below:
Click the “+” sign next to the Logging Target Listeners and select Add Logging Target Listeners => Add Flat File Trace Listener.
Once you add the Flat File Listener the configuration tool will looks like below:
After that select the log file name to log the events and the Formatter as Text Formatter.
Upon making the above settings make sure to add the above listener to the All Events settings as below :
Upon following the above steps write the following code in C# and test the solution, you will find the entries written upon in the Event Log and the Log file which you did specified in the above step :
try
{
LogEntry logEntry = new LogEntry();
logEntry.Severity = System.Diagnostics.TraceEventType.Information;
logEntry.ProcessName = "Section XYZ from your project";
logEntry.Title = "Testing";
logEntry.Categories.Add("General");
logEntry.Message = string.Format("Your Message");
logEntry.TimeStamp = DateTime.Now; Logger.Write(logEntry);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
0 comments:
Post a Comment