You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-user@logging.apache.org by Bart Read <ba...@red-gate.com> on 2007/05/21 12:53:11 UTC
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
RE: NullReferenceException in RollingFileAppender
Posted by Bart Read <ba...@red-gate.com>.
Hello again,
So here's the problem. I grabbed a debug log4net DLL which gave me a
more useful stack trace. This showed me that the NullReferenceException
occurs on line 557 of RollingFileAppender:
override protected void OpenFile(string fileName, bool
append)
{
lock(this)
{
fileName =
GetNextOutputFileName(fileName);
// Calculate the current size of the
file
long currentCount = 0;
if (append)
{
using(SecurityContext.Impersonate(this)) // BANG!!!!!
{
if
(System.IO.File.Exists(fileName))
{
currentCount =
(new FileInfo(fileName)).Length;
}
}
}
else
{
if (LogLog.IsErrorEnabled)
{
// Internal check that
the file is not being overwritten
// If not Appending to
an existing file we should have rolled the file out of the
// way. Therefore we
should not be over-writing an existing file.
// The only exception is
if we are not allowed to roll the existing file away.
if (m_maxSizeRollBackups
!= 0 && FileExists(fileName))
{
LogLog.Error("RollingFileAppender: INTERNAL ERROR. Append is False but
OutputFile ["+fileName+"] already exists.");
}
}
}
if (!m_staticLogFileName)
{
m_scheduledFilename = fileName;
}
// Open the file (call the base class to
do it)
base.OpenFile(fileName, append);
// Set the file size onto the counting
writer
((CountingQuietTextWriter)QuietWriter).Count = currentCount;
}
}
I should have pointed out in my previous email that I'm using version
1.2.10, which is where this source code comes from.
Anyway, SecurityContext is not set so a NullReferenceException is
thrown. I checked again through the manual on the website and couldn't
find any information on SecurityContexts, and the information in the API
documentation isn't very descriptive either, however I fixed the
exception by setting the value of SecurityContext to
NullSecurityContext.Instance. Now it's working fine, but I'm not sure
it's very satisfactory for this kind of gotcha to exist in the
framework. Anyone else got any thoughts on this?
Thanks,
Bart
________________________________
From: Bart Read [mailto:bart.read@red-gate.com]
Sent: 21 May 2007 11:53
To: log4net-user@logging.apache.org
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
RE: NullReferenceException in RollingFileAppender
Posted by Bart Read <ba...@red-gate.com>.
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: nicko@apache.org [mailto:nicko@apache.org]
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:bart.read@red-gate.com]
Sent: 21 May 2007 11:53
To: log4net-user@logging.apache.org
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
RE: NullReferenceException in RollingFileAppender
Posted by ni...@apache.org.
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:bart.read@red-gate.com]
Sent: 21 May 2007 11:53
To: log4net-user@logging.apache.org
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