In this article, you will learn how to use Logging Application Block with a real-world example in ASP.NET.
Why use Logging Application Block? Developers frequently write applications that require logging functionality. Typically, these applications format and log information in response to application events. For example, developers often write code to log information in response to unexpected conditions, such as an application exception, or failure to connect to a database. Developers also write code to trace application flow through components during the execution of an application use case or scenario. The Enterprise Library Logging Application Block simplifies the implementation of common logging functions. You can use the Logging Application Block to write information to a variety of locations:
- The event log
- An e-mail message
- A database
- A message queue
- A text file
- A Windows® Management Instrumentation (WMI) event
- Custom locations using application block extension points
Read more at: http://msdn.microsoft.com/en-us/library/ff664569(v=pandp.50).aspx
Install Enterprise Library
Please follow this link to download the Enterprise Library:
http://www.microsoft.com/en-in/download/details.aspx?id=15104
Getting Started
Begin using the following procedure:
- Start Visual Studio
- Create a new website
- Provide the name and location of website
- Click "Next"
Now add a reference for the following two assemblies in the bin folder: "Microsoft.Practices.EnterpriseLibrary" and "Logging.dll".
Now open the web.config file in edit mode and edit the logging settings.
Image 1.
First of all add the Logging setting block.
Image 2.
Now delete the existing event log listener.
Image 3.
And add a new flat file trace listener.
Image 4.
Now provide the following details, like formatter name, header footer message and trace output options and in the general tab select the flat file trace listener and in the logging errors and warnings, select the flat file trace listener and click "Save".
Image 5.
Now check the updated web.config.
- <configSections>
- <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
- </configSections>
- <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
- <listeners>
- <add name="Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
- fileName="trace.log" formatter="Text Formatter" />
- </listeners>
- <formatters>
- <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
- template="Timestamp: {timestamp}{newline} Message: {message}{newline} Category: {category}{newline} Priority: {priority}{newline} EventId: {eventid}{newline} Severity: {severity}{newline} Title:{title}{newline} Machine: {localMachine}{newline} App Domain: {localAppDomain}{newline} ProcessId: {localProcessId}{newline} Process Name: {localProcessName}{newline} Thread Name: {threadName}{newline} Win32 ThreadId:{win32ThreadId}{newline} Extended Properties: {dictionary({key} - {value}{newline})}"
- name="Text Formatter" />
- </formatters>
- <categorySources>
- <add switchValue="All" name="General">
- <listeners>
- <add name="Flat File Trace Listener" />
- </listeners>
- </add>
- </categorySources>
- <specialSources>
- <allEvents switchValue="All" name="All Events" />
- <notProcessed switchValue="All" name="Unprocessed Category" />
- <errors switchValue="All" name="Logging Errors & Warnings">
- <listeners>
- <add name="Flat File Trace Listener" />
- </listeners>
- </errors>
- </specialSources>
- </loggingConfiguration>
To test whether it's working write some code in the page.
- protected void Button1_Click(object sender, EventArgs e)
- {
- LogEntry logEntry = new LogEntry();
- logEntry.EventId = 100;
- logEntry.Priority = 2;
- logEntry.Message = "message by Raj Kumar";
- Logger.Write(logEntry);
- }
Now run the application to see the output.
As you will see, there is a trace, "trace.log", that has been created in your application root, now open it.
Image 6.