Logging is a very important feature for many applications and serves the very critical purpose in a software application's life. Logging enables looking for monitoring data and also documents what is happening inside and outside of the application; also known as auditing.
The most common places to log such activities, information and data are:
- Flat files (.log/.txt)
- Event logs (Windows event viewer) and
- Database.
In the absence of a Logging Application Block, the logging code needs to be repeated in the application in many places. This code will also include .NET library and function calls based on the type of place you want to log the information. Hence, a Logging Application Block simply de-couples the logging functionality from the application code.
Configuring the Logging Application Block
1. Open your Visual Studio project; if an app.config file is not already available, then add it
2. Open Microsoft Enterprise Library console.
3. In the Blocks menu, choose the "Add Logging Settings" option, this will appear as shown here:
4. Based on your requirements add listeners (File, Event, Database etc.).
5. Describe "Event Log" target listener:
6. Describe "Flat File" trace listener:
7. Describe "Message Formatter":
Now save this configuration by clicking File --> Save option and choose the .config file you added.
This will be modified and will appear as shown here.
Now you can add the code to enable the Logging from your application into the defined "Target Trace Listeners"; in our case "Flat File" and "Event Log".
I have designed a Windows Form application which reads a file from a specified path location. If the file is found it loads the content into the text box, else in the case of the exception "FileNotFoundException" the error info is logged into the Log file (c:\exception.log) and the Event Log.
The C# Code will look like this:
catch (FileNotFoundException)
and Event Viewer";
objLog.Message = "Please provide valid Filename";
}
}
}
Now if you open the C:\Exception.log you will see:
----------------------------------------
Timestamp: 10/1/2012 7:35:03 PM
Message: There is no explicit mapping for the categories 'File Read Error'. The log entry was:
Timestamp: 10/1/2012 7:35:03 PM
Message: Please provide valid Filename
Category: File Read Error
Priority: -1
EventId: 0
Severity: Information
Title:
Machine: VIDYAVRAT-PC
App Domain: LoggingApplicationBlock.vshost.exe
ProcessId: 9164
Process Name: C:\VidyaVrat\Microsoft .NET\Application Code POCs\EnterpriseLibrary-Application Blocks\LoggingApplicationBlock\LoggingApplicationBlock\bin\Debug\LoggingApplicationBlock.vshost.exe
Thread Name:
Win32 ThreadId:13588
Extended Properties:
Category:
Priority: -1
EventId: 6352
Severity: Error
Title:
Machine: VIDYAVRAT-PC
App Domain: LoggingApplicationBlock.vshost.exe
ProcessId: 9164
Process Name: C:\VidyaVrat\Microsoft .NET\Application Code POCs\EnterpriseLibrary-Application Blocks\LoggingApplicationBlock\LoggingApplicationBlock\bin\Debug\LoggingApplicationBlock.vshost.exe
Thread Name:
Win32 ThreadId:13588
Extended Properties:
----------------------------------------
Also, if you open the EventVwr.exe then you will see:
This concludes the explanation of how a Logging Application Block enables you to write a small amount of code to do such a critical task of auditing.

Dinesh GabhanePosted Nov 8, 2019, 1:34 AM
Thank you so much for article
Sr KarthigaPosted May 6, 2016, 9:17 PM
good one sir
Gowtham RajamanickamPosted Mar 16, 2015, 11:14 PM
sooper work...
Santhakumar MunuswamyPosted Mar 16, 2015, 1:49 PM
Good Work Sir
Tom MohanPosted Mar 16, 2015, 12:15 PM
nice one
ROHAN PANDEYPosted Mar 16, 2015, 6:12 AM
Thanks
Rahul Kumar SaxenaPosted Mar 16, 2015, 2:11 AM
Good Work..
sudhakar rPosted Nov 19, 2013, 4:08 PM
I want only Timestamp and Message in log file so I have removed all other properties like Category: {category}{newline} from Log message formatters template and saved and checked in app.config file not there those fileds. but still i am getting all those details in log files. Please let me know how to remove those details in log files. Thanks in advance
SAI KRISHNA VELLANKIPosted Nov 4, 2013, 8:43 AM
Thank you so much for you article. I have been waiting for this for a long time.
gkvnadh viswanadhPosted Feb 1, 2013, 1:18 AM
your every article is more effictiveness. i excepting few more articles.
Vidya Vrat AgarwalPosted Jan 1, 2013, 4:34 AM
Thanks Terrance and Vithal.
Vithal WadjePosted Jan 1, 2013, 4:20 AM
super work
Terrance CPosted Dec 31, 2012, 1:17 PM
Thanks for bring this functionality to my attention.