Monday, May 21, 2012
 
The best way to analyze your logs! Minimize
 Log4Net Mail archive   

The Log4Net mailing list is a great source of information about using log4Net, in this forum we collect all the messages in the log4net user list and some selected threads from the developer list.

Subject: Defer file creation
Prev Next
You are not authorized to post a reply.

Author Messages
Wolfgang Trog

07/31/2009 4:15 PM  

Hi Everbody,

 

I’m looking for a way to defer the creation of the log file until the first entry is logged. I want to use log4net for writing protocols for jobs that can be executed in my application. With my current configuration the file is created during startup which makes no sense if the job doesn’t run. 

 

My current configuration:

 

      <appender name="" type="log4net.Appender.RollingFileAppender">

        <file value="....." />

        <appendToFile value="false" />

        <rollingStyle value="Date" />

        <datePattern value="yyyy_MM_dd.\tx\t" />

        <staticLogFileName value="false" />

        <threshold value="INFO" />

        <layout type="log4net.Layout.PatternLayout">

          <conversionPattern value="%date;%level;%m%n" />

        </layout>

      </appender>

 

One possibility seems to be to generate the appender programmatically, but then I lose a lot of flexibility. Another one is writing my own appender (or subclassing an existing one). Is there another way ?

 

Any help is appreciated,

Wolfgang

 

Karim Bourouba

07/31/2009 5:05 PM  

Hi There,

 

I dont think I have an answer to your question just now - but can I ask why you would prefer to defer creating the log file?


Wolfgang Trog

07/31/2009 5:14 PM  

I want to create protocols for jobs that can be executed in our application, preferably one file per job and execution. But I don’t want a file, if the job is not executed. The jobs are only executed once a month for example.

 

Thx for the quick response !

 

Wolfgang

 

Wolfgang Trog

07/31/2009 5:23 PM  

For now, I  tried a quick hack and modified FileAppender and it’s now working like I want:

 

override public void ActivateOptions()

        {

           

 

            if ( m_fileName != null )

            {

                //SafeOpenFile( m_fileName, m_appendToFile );

            }

            else

            {

                LogLog.Warn( "FileAppender: File option not set for appender [" + Name + "]." );

                LogLog.Warn( "FileAppender: Are you using FileAppender instead of ConsoleAppender?" );

            }

        }

 

I would prefer a solution, where I don’t have to use my own appender, but at least I have a workaround.

 

Regards,
Wolfgang

Ross Hinkley

07/31/2009 6:02 PM  
Off the top of my head: doesn't the logfile only get generated if the logger is configured?  Rather than deferring the log file getting created, couldn't you defer execution of the configurator?

You could use a getter to run the configurator, like the following:

private static ILog _log = LogManager.GetLogger(typeof(Program));
public static ILog log
{
    get
    {
        if(!log4net.LogManager.GetRepository().Configured)
            log4net.Config.XmlConfigurator.Configure(new FileInfo(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile));
        return _log;
    }
}

This way, your logger will only get configured (and the file subsequently made) if something is logged.

Is it a hack?  Absolutely.  But it's the quickest way I could think of to answer your question. :)

-Ross
Wolfgang Trog

08/03/2009 8:43 AM  

Hi Ross,

 

thx for the suggestion. I can’t defer the logger configuration, because I need basic logging from the beginning. I could probably use a separate log file for each job, but that seems a bit awkward. I think I’ll stick to the “write my own appender” approach for the moment.

 

Thx,

Wolfgang

 

Karim Bourouba

08/03/2009 9:00 AM  

Hi Wolfgang,

 

sorry, but I havent really been paying much attention to this thread I am afraid. Is it not possible for you to write your log to a DB and then create logfiles from that? It seems a bit long-winded, but could be a solution as opposed to creating your own appender.

Wolfgang Trog

08/03/2009 9:19 AM  

Karim, no worries. About the DB … in fact I want to use the flexibility of log4net, to decide where the logs should go  (file and/or database), to change the log level on the fly and to separate activities form trace information. So it’s much easier for me, to change the appender (where I simply have to comment one line) than to write the code for writing it to the database and then to a log file.

 

Thx,

Wolfgang

Ross Hinkley

08/03/2009 2:19 PM  
Wolfgang,

If you're separating trace information from activity information, then yes, my (somewhat trite) solution is insufficient.  As noted, it is a bit of a hack.  The custom appender solution is probably preferable.

Are you using the same logfile for every job?  That seems a touch awkward to me (more so than one per job), but maybe for your implementation it fits.

After these jobs are run and the log files are written, what's your process after that?  Do you go in and look to see if any files were made, or is there some automated notification process?  If it's the latter, I really like Karim's idea - presumably, you'd have some job set up to let you know there were log files; why not simply run a database report instead?

-Ross
Wolfgang Trog

08/04/2009 10:35 AM  

Hi Ross, all

 

some more details about my setup:

 

-          WinForm application

-          Use of Windows Workflow Foundation for customer specific jobs

 

We use log4net for “normal” application logging. Now I need a solution for generating protocols for the jobs (workflows), meaning, which action were made, errors, and so on. These jobs run maybe once a week, a month or so. Each job should get its own protocol file. My decision for using log4net for that, too, was primarily based on the flexibility log4net offers for defining what gets logged and where. I can activate tracing for these jobs on the fly and I can switch to a different appender and write everything to the database. Currently there is no real process in “analyzing” the protocols. The customers simply want them and sometimes we need them for debugging.

 

I’m fine with using my “own” appender, I was just looking if I missed some small configuration value or something like that.

 

Thx to all for the suggestions and quick responses,

Wolfgang

 

Karim Bourouba

08/04/2009 10:57 AM  
Hi Wolfgang,
 
have you considered look at the common logging infrastructure at all? It can work with Log4Net and could be quite useful for you.
 
BR
-
Karim
Wolfgang Trog

08/04/2009 11:01 AM  

Not really, since we also use NHibernate, we didn’t look at anything else. But I’ll give it a try. Thx for the hint.

 

Wolfgang

 

Karim Bourouba

08/04/2009 11:25 AM  
My only reason for suggesting it is that you can use it to connect different logging frameworks together. This could be useful for you with the system you are proposing.


You are not authorized to post a reply.
Forums > Log4Net > Log4Net Mail archive > Defer file creation



ActiveForums 3.7

 

 

 

 

 

 

 

 

Log4Net Dashboard

Log analysis and monitoring made easy!

Log4Net Dashboard is a log viewer that can read log statements from a variety of logging output targets.

You can download a free developer version.

  

Check it out!

On the demonstration site you can try it  with live data.demo.l4ndash.com - Try Log4Net Dashboard with live data

The mail archive is a copy of all the mail sent to the mail address: log4net-user@logging.apache.org, organized as a forum.

If you would like to participate in the mail list, send a mail to log4net-user-subscribe@logging.apache.org.

More information about the mailing list is available on: http://logging.apache.org/log4net/support.html

 

A complete topic list is available and can be viewed here (warning, it takes some time to load)

 

Copyright 2005-2008 by FaktNet AS Terms Of Use Privacy Statement