views:

33

answers:

3

Hello all.

I'm trying to create a setup in my ASP.NET website that allows me to enable/disable logging while the application is running at any point. I figure some of you have already done such a thing.

Here's the gist of what i'm trying to do:

if(ShouldBeLogging)
logger.Info("helloooooo there");

So how would you set up the boolean value ShouldBeLogging to be able to be turned on/off while the website is running without getting any serious performance drawbacks(seeing how its going to be accessed frequently)?

I was thinking about putting something in the web.config, but wouldn't a change to that kick my user sessions if i wanted to turn it on?

Thanks!

A: 

Yes, a change to the web.config will result in the application restarting itself, and session being lost depending on your session store.

The performance overhead of a boolean check is minimum, but its a maintenance nightmare.

Why don't you just use Enterprise Library Logging Application Block, which is already externally configurable? A reference for what you are trying to do would be here Checking Filter Status before Logging

If you decide you really want to toggle logging via if checks a simple psuedo solution is to add a value to your web.config appSettings

<add key <appSettings>
  <add key="DiagnosticLogging" value="true" />
</appSettings>

And then in your Global Page public static readonly Boolean LoggingEnabled { get;}

  Application_Start(..){
         string log = ConfigurationSettings.AppSettings["DiagnosticLogging"];
         LoggingEnabled  = false;
         if(!string.IsNullOrEmpty(log)){
             if(!boolean.TryParse(out LoggingEnabled )){
                   //bad application setting.. handle
              }   
         }      
  }
Nix
A: 

The built in diagnostics/tracing might be the best fit for your needs, i.e. see http://www.beansoftware.com/ASP.NET-Tutorials/Tracing-ASP.NET.aspx

sample for web.config:

<configuration>
    <appSettings/>
    <connectionStrings/>
    <system.web>
        <compilation debug="false" />
        <authentication mode="Windows" />
      <trace enabled ="true" pageOutput ="false" requestLimit ="20" traceMode ="SortByTime " /> 


</system.web>

BrokenGlass
A: 

I decided to go with log4net which supports this exact functionality. You can put a line in your assemblyinfo.cs like:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "config\\log4net.config", Watch = true)]

which will tell it to watch the config file for changes. then you can set the level node in the config xml at any time during the program's execution and the logging will be turned on/off, as long as you're using the following checks in your code:

if(log.IsDebugEnabled)
    log.Debug("write a debug message to the logger");

see http://logging.apache.org/log4net/release/manual/internals.html for more info.

Micah