Quickstart tutorial into Enterprise Library logging
How do you go about logging with Enterprise Library with the following line
Logger.Write("Hello world");
I came up with this post because I explain Enterprise Library logging to a lot of people, in a very short time. You can probably talk about logging and Enterprise Library for a long, long time. But I just want to introduce the very basics of logging. When a new developer starts working at a customer or students in my class want to know, this is the shortest possible story I tell them. That’s why I decided to write this down, as it’s still information people tend to forget easily.
I’m talking about Enterprise Library 4.1 and this is what I’ll explain:
- Adding the necessary resources (assemblies, dll files)
- Adding and understanding minimal configuration.
- Logging information with and without a specific category.
Again, I could talk about priorities and more, but this is just the basics. For more information I’d redirect you to the Enterprise Library manual on logging.
Adding necessary resources
When you’ve installed Enterprise Library 4.1 correctly, you should have the assemblies in your “Add Reference…” dialog under the .NET tab. If not, you can always add them from the folder C:Program FilesMicrosoft Enterprise Library 4.1 – October 2008Bin or from C:Program Files (x86)Microsoft Enterprise Library 4.1 – October 2008Bin if you’re running on a 64bit machine.
Sidenote : It’s always best to copy the necessary assemblies to a project specific lib folder so that everyone can use your logging solution without installing Enterprise Library. This also goes for the build server, which should obtain the assemblies from source control.
In the above image you can see the “Enterprise Logging Application Block” we should add. In Visual Studio this is the only reference you need to add. After you’ve compiled your project however, you’ll notice additional assemblies being added to the /bin/ folder. It’s out of the scope of this article, what these assemblies do. But the following assemblies are the minimum you need to use Enterprise Library logging. If you have /lib/ folder in source control, put these assemblies in, for the build server to correctly build your projects.
- Microsoft.Practices.EnterpriseLibrary.Common.dll
- Microsoft.Practices.EnterpriseLibrary.Logging.dll
- Microsoft.Practices.ObjectBuilder2.dll
- Microsoft.Practices.Unity.dll
Adding minimal configuration
If you haven’t, first add a web.config or app.config to your application. If installed correctly, right-click your configuration file and select “Edit Enterprise Library Configuration” as shown on the right in image 2.
The Enterprise Library configuration tool will be opened. If not, run the tool from the start menu or locate it in the above mentioned folder. After opening it, you should see the empty configuration, as shown in image 3. There’s already a connectionstring in the Data Access block, which is ‘inherited’ from machine.config.
In image 3 you can see an arrow where you should right-click and select “New” and “Logging Application Block”. When the configuration is added, these are the folders that are added
- Filters
I won’t discuss these, but they’re not too difficult to understand either.
- Category sources
Everything you log in your application, falls under a category. When you don’t specify a category while logging, the default category is used. You can see the default category added right now is called “General”.
- Special sources
When Enterprise Library tries to log something but it doesn’t work (for example you try to log to the database, but it can’t find the database) or it can’t find a category you specified, this is the place to configure what should happen then.
- Trace Listeners
If you want to log to the database, a file, eventlog or anything else, you need trace listeners. By default trace listeners are added for logging to database, file, email, eventlog, msmq, wmi and more… - Formatters
You need formatters to… well, format your logging messages.
We’ll start with these.
Formatters
Open up the “Formatters” folder and select “Text Formatter”. In the properties pane (press F4 if it’s not there or select from the menu “View” and “Properties Window”. In the properties pane (or window) there’s a property called “Tempalte”. Select its value and press the button on the site as shown on the right.
A new dialog window should open with the current message template. As you can see there’s a lot of data there. The minimum message you can use should probably be something like below. When logging to, for example, a database or you’re tracing a lot of messages for some reason, this is probably easier to digest. You can use a cool tool like baretail to read the messages while they’re being written to the logfile on the server. When you’re logging exceptions or other information, you probably want much more detail. At the bottom of the dialog or the possible tokens you can insert. For now, leave it like it is.
However, do rename the formatter from “Text Formatter” to “My Formatter”. Now right-click any node in the configuration editor and select “Validate”. You’ll see everything validates, even though you just changed the name of the formatter. That’s how cool Enterprise Library is. 😉
Trace Listeners
We’ll add a new trace listener as the “Formatted EventLog TraceListener” can give problems on a server because the account that’s trying to log messages isn’t authorized to write into the eventlog. Right-click the “Trace Listeners” folder and select “New” and “Rolling Flat File Trace Listener”. In the properties pane, rename it to “Rolling Flat File”. Change the filename so that it will log to a specific directory. I normally use something like “C:LoggingProjectName"”. You can already specify a filter here, but we’ll leave it at “All”.
For the formatter property you’ll have to select “My Formatter”, which is the text formatter you just renamed. Also notify the other properties available. You can specify to use a time interval to create a new logfile, or a size interval. Set it to a small size, something like 1Kb so you can easily test what will happen.
Categories
Let’s add a new category and set that category as default one. If you do so, it’s probably best to remove the “General” category, as most people will think that will be the default. Create a new category called “DefaultCategory”. Select the “Logging Application Block” folder and in the properties pane select your new category to be the default one.
Right-click your new “DefaultCategory” category and select “New” and “Trace Listener Reference”. In the properties pane, change the “ReferencedTraceListener” to be your “Rolling Flat File” trace listener.
Logging information
You’re now done with the configuration. Save it and go to your code. Enter the following line somewhere in your code:
Logger.Write("Hello world");
Now go check your configured folder for the file. If it’s not that, check the eventlog anyway, as Enterprise Library might have failed and maybe has written the info there. This worked for me, as I ran into this read or write protected memory error when creating this article.
Thanks, I’ll read this before implementing on my next project!
Thanks, good tutorial
Thanks.
Is there a trace-listener for the console?
I’d like do a ‘logger.write’ to both a file and the console.
@Dries : You need a custom trace listener. Here’s an example:
http://stackoverflow.com/questions/1011106/how-to-write-just-message-to-debug-output-with-enterprise-library-logging
Is there a way to write on the log specifying the log category (routing) ?
Something like : logger.write(‘Hello World’, WorldCategory)
Thanks,
MLebel
@Maxime : Yes there is, just like you write… You just need some additional quotes, like:
Logger.Write(“Hello World”, “WorldCategory”);
Where I talk about configuration, under “category sources” I talk about the “General” category. This is the default. You can create more categories. Normally I use on for logging/tracing and another one for exceptions. Both write to a rolling-file but both with a different filename.
http://msdn.microsoft.com/en-us/library/dd203255.aspx#props_categorysources
http://msdn.microsoft.com/en-us/library/dd139943.aspx
A perfect stater for a beginner like me – Thanks a lot
Exactly what I needed to get started with EL. Great job! Missed this in the EL documentation! This should have been the first subject in the documentation! Gets you going in 5 minutes!
Good Post.Its really interesting.
Excellant tutorial. Can you pls give a tutorial for Database trace listener.
In the beginning of this article you mentioned something like “your students”. I am curious, if you offer any online courses which i am very much willing to register for.
Please advice me.
But Thanks a lot for this article.
Sorry, no online training…
You can ask me questions via the contact page any time you’d like, or check out http://www.pluralsight.com for online training. Those are quite good.
I am not able to find the log file. No errors. But log file is missing!!
Same here. No log file!
Some possibilities:
1) Permissions
You don’t have permission to write on disk?!
Also check under which user the process is running. For example if it’s a website that’s logging, it’s probably “Network Service” and if it’s a Windows Service it’s possibly “Local System”
2) Configuration
Something with configuration is wrong. EntLib is throwing exceptions. It wants to log these, but no listener is configured.
Under “Logging Application Block”, under “Special Sources”, under “Logging Errors & Warnings” make sure there’s a trace listener that can ALWAYS write. If EntLib throws exceptions, it uses that trace listener to notify you about it.
Amazing. I just spent 4 hours fighting with the Application Blocks, and you got me where I needed to be in 5 minutes.
Thanks!
Can I recommend doing one with the Exception Block that logs to a SQL server?
Awesome Post sir
To “Aoe”:
you can simply create your won rolling.log in the application folder and give its path in your listener.
make sense, easy to understand. nice posting..
Is it possible to easily change this to write to a log file instead of the event viewer?
@Alison : Yes
Thanks for the nice tutorial you posted here. Helped me big time.