tags:

views:

2470

answers:

3

I am trying to find somebody smarter than me to validate some syntax I wrote up. The idea is to configure the filename of my RollingFileAppender to the name of the assembly in order to make it more re-usable for my projects.

I've seen this previous SO article but it wasn't exactly able to answer my question...

I've had a dickens of a time trying to understand the inner components of Log4net and this is what I came up with (residing in the Global.asax file - Application_Start method):

// Bind to the root hierarchy of log4net
log4net.Repository.Hierarchy.Hierarchy root = 
  log4net.LogManager.GetRepository() 
    as log4net.Repository.Hierarchy.Hierarchy;

if (root != null)
{
  // Bind to the RollingFileAppender
  log4net.Appender.RollingFileAppender rfa = 
    (log4net.Appender.RollingFileAppender)root.Root.GetAppender("RollingLogFileAppender");

  if (rfa != null)
  {
    // Set the file name based on the assembly name
    string filePath = 
      string.Format("~/App_Data/{0}.log", GetType().Assembly.GetName().Name);

    // Assign the value to the appender
    rfa.File = Server.MapPath(filePath);

    // Apply changes to the appender
    rfa.ActivateOptions();
  }
}

Can anyone tell me, 'this is hideous', or 'this should work fine'? Also, if I set the file dynamically can I still expect the log4net behavior to rotate the files based on the log4net.config file settings?

Much appreciated!

+6  A: 

You are doing this the hard way! Define your log4net config as XML in your application's configuration file and use %property{} to advantage:

<appender name="YourAppender" type="log4net.Appender.RollingFileAppender">
  <file type="log4net.Util.PatternString" value="~/App_Data/%property{LogName}" />
  ....
</appender>

This is dynamic -- you just have to set the log4net property "LogName" before you initialize log4net. Thus, in your code any time before you configure log4net, set the desired value of this property:

string LogName = GetType().Assembly.GetName().Name + ".log";
log4net.GlobalContext.Properties["LogName"] = LogName;

Of course, you may use any property name. I've chosen "LogName" for a simple example, but you can have one per application if you want, as long as your code knows what the correct property name is and what the correct value should be.

Eddie
Ah, exactly why I posted this... It seemed more difficult than it should have been. Thank you...
Dscoduc
Do you know if there is an equivalent method or way to do this when using the log4netfactoryadapter? I am using common.logging in all my code and declaratively defining the configuration (app.config). I have tried the GlobalContext method but end up with (null) in my file name, and I know the variable I'm using is not null.
Kyle LeNeau
Unfortunately, I'm not familiar with the log4netfactoryadapter. Are you sure you're using GlobalContext ** before ** log4net is initialized or used? If log4net gets used or initialized before you define your properties, they'll be null.
Eddie
A: 

Hi, I have been doing the same using %property{}, but the file is created as "(nul).log" below is the code

log4net.GlobalContext.Properties["service"] = _servicename.ToString(); _flatFileLogger = LogManager.GetLogger("FlatFileLogger"); _flatFileLogger.Debug(logData.ToString());

I have declared in the XML file as

raaga_sai
A: 

Thanks so much Eddie. Your tip for me and I am able to get exactly what i need.

Sujatha