0

I'm trying to write a log without having a configuration file by using Fluent API.

this is my code:

 var builder = new ConfigurationSourceBuilder();
                builder.ConfigureData()
                .ForDatabaseNamed("Logging")
                .ThatIs.ASqlDatabase()
                .WithConnectionString(ConnectionString)
                .AsDefault();

 var configSource = new DictionaryConfigurationSource();
 builder.UpdateConfigurationWithReplace(configSource);

 var logger = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();

 logger.Write("Test", "General");

when I run this it just says that

Activation error occured while trying to get instance of type LogWriter, key ""

what did I do wrong in configuration ?

Pouyan
  • 2,849
  • 8
  • 33
  • 39

1 Answers1

2

There are 2 things missing:

  1. You forgot to update the container with the configuration using the EnterpriseLibraryContainer.CreateDefaultContainer method
  2. 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");
Randy Levy
  • 22,566
  • 4
  • 68
  • 94
  • yes thank you very much ... you made good notes .. but the problem is I cannot undrestand the complexity of configuring Logging block. this sample code from MSDN is just showing how to write to Event Log not Database. I could not find any source that shows how to configure Logging with fluent API – Pouyan Jan 08 '13 at 14:17
  • I second that, you are awesome. – LeLong37 Oct 16 '13 at 22:27
  • Awesome post. Thanks. But I think it is also have the same problem I am having. When the `logger` will be unloaded? – pcbabu Sep 08 '16 at 16:15