Runtime change in database for Logging block

Topics: Logging Application Block
Jan 13, 2009 at 10:24 PM

Hi,
 I am changing an application for logging settings. How can i tell my logger.write method of Entlib 2.0 to use specific database on runtime. thanks...

 Logger.Write(prsEx.Message, prsEx.Category, prsEx.Priority, 1, prsEx.Severity, prsEx.Title). At present logger picks the value from web.config file ...

 

<

 

loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">

 

<

 

listeners>

 

<

 

add databaseInstanceName="LoggingConnectionString" writeLogStoredProcName="WriteLog" addCategoryStoredProcName="AddCategory" formatter="Text Formatter" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Database.Configuration.FormattedDatabaseTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.Database.FormattedDatabaseTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging.Database, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" name="Database Trace Listener"/>

 

</

 

listeners>

 

<

 

formatters>

 

<

 

add template="Timestamp: {timestamp}&#xA;Message: {message}&#xA;Category: {category}&#xA;Priority: {priority}&#xA;EventId: {eventid}&#xA;Severity: {severity}&#xA;Title:{title}&#xA;Machine: {machine}&#xA;Application Domain: {appDomain}&#xA;Process Id: {processId}&#xA;Process Name: {processName}&#xA;Win32 Thread Id: {win32ThreadId}&#xA;Thread Name: {threadName}&#xA;Extended Properties: {dictionary({key} - {value}&#xA;)}" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" name="Text Formatter"/>

 

</

 

formatters>

 

<

 

categorySources>

 

<

 

add switchValue="All" name="Security">

 

<

 

listeners>

 

<

 

add name="Database Trace Listener"/>

 

</

 

listeners>

 

</

 

add>

 

<

 

add switchValue="All" name="Warning">

 

<

 

listeners>

 

<

 

add name="Database Trace Listener"/>

 

</

 

listeners>

 

</

 

add>

 

<

 

add switchValue="All" name="UnhandledException">

 

<

 

listeners>

 

<

 

add name="Database Trace Listener"/>

 

</

 

listeners>

 

</

 

add>

 

<

 

add switchValue="All" name="HandledException">

 

<

 

listeners>

 

<

 

add name="Database Trace Listener"/>

 

</

 

listeners>

 

</

 

add>

 

<

 

add switchValue="All" name="General">

 

<

 

listeners>

 

<

 

add name="Database Trace Listener"/>

 

</

 

listeners>

 

</

 

add>

 

</

 

categorySources>

 

<

 

specialSources>

 

<

 

allEvents switchValue="All" name="All Events"/>

 

<

 

notProcessed switchValue="All" name="Unprocessed Category"/>

 

<

 

errors switchValue="All" name="Logging Errors &amp; Warnings">

 

<

 

listeners>

 

<

 

add name="Database Trace Listener"/>

 

</

 

listeners>

 

</

 

errors>

 

</

 

specialSources>

 

</

 

loggingConfiguration>

 

<

 

dataConfiguration defaultDatabase="PRSConnectionString"/>

 

<

 

connectionStrings>

 

<

 

add name="LoggingConnectionString" connectionString="User ID=sa;Password=password!;Initial Catalog=Logging;Data Source=PRS01" providerName="System.Data.SqlClient"/>

 

<

 

add name="PRSConnectionString" connectionString="User ID=sa;Password=password!;Initial Catalog=PRS_DEV_10312008;Data Source=PRS01" providerName="System.Data.SqlClient"/>

 

</

 

connectionStrings>

 

Jan 14, 2009 at 3:20 AM
Edited Jan 14, 2009 at 3:31 AM
Hi,

You can try this, 


ConfigurationFileMap configFileMap = new ConfigurationFileMap();

configFileMap.MachineConfigFilename = "PathOftheConfigFile";
Configuration entLibConfig = ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);
LoggingSettings loggingSettings = (LoggingSettings)entLibConfig.GetSection(LoggingSettings.SectionName);

FormattedDatabaseTraceListenerData data = loggingSettings.TraceListeners.Get("Database Trace Listener") as FormattedDatabaseTraceListenerData;

data.DatabaseInstanceName = "OtherDbConnection";

entLibConfig.Save();



Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jan 14, 2009 at 2:58 PM
Edited Jan 15, 2009 at 2:43 PM
Thanks,

But this would create a new entry in web.Config file.

I just want to change the database settings (for logging) from specified connection string in web.Config to one that i can set on runtime.

Thanks
Dev
Jan 15, 2009 at 3:16 AM
Hi,

I think it would not create a new entry but rather change the connection string(that is associated with the Database Trace Listener) and save it. 

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com