not using the configuration file

Topics: Logging Application Block
Aug 25, 2011 at 9:23 AM

Hello, 

I've known that logging block to a database has a configuration file that contains the database, server name and other credentials to connect to the target database. 
I have this application that's already had a configuration file that contains the connection string. But I would like to add the logging application block on this application. 
I would like to ask your help if there is any way wherein I won't have to use the configuration file of the logging application block? Instead retrieve the connection string of my application and be able to pass this information as connection for the logging block. 
Thanks for your time in reading this concern. I greatly appreciate any replies. 

Aug 25, 2011 at 11:22 AM

Hi gelai,

You can leverage the Fluent Configuration API on your scenario where you can create your own configuration at runtime. You will have to configure both database and logging application block. Let me know if you have further questions :)

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us

Sep 2, 2011 at 8:02 AM

hello,

Thanks for the reply. Had created the following: 

      var builder = new ConfigurationSourceBuilder();           

                       builder.ConfigureData()                                                                                            .ForDatabaseNamed("dbaseName")                                                   .ThatIs.ASqlDatabase()                                      .WithConnectionString("Server=serverName; database=dbaseName; Integrated Security=SSPI;")                                                                                  .AsDefault();

var configSource = new DictionaryConfigurationSource();                                                                               builder.UpdateConfigurationWithReplace(configSource);                                                  EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);

      and it created a section which contains the connection string above. How would the LogEntry recognize this new configuration? 

Sep 2, 2011 at 8:43 AM
Edited Sep 2, 2011 at 10:13 AM

Hi,

Create an instance of LogWriter after your code above:

LogWriter myWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();

 then pass your LogEntry object on call to Write() method of LogWriter.

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us

Sep 8, 2011 at 8:44 AM

Hello,

Thanks for the reply. I had a different question.

 LogEntry logEntry = new LogEntry();
logEntry.EventId = 210;
logEntry.Priority = 2;
logEntry.Message = "Informational message";
Logger.Write(logEntry);

then having app.config as my configuration file. i tried to rename the app.config, an error will display.

does it mean that it will only used the "app.config" filename?

Sep 9, 2011 at 1:42 AM
Edited Sep 9, 2011 at 1:42 AM

Hi,

Yes it will look for a configuration of that name. If it is a web application, then it will look for the web.config.

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us

Sep 16, 2011 at 8:08 AM
AvanadeSupport wrote:

Hi,

Create an instance of LogWriter after your code above:

LogWriter myWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();

 then pass your LogEntry object on call to Write() method of LogWriter.

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us

Hello,

when i tried to use the code above it returns an error. while doing this i didn't have any app.config. am i doing this right? :'(

thanks,

gelai

Sep 16, 2011 at 8:43 AM

Hi gelai,

Have you configured your settings through the Fluent Configuration API?

 

Noel Angelo Bolasoc
Avanade Software
Avanade, Inc.
Contact Us

 

Sep 19, 2011 at 12:08 AM

Hellow,

yes, i used the code posted above. but still it doesn't work. :(

gelai

Sep 19, 2011 at 1:35 AM

Hi gelai,

Looking at the code you've posted, you just only configured the data access application block. Since you are using the logging settings, you must also configure it. Here is the sample logging settings from Fluent Config API

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);

Hope this helps.

 

Noel Angelo Bolasoc
Avanade Software
Avanade, Inc.
Contact Us

Sep 20, 2011 at 6:09 AM

hello, 

i tried to test the following sample code. it doesn't had any error but it doesn't save the record at Log table. :(

var builder = new ConfigurationSourceBuilder();

builder.ConfigureData()
       .ForDatabaseNamed("databaseName")
         .ThatIs.ASqlDatabase()
         .WithConnectionString("server=(local); database=databaseName; Integrated Security=true;")
         .AsDefault();

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);

LogEntry logEntry = new LogEntry();
logEntry.EventId = 171;
logEntry.Priority = 1;
logEntry.Categories.Add("Trace");
logEntry.Message = "Message";

LogWriter myWriter = EnterpriseLibraryContainer.Current.GetInstance();
myWriter.Write(logEntry); 

Sep 20, 2011 at 6:45 AM

Hi,

It seems you forgot to configure your database trace listener. Here is a sample code:

builder.ConfigureLogging()
                .WithOptions
                .LogToCategoryNamed("General")
                .SendTo
                .Database("DbListener").UseDatabase("LoggingDb").WithAddCategoryStoredProcedure("AddCategory").
                WithWriteLogStoredProcedure("WriteLog");

            builder.ConfigureData().ForDatabaseNamed("LoggingDb").ThatIs
                .ASqlDatabase().WithConnectionString(
                    "Data Source=(local)\\;Database=Data_Sync_Dev;Initial Catalog=Logging;" +
                    "Trusted_Connection=True;").AsDefault();

Let me know if you have further questions :)

 

Noel Angelo Bolasoc
Avanade Software
Avanade, Inc.
Contact Us

 

Sep 22, 2011 at 12:01 AM
Edited Sep 22, 2011 at 12:02 AM

hello,

thanks for the quick reply. i tried testing the code, and it doesn't return any error but still cannot save in the database. oh my.. :(

Sep 22, 2011 at 2:32 AM

Hi gelai,

Kindly double check if you are using the proper connectionstring. If the problem still there, you can send us your solution so we can analyze it. Send it to entlib.support@avanade.com.

 

Noel Angelo Bolasoc
Avanade Software
Avanade, Inc.
Contact Us

 

 

Sep 22, 2011 at 3:35 AM

Hello,

Thanks. Email sent.

gelai

Sep 22, 2011 at 5:34 AM

I noticed that you are using the default settings for logging which is pointing to an eventlog since you set the "General" category as the default with this code:

LogToCategoryNamed("General")
                 .WithOptions.SetAsDefaultCategory()
                 .SendTo.SharedListenerNamed("Formatted EventLog TraceListener");

but your database trace listener is in the "Security" category. Note that a parameterless call to get instance invokes the default category. What you can do is to set your "Security" category as the default or supply the method with "Security":

EnterpriseLibraryContainer.Current.GetInstance<LogWriter>("Security");

Hope this helps :)

 

Noel Angelo Bolasoc
Avanade Software
Avanade, Inc.
Contact Us

Nov 9, 2011 at 5:46 AM

Hello,

It's been a while, I was on to a different task. Anyways, I tried the following. It has no error but still doesn't save anything on Log table. :(

 

            var builder = new ConfigurationSourceBuilder();
            builder.ConfigureData()
                 .ForDatabaseNamed("dbaseName")
                   .ThatIs.ASqlDatabase()
                   .WithConnectionString(@"Server=SERVERNAME\SQL2008_CI_AS; database=dbaseName; Integrated Security=SSPI;")
                   .AsDefault();

            //configuring for the logging settings                              
            builder.ConfigureLogging()
                   .WithOptions
                   .LogToCategoryNamed("General")
                   .SendTo
                    .Database("Formatted Database TraceListener").UseDatabase("dbaseName")
                    .WithAddCategoryStoredProcedure("AddCategory")
                    .WithWriteLogStoredProcedure("WriteLog");

            //configuration
            var configSource = new DictionaryConfigurationSource();
            builder.UpdateConfigurationWithReplace(configSource);
            EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);

            LogEntry logEntry = new LogEntry();
            logEntry.EventId = 180;
            logEntry.Priority = 1;            
            logEntry.Message = "Message";

            LogWriter myWriter = EnterpriseLibraryContainer.Current.GetInstance();
            myWriter.Write(logEntry);

Thanks. 

 

Nov 10, 2011 at 1:48 AM

The configuration is set with a category of "General" but no categories are set on the LogEntry and no default category is set.

Add the General category and you should be OK (provided database configuration is working :) ):

logEntry.Categories.Add("General");

Or modify the configuration to set a default category:

builder.ConfigureLogging()
                   .WithOptions
                   .LogToCategoryNamed("General")
                   .WithOptions.SetAsDefaultCategory()


--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com

Nov 10, 2011 at 3:09 AM

weee~ thanks! it's working now. :D am so super happy! :D