Dennis van der Stelt

The only way to win is to learn faster than anyone else

Community

Email Notifications

News

  • Addicted to Refactor! Pro

I read...

I Use...

Tags

Recent Posts

Archives

Blog Subscription Form

  • Email Notifications
    Go

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 Files\Microsoft Enterprise Library 4.1 - October 2008\Bin or from C:\Program Files (x86)\Microsoft Enterprise Library 4.1 - October 2008\Bin 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.

addreferencedialog

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.dllEditConfiguration
  • 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.

InitialConfigThe 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

  1. Filters
    I won’t discuss these, but they’re not too difficult to understand either.
  2. 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”.
  3. 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.
  4. 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…
  5. Formatters
    You need formatters to… well, format your logging messages.
    We’ll start with these.

Formatters

FormatterTemplatePropertiesOpen 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.

FormatterTemplateEditor

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:\Logging\ProjectName"\”. 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.

Comments

NewsPeeps said:

Thank you for submitting this cool story - Trackback from NewsPeeps

# July 21, 2009 8:48 PM

Sanjeev Agarwal said:

Daily tech links for .net and related technologies - July 22-24, 2009 Web Development Simplify calling

# July 23, 2009 1:16 PM

DotNetShoutout said:

Thank you for submitting this cool story - Trackback from DotNetShoutout

# July 23, 2009 2:31 PM

Nathan Prather said:

Thanks, I'll read this before implementing on my next project!

# July 23, 2009 5:30 PM

9eFish said:

9efish.感谢你的文章 - Trackback from 9eFish

# July 24, 2009 5:53 PM

Deidn said:

Thanks, good tutorial

# July 30, 2009 11:29 AM

Dries said:

Thanks.

Is there a trace-listener for the console?

I'd like do a 'logger.write' to both a file and the console.

# August 25, 2009 4:45 PM

Dennis van der Stelt said:

# August 25, 2009 8:05 PM

Maxime Lebel said:

Is there a way to write on the log specifying the log category (routing) ?

Something like : logger.write('Hello World', WorldCategory)

Thanks,

MLebel

# August 31, 2009 4:23 PM

Dennis van der Stelt said:

@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.

msdn.microsoft.com/.../dd203255.aspx

msdn.microsoft.com/.../dd139943.aspx

# August 31, 2009 4:35 PM

Gangadhar said:

A perfect stater for a beginner like me - Thanks a lot

# September 14, 2009 11:57 AM

Anders Ericsson said:

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!

# October 28, 2009 11:27 PM

traslochi milano said:

Good Post.Its really interesting.

# November 27, 2009 8:57 AM

Hitu said:

Excellant tutorial. Can you pls give a tutorial for Database trace listener.

# December 11, 2009 2:19 PM

crazyfire said:

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.

# December 18, 2009 3:42 AM

Dennis van der Stelt said:

Sorry, no online training...

You can ask me questions via the contact page any time you'd like, or check out www.pluralsight.com for online training. Those are quite good.

# December 18, 2009 7:16 AM

Tom said:

I am not able to find the log file. No errors. But log file is missing!!

# December 31, 2009 9:44 AM

Aoe said:

Same here. No log file!

# January 6, 2010 8:40 AM

Dennis van der Stelt said:

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.

# January 6, 2010 8:50 AM

Bulldog said:

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?

# October 28, 2010 8:39 PM

Sachin Arora said:

Awesome Post sir

# November 16, 2010 12:33 AM

vin said:

To "Aoe":

you can simply create your won rolling.log in the application folder and give its path in your listener.

# January 7, 2011 2:44 PM

Dan said:

make sense, easy to understand. nice posting..

# March 18, 2011 9:22 PM

Alison said:

Is it possible to easily change this to write to a log file instead of the event viewer?

# May 5, 2011 3:36 PM

Dennis van der Stelt said:

@Alison : Yes

# May 9, 2011 12:51 PM

IamaC said:

Thanks for the nice tutorial you posted here. Helped me big time.

# September 5, 2012 3:47 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Please add 5 and 1 and type the answer here: