2

I have made my own HttpModule called 'JsonLogger' for logging json requests and responses.

For the logging I need a connectionstring.

To find the connectionstring, I added one in my web.config and I get it by code.

web.config

<connectionStrings>
   <add name="WebShopConfiguration" connectionString="myConString"/>
</connectionStrings>

<system.web>
   <httpModules>
     <add name="JsonLogger" type="ServiceModel.Logging.JsonLogger, ServiceModel"/>
    </httpModules>
</system.web>

<system.webServer>
   <modules>
      <add name="JsonLogger" type="ServiceModel.Logging.JsonLogger, ServiceModel"/>
   </modules>
</system.webServer>

HttpModule

ConnectionString = System.Configuration.ConfigurationManager.
            ConnectionStrings["WebShopConfiguration"].ConnectionString;

Everything works fine but imo this isn't clean (future developers won't know why that connectionstring is there in my web.config).

I would like to fill in the connectionstring where I add the httpmodule in my web.config. I tried this but this isn't working (like I already thought):

web.config

<add name="JsonLogger" type="ServiceModel.Logging.JsonLogger, ServiceModel" ConnectionString="myConString"/>

Property in HttpModule

[ConfigurationProperty("ConnectionString", DefaultValue = "", IsRequired = true, IsKey = false)]
public string ConnectionString { get; set; }

After some research I still don't find a way to do this. Anyone knows if what I'm trying to achieve is possible?

Many thanks, Kjell

Kjell Derous
  • 502
  • 1
  • 6
  • 19

1 Answers1

1

I made it work.

In my web.config I added a configSection with the type LoggingConfiguration. In the global.asax init() I get my configuration and create a new JsonLogger instance with the correct parameters.

web.config

<configSections>    
    <section name="LoggingConfiguration" type="WebServices.Services.Configurations.LoggingConfiguration"/>
</configSections>

<LoggingConfiguration
connectionString="myConString">
</LoggingConfiguration>

LoggingConfiguration.cs

public sealed class LoggingConfiguration : ConfigurationSection
{
    private const string ConnectionStringKey = "connectionString";

    [ConfigurationProperty(ConnectionStringKey, IsRequired = true)]
    public string ConnectionString
    {
        get { return (string) base[ConnectionStringKey]; }
        set { base[ConnectionStringKey] = value; }
    }        
}

Global.asax.cs

public override void Init()
{
    base.Init();
    var loggingConfiguration = ConfigurationManager.GetSection("LoggingConfiguration") as LoggingConfiguration;
    if (loggingConfiguration != null)
    {               
        new JsonLogger(loggingConfiguration.ConnectionString).Init(this); 
    }
}

This way I could remove following code in my web.config:

<connectionStrings>
   <add name="WebShopConfiguration" connectionString="myConString"/>
</connectionStrings>

<system.web>
   <httpModules>
     <add name="JsonLogger" type="ServiceModel.Logging.JsonLogger, ServiceModel"/>
    </httpModules>
</system.web>

<system.webServer>
   <modules>
      <add name="JsonLogger" type="ServiceModel.Logging.JsonLogger, ServiceModel"/>
   </modules>
</system.webServer>
Kjell Derous
  • 502
  • 1
  • 6
  • 19