Enterprise Library Logging 101
This article demonstrates the absolute minimum effort necessary to begin logging messages from within your application using Microsoft?s Enterprise Library 4.1.
I will include only a few details about what is happening under the covers, this is simply a HOW TO for getting that first logging message out there from a C# code file.
Installing Enterprise Library
Download and install the Enterprise Library 4.1. Realize that you are getting and installing a lot more than a logging library here. Be patient.
Creating a new Visual Studio project
- Open Visual Studio.
- Select New | Project | Visual C# | Console Application
- Name the project something appropriate like HelloWorldEntLibLogging
Adding a reference to the Ent. Lib. logging assembly
- In Solution Explorer, right click the project node and select Add Reference.
- On the .NET tab, select Enterprise Library Logging Application Block
- Click OK.
Configure Logging for the Application
Much of EntLib?s functionality is specified through configuration. Logging is no exception. Here, you?ll add an application configuration file to the project and add configuration information to it with the Visual Studio add-in that the EntLib installer added to Visual Studio.
- In Solution Explorer, right click the project node and select Add | New Item.
- Select Visual C# Items | General | Application Configuration File
- Leave the filename App.config as shown
- Click Add
- Open app.config in the XML editor (double click it in Solution Explorer) and note that it is empty. You should see something like this:
- Close app.config.
- Right click app.config in Solution Explorer and select Edit Enterprise Library Configuration as shown:
The Enterprise Library Configuration Editor opens.
- Right click the app.config node in the editor and select New | Logging Application Block as shown:
A new Logging Application Block node is added to the configuration editor as shown:
- Close the app.config visual editor window.
- Double click app.config in Solution Explorer to open it in the XML editor. You can see the configuration XML added for the logging block.
This can look intimidating the first time you see it. There is a lot of XML in there and that stays pretty true to the idea that with flexibility comes complexity. Ignore all this for now and just close the XML editor window.
Adding Logging Code to the Application
OK, the application is all configured up to work with logging and the dependencies are linked in. Time to code.
- Open Program.cs in the code editor.
- Add the highlighted using statement at the top of the code file.
- Inside the Main method, instantiate a new LogEntry object and set its Message property to something appropriate, like ?Hello EntLib Logging?.
- Use the static Logger.Write() method to submit the LogEntry for logging.
Your code should look similar to this:
You?ve done it! Your very first EntLib logging application.
Running the Application and Validating the Logging
- Press F5.
The application runs under the debugger and exits quickly, just flashing a command window.
- Open the Windows Event Viewer.
I am running on Windows 2008 Server, so I just Start ?Event Viewer?
- Open the application event log and view recent events.
You should see an informational event logged to the Windows Event Log as shown below.
The details of the information logged will look much like this:
Timestamp: 1/20/2009 11:20:08 PM
Message: Hello Ent. Lib. Logging
Application Domain: HelloEntLibLogging.vshost.exe
Process Id: 5384
Process Name: C:\projects\sandbox\HelloEntLibLogging\HelloEntLibLogging\bin\Debug\HelloEntLibLogging.vshost.exe
Win32 Thread Id: 4512
Although you can greatly control and extend the information logged here, we?ll save that for another time. For a simple ?gee whiz? try this.
Up the severity of you LogEntry when it is instantiated like this:
Now when you run the app, you get a really bad log entry :).