Brilliant, thanks Nicko. I've now got:

 

PatternLayout   layout                  = new PatternLayout(
"%utcdate{dd MMM yyyy HH:mm:ss,fff} [%thread] %-5level %logger -
%message%newline" );

appender.Layout                                 = layout;

 

layout.ActivateOptions();

appender.ActivateOptions();

 

BasicConfigurator.Configure( appender );

 

and it seems to be working fine. Really appreciate the quick response.

 

 

Cheers,

Bart

 

 

 

________________________________

From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 
Sent: 21 May 2007 12:28
To: Log4NET User
Subject: RE: NullReferenceException in RollingFileAppender

 

Bart,

 

You need to call the ActivateOptions method on the PatternLayout and on
the RollingFileAppender before calling  BasicConfigurator.Configure.

 

 

http://logging.apache.org/log4net/release/sdk/log4net.Appender.RollingFi
leAppender.ActivateOptions.html

 

 

Cheers,

Nicko

 

------------
Nicko Cadell
log4net development
http://logging.apache.org/log4net

 

From: Bart Read [mailto:[EMAIL PROTECTED] 
Sent: 21 May 2007 11:53
To: [email protected]
Subject: NullReferenceException in RollingFileAppender

 

Hi everyone,

 

 

I'm trying to do something which (I hope) is fairly simple, however in
the output from my application it seems that the RollingFileAppender
always throws a NullReferenceException in its OpenFile method. Here's
the exception output:

 

log4net:ERROR [RollingFileAppender] OpenFile(C:\Documents and
Settings\bart.read\Local Settings\Application Data\Red
Gate\ourapp\mylogfile.txt,True) call failed.

System.NullReferenceException: Object reference not set to an instance
of an object.

   at log4net.Appender.RollingFileAppender.OpenFile(String fileName,
Boolean append)

   at log4net.Appender.FileAppender.SafeOpenFile(String fileName,
Boolean append)

 

And here's a snippet of the code I use to initialise the logging:

 

                public void InitialiseLogging()

                {

                        if ( null != m_Filename )

                        {

                                return;

                        }

 

                        string  folder  =
StorageFolders.GetOptionsFolderPath();

                        try

                        {

                                if ( ! Directory.Exists( folder ) )

                                {

                                        Directory.CreateDirectory(
folder );

                                }

                        }

                        catch ( IOException ) {}

                        catch ( UnauthorizedAccessException ) {}

 

                        m_Filename      = Path.Combine( folder,
"mylogfile.txt" );

 

                        RollingFileAppender     appender        = new
RollingFileAppender();

                        appender.File
= m_Filename;

                        appender.LockingModel                   = new
FileAppender.MinimalLock();

                        appender.ImmediateFlush                 = true;

                        appender.StaticLogFileName              = true;

 

                        EngineOptions   options                 =
EngineOptions.GetGlobalOptions();

                        appender.MaxFileSize                    =
options.LogFileMaxSizeK * 1024;

                        appender.MaxSizeRollBackups             =
options.LogFileMaxBackups;

                        appender.Threshold
= options.LoggingLevel;

                        appender.Layout
= new PatternLayout( "%utcdate{dd MMM yyyy HH:mm:ss,fff} [%thread]
%-5level %logger - %message%newline" );

 

                        BasicConfigurator.Configure( appender );

 

                        ILog    logger  = GetLogger( this );

                        if ( logger.IsInfoEnabled )

                        {

                                logger.Info( "Starting new ourapp
session" );

                        }

                }

 

You'll note that I'm not using an XML file for configuration. This is
deliberate as it's not something we want exposed to our users.

 

I'm assuming there's a problem with the above code, although there's
nothing obviously wrong that I can see. I've googled, checked the API
documentation and the manual and have so far drawn a blank so if anyone
has any idea what's going on I'd be extremely grateful.

 

Note that

 

(i)               INFO logging is enabled,

(ii)              the exception is thrown during the call to
ILog.Info(),

(iii)            StorageFolders.GetOptionsFolderPath() returns a folder
that does exist and to which I do have write access (I'm already storing
other items there),

(iv)            RollingFileAppender.MaxFileSize is set to 8K,

(v)             RollingFileAppender.MaxSizeRollBackups is set to 6.

 

As I said, any help would be greatly appreciated.

 

 

Many thanks,

Bart

 

 

Reply via email to