There are 2 things missing:
- You forgot to update the container with the configuration using the
EnterpriseLibraryContainer.CreateDefaultContainer
method
- You are configuring data access but are trying to use the logging block
Here's how you create the container with the configuration information:
var builder = new ConfigurationSourceBuilder();
builder.ConfigureData()
.ForDatabaseNamed("Logging")
.ThatIs.ASqlDatabase()
.WithConnectionString(ConnectionString)
.AsDefault();
var configSource = new DictionaryConfigurationSource();
builder.UpdateConfigurationWithReplace(configSource);
EnterpriseLibraryContainer.Current
= EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
To configure logging using the Fluent API see Using the Fluent Configuration API.
The example from that article is:
var builder = new ConfigurationSourceBuilder();
builder.ConfigureLogging()
.WithOptions
.DoNotRevertImpersonation()
.LogToCategoryNamed("Security")
.SendTo.FlatFile("Security Log File")
.FormatWith(new FormatterBuilder()
.TextFormatterNamed("Text Formatter")
.UsingTemplate("Timestamp: {timestamp}...{newline})}"))
.ToFile("security.log")
.SendTo.EventLog("Formatted EventLog TraceListener")
.FormatWithSharedFormatter("Text Formatter")
.ToLog("Application")
.LogToCategoryNamed("General")
.WithOptions.SetAsDefaultCategory()
.SendTo.SharedListenerNamed("Formatted EventLog TraceListener");
var configSource = new DictionaryConfigurationSource();
builder.UpdateConfigurationWithReplace(configSource);
EnterpriseLibraryContainer.Current
= EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
If you wish to configure both the logging block to write to a database using the data access block you can also do it using the fluent interface:
var builder = new ConfigurationSourceBuilder();
builder.ConfigureData()
.ForDatabaseNamed("Logging")
.ThatIs.ASqlDatabase()
.WithConnectionString(@"data source=.\SQLEXPRESS;Integrated Security=SSPI;Database=Logging")
.AsDefault();
builder.ConfigureLogging()
.WithOptions
.DoNotRevertImpersonation()
.LogToCategoryNamed("General")
.WithOptions.SetAsDefaultCategory()
.SendTo.Database("Database Trace Listener")
.WithAddCategoryStoredProcedure("AddCategory")
.UseDatabase("Logging")
.Filter(System.Diagnostics.SourceLevels.All)
.WithWriteLogStoredProcedure("WriteLog");
var configSource = new DictionaryConfigurationSource();
builder.UpdateConfigurationWithReplace(configSource);
EnterpriseLibraryContainer.Current
= EnterpriseLibraryContainer.CreateDefaultContainer(configSource);
var logger = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
logger.Write(logEntry);
Logger.Write("Test", "General");