Configuration-less Operation

Topics: Enterprise Library Core, Logging Application Block
Feb 2, 2007 at 6:28 AM
One thing I had to override in 2.0 which I would like to see go forward in 3.0 is the ability to use the logging block without any configuration values. We built our own Logger class which simply did not do anything if the logging configuration wasn't present. I found this quite useful when building libraries that were used in other applications. Is this something that should/could make sense for 3.0?
Feb 2, 2007 at 2:02 PM
I am not sure I quite understand your question, but it sounds like you want the ability to use the Logging Application Block without using an IConfigurationSource.

You have always had the option of programmatically creating the objects directly in the logging application block. Here is a tutorial I wrote that shows you how to create formatters, tracelisteners, category sources, etc. programmatically to use in your application:

Programmatically Using Logging Application Block With No Configuration File

I haven't tried it in Enterprise Library 3.0, but I assume you still have the option of doing everything programmatically.

Regards,

Dave

_____________________

David Hayden
Microsoft MVP C#
Feb 2, 2007 at 5:30 PM
Dave,

What I'm looking to do is use the application configuration if it exists, otherwise have it default to a non-existant log source so we don't have to remove the logging code (and possible set ShouldLog to return false at all times). Your article does touch on that scenario, but it highlights that all configuration must be done in code. I'm looking for a more transparent way in the logging wire-up logic itself to detect the non-existent configuration scenario and handle it more gracefully (that is, don't throw an exception).

Currently we have a class that will try and wire up a logger and catch the configuration exception to return an non-existent logger if there is no configuration. It feels like a hack to me.

Cheers,
Colin
Feb 2, 2007 at 10:20 PM
What would be the expected behavior if no config file exists? Write everything to the event log?

Tom
Feb 3, 2007 at 12:51 AM
Tom,

If no configuration file existed then I would expect no logging coming out of the application. I'm sure there are arguments for and against this suggested approach. I'm looking for an easy way to disable it on small tool-like apps that don't need it but reuse those same libraries on bigger apps where I will investing the time in proper configuration for production logging. Somewhat akin to the instrumentation - optional functionality depending on the scenario.

Cheers,
Colin
Feb 23, 2007 at 6:15 PM
I'm in the same boat as Colin. I've recently started using the logging block in my core "utilities" DLL, and now anything that uses my DLL blows up with ConfigurationExceptions if they don't add config file entries for the logging block. For production apps, its not a big deal to setup a config file, but its very annoying to have to add an App.Config file in every project that uses my DLL just to get unit testing to work.

My DLL needs to encapsulate its requirements and provide a default configuration. Can you imagine how annoying it would be if System.Diagnostics.Trace.Write blew up every time it was called if you didn't manually configure a trace listener? That would suck. Being able to supply a default configuration at the DLL-level is a good thing. I'd like to be able to:

#1 Check a Logger.IsConfigured property (or something like that) to see if the logging system has a valid configuration that it can run with.

#2 If it doesn't have a valid configuration, I'd like to be able to point it to a resource string (or an XmlReader) that contains the configuration it should use by default.

In my case I've wrapped all the logging block behind my own Log class, so I could do this internally in a static constructor if capabilities #1 and #2 existed. Right now with EntLib 3.0 Jan 07 CTP, I'm trying to figure out how to do these things as simply as possible. I've seen David's article on programmatic configuration, but I'd hate to have to do all that versus just using a default configuration from a resource.

Any guidance in this area would be appreciated.
Feb 27, 2007 at 3:59 PM
FWIW, here's an easy way to tell if logging is configured or not:

LoggingSettings settings = LoggingSettings.GetLoggingSettings(ConfigurationSourceFactory.Create());
bool isLoggingConfigured = settings != null;

I haven't found an easy way to plug in my own configuration from a resource, but at least I don't have to catch and eat ConfigurationExceptions now. I've updated my Log class to silently do nothing if no configuration is present. That's pretty much what System.Diagnostics.Trace does (other than OutputDebugString), and it's a useful behavior. Now if anyone using my library ever wants something to be logged, all they have to do is add a proper config file. Just consuming my library doesn't require them to add configuration for it.