logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ramaa Davanagere <RDAVA...@mobius.com>
Subject RE: What do I need to do to get rid of this error? Please help
Date Wed, 09 Nov 2005 19:07:54 GMT
 

Georg, thanks a lot for your help. Singleton pattern did the trick. 

 

-----Original Message-----
From: Georg Jansen [mailto:Georg.Jansen@FaktNet.com] 
Sent: Monday, November 07, 2005 1:23 PM
To: 'Log4NET User'
Subject: RE: What do I need to do to get rid of this error? Please help

 

Ramaa,

 

The reason for not having multiple entries with default lock, is because the
first appender would hold a lock on the file, and the other appenders would
not be able to write to it, you can easily modify my test program to give
you the same result. But running with internal debug = true, you will se
error messages form log4net telling you that the other apenders could not
write to the file due to locking.

 

Singelton:  the simples' solution for you is probably to do something like
what I have done in the new version of the testprogram (see below). This is
not exactly in accordance with the singleton pattern, but it will solve your
problem (I hope). I only use a static member variable in the class to test
if the init function has already been done, if it has I don't create a
second instance. A full implementation of a singleton will require that you
change the code every place it is used - this method does not.

 

For an explanation to singleton pattern: You can find heaps of resources if
you search on google, a few starters:

http://en.wikipedia.org/wiki/Singleton_pattern
<http://en.wikipedia.org/wiki/Singleton_pattern> 

http://www.yoda.arachsys.com/csharp/singleton.html
<http://www.yoda.arachsys.com/csharp/singleton.html> 

 

 

Best regards

Georg

http://www.l4ndash.com <http://www.l4ndash.com/>  - Log4Net Dashboard / Log
Viewer

 

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace L4NTest

{

    class Program

    {

        static void Main(string[] args)

        {

 

            MyLogger m1 = new MyLogger();

            MyLogger m2 = new MyLogger();

 

            m1.InitLog();

            m2.InitLog();

 

 

            m1.WriteToLog_Info("test message "); 

        }

    }

 

 

    class MyLogger

    {

        public log4net.ILog logger =
log4net.LogManager.GetLogger(typeof(MyLogger));

        private static bool IsInit = false;

 

        public MyLogger()

        { }

 

        public void InitLog()

        {

            if (IsInit == true) // Make sure we only creates one appender

                return;

            IsInit = true;

 

            log4net.Appender.RollingFileAppender.LockingModelBase
m_lockingType = null;

            m_lockingType = new
log4net.Appender.RollingFileAppender.MinimalLock();

            //Set the patternlayout settings

 

            log4net.Layout.PatternLayout oPatternLayout1 = new
log4net.Layout.PatternLayout();

            oPatternLayout1.ConversionPattern = "%date %-5level:
%message%newline";

            oPatternLayout1.ActivateOptions();

 

            log4net.Util.LogLog.InternalDebugging = true;

 

            //set the rollingfileappender settings

            log4net.Appender.RollingFileAppender oRollingFileAppender = new
log4net.Appender.RollingFileAppender();

            oRollingFileAppender.Name = "myRollingAppender";

            oRollingFileAppender.MaximumFileSize = "5MB";

            oRollingFileAppender.RollingStyle =
log4net.Appender.RollingFileAppender.RollingMode.Size;

            oRollingFileAppender.MaxSizeRollBackups = 20;

            oRollingFileAppender.File = "L4NTest.text";

            oRollingFileAppender.AppendToFile = true;

            oRollingFileAppender.StaticLogFileName = true;

            oRollingFileAppender.Layout = oPatternLayout1;

            oRollingFileAppender.Threshold = log4net.Core.Level.All;

            oRollingFileAppender.LockingModel = m_lockingType;

            oRollingFileAppender.ActivateOptions();

 
log4net.Config.BasicConfigurator.Configure(oRollingFileAppender);

        }

 

        public void WriteToLog_Info(string sMessage)

        {

            logger.Info(sMessage);

        }

    }

}

 

  _____  

From: Ramaa Davanagere [mailto:RDAVANAG@mobius.com] 
Sent: 7. november 2005 18:57
To: 'Log4NET User'
Subject: RE: What do I need to do to get rid of this error? Please help

 

 

Georg, I agree with what you say below. But how come I didn't see the
duplicate messages, when I changed my code to use the default locking type. 

 

Also, I would like to try the "singleton pattern" you mentioned below. Can
you give me more details on this? How do you check if the
rollingFileAppender instance is created or not?

 

-----Original Message-----
From: Georg Jansen [mailto:Georg.Jansen@FaktNet.com] 
Sent: Monday, November 07, 2005 12:49 PM
To: 'Log4NET User'
Subject: RE: What do I need to do to get rid of this error? Please help

 

Hi Ramaa,

 

You are saying that your logging component is used from 20+ other
components.

 

One reason could be that every 20+ components is creating one instance of
your logging class, this will lead to the creation of 20 instances of the
rolling file appender, and hence 20 log entries per message which is logged.

I wrote a small testprogram, just to verify that this would actually happen,
and it did. I have enclosed a copy of the test program.

 

If this is the case in your situation as well, I'll guess you need to modify
your logging class in accordance with a singleton pattern, so you make sure
you only create one RollingFileApender.

 

Best regards

Georg

http://www.l4ndash.com <http://www.l4ndash.com/>  - Log4Net Dashboard / Log
Viewer

 

 

 

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace L4NTest

{

    class Program

    {

        static void Main(string[] args)

        {

 

            MyLogger m1 = new MyLogger();

            MyLogger m2 = new MyLogger();

 

            m1.InitLog();

            m2.InitLog();

 

 

            m1.WriteToLog_Info("test message "); // This statment will write
to log entryes, due to duplicate logging objects

        }

    }

 

 

    class MyLogger

    {

        public log4net.ILog logger =
log4net.LogManager.GetLogger(typeof(MyLogger));

 

        public MyLogger()

        { }

 

        public void InitLog()

        {

            log4net.Appender.RollingFileAppender.LockingModelBase
m_lockingType = null;

            m_lockingType = new
log4net.Appender.RollingFileAppender.MinimalLock();

            //Set the patternlayout settings

 

            log4net.Layout.PatternLayout oPatternLayout1 = new
log4net.Layout.PatternLayout();

            oPatternLayout1.ConversionPattern = "%date %-5level:
%message%newline";

            oPatternLayout1.ActivateOptions();

 

            log4net.Util.LogLog.InternalDebugging = true;

 

            //set the rollingfileappender settings

            log4net.Appender.RollingFileAppender oRollingFileAppender = new
log4net.Appender.RollingFileAppender();

            oRollingFileAppender.Name = "myRollingAppender";

            oRollingFileAppender.MaximumFileSize = "5MB";

            oRollingFileAppender.RollingStyle =
log4net.Appender.RollingFileAppender.RollingMode.Size;

            oRollingFileAppender.MaxSizeRollBackups = 20;

            oRollingFileAppender.File = "L4NTest.text";

            oRollingFileAppender.AppendToFile = true;

            oRollingFileAppender.StaticLogFileName = true;

            oRollingFileAppender.Layout = oPatternLayout1;

            oRollingFileAppender.Threshold = log4net.Core.Level.All;

            oRollingFileAppender.LockingModel = m_lockingType;

            oRollingFileAppender.ActivateOptions();

 
log4net.Config.BasicConfigurator.Configure(oRollingFileAppender);

        }

 

        public void WriteToLog_Info(string sMessage)

        {

            logger.Info(sMessage);

        }

    }

}

 

 

 

 

  _____  

From: Ramaa Davanagere [mailto:RDAVANAG@mobius.com] 
Sent: 7. november 2005 15:44
To: 'Log4NET User'
Subject: RE: What do I need to do to get rid of this error? Please help

 

 

I really want somebody helping me with duplicate log statements issue rather
than talking about having a configuration file for all the settings (This is
definitely NOT CRITICAL for me RIGHT NOW). 

 

-----Original Message-----
From: Ramaa Davanagere 
Sent: Monday, November 07, 2005 9:34 AM
To: 'Log4NET User'
Subject: RE: What do I need to do to get rid of this error? Please help

 

Duplicate log statements are definitely related to appender locking type. I
reset everything back to the way it was before, to use the default locking
type, and my log looks just fine but doing so, I get into "unable to aquire
lock to the file" issue.

 

-----Original Message-----
From: Ramaa Davanagere 
Sent: Monday, November 07, 2005 9:22 AM
To: 'Log4NET User'
Subject: RE: What do I need to do to get rid of this error? Please help

 

Here is my code.....

 

using System;

using log4net;

using log4net.Config;

using System.Runtime.InteropServices;

using System.Xml;

using System.Diagnostics;

using System.IO;

using System.Text.RegularExpressions;

using System.Collections;

 

namespace companyname.productname.DotNetErrHandler

{ 

                public class DotNetErrHandler

                {

                                public ILog logger =
LogManager.GetLogger(typeof(DotNetErrHandler));

                                string sLogFilePath = "";

                                string sRootPath = "";

                                bool bEnableDebug = false;

                                bool bEnableInfo = true;

                                bool bEnableError = true;

                                bool bInternalDebugging = false;

 

 

                                //Constructor 

                                public DotNetErrHandler()       

                                {               

 

                                }

                                

                                public void InitializeErrorHandler(string
sRequestXML, string sFileName)

                                {               

                                                XmlDocument oReqDom = new
XmlDocument();

 

                                                if (sFileName.Length > 0){

 
oReqDom.Load(@sFileName);

                                                }else{

 
oReqDom.LoadXml(sRequestXML);

                                                }

                                                if (oReqDom != null){

                                                                XmlElement
oApp = (XmlElement) oReqDom.SelectSingleNode("//application");

                                                                if (oApp !=
null)

                                                                {

 
sLogFilePath = oApp.GetAttribute("logfilepath"); 

 
sRootPath = oApp.GetAttribute("rootpath");

                                                                }else{

 
sLogFilePath = "C:\\temp\\logfiles"; 

 
sRootPath = "C:\\temp";

                                                                }

 
SetAppenderProperties();

                                                }else{

                                                                //do nothing

                                                }

                                }

                                

                                private void SetAppenderProperties()

                                {

 
log4net.Appender.RollingFileAppender.LockingModelBase m_lockingType=null;

                                                m_lockingType = new
log4net.Appender.RollingFileAppender.MinimalLock();

 

                                                //Set the patternlayout
settings

                                                log4net.Layout.PatternLayout
oPatternLayout1 = new log4net.Layout.PatternLayout();

 
oPatternLayout1.ConversionPattern = "%date %-5level: %message%newline"; 

 
oPatternLayout1.ActivateOptions();

                                                

                                                XmlDocument
oLog4NetConfigDom = new XmlDocument();

 
oLog4NetConfigDom.Load(@sRootPath + "\\log4netconfig.xml");

 

                                                if (oLog4NetConfigDom !=
null){

                                                                //set the
logging level.

                                                                bEnableDebug
=
Convert.ToBoolean(oLog4NetConfigDom.SelectSingleNode("//log4netBOConfig/logg
inglevel/debug").InnerText);

                                                                bEnableInfo
=
Convert.ToBoolean(oLog4NetConfigDom.SelectSingleNode("//log4netBOConfig/logg
inglevel/info").InnerText);

                                                                bEnableError
=
Convert.ToBoolean(oLog4NetConfigDom.SelectSingleNode("//log4netBOConfig/logg
inglevel/error").InnerText);

 
bInternalDebugging =
Convert.ToBoolean(oLog4NetConfigDom.SelectSingleNode("//log4netBOConfig/logg
inglevel/internalDebugging").InnerText);

                                                                

                                                                if
(bInternalDebugging)

                                                                {

 
string sFileNameAndPath = "C:\\" + "log4netInternalDebugging_" +
System.Guid.NewGuid().ToString() + ".txt";

 
FileStream fileStream = new FileStream(sFileNameAndPath, FileMode.Create,
FileAccess.Write );  

 
if (fileStream != null && fileStream.CanWrite)  

 
{

 
System.Diagnostics.Trace.Listeners.Clear();

 
System.Diagnostics.Trace.Listeners.Add(new
TextWriterTraceListener(fileStream));

 
}

 
log4net.Util.LogLog.InternalDebugging = true;

                                                                }

 

                                                                //set the
rollingfileappender settings

 
log4net.Appender.RollingFileAppender oRollingFileAppender = new
log4net.Appender.RollingFileAppender();

 
oRollingFileAppender.Name = "myRollingAppender";

 
oRollingFileAppender.MaximumFileSize =
oLog4NetConfigDom.SelectSingleNode("//log4netBOConfig/maximumfilesize").Inne
rText;//"1000KB"; //500MB

 
oRollingFileAppender.RollingStyle =
log4net.Appender.RollingFileAppender.RollingMode.Size;

 
oRollingFileAppender.MaxSizeRollBackups =
Convert.ToInt32(oLog4NetConfigDom.SelectSingleNode("//log4netBOConfig/number
ofrollbackups").InnerText);

 
oRollingFileAppender.File = sLogFilePath + "\\" +
oLog4NetConfigDom.SelectSingleNode("//log4netBOConfig/logfilename").InnerTex
t;;                                           

 
oRollingFileAppender.AppendToFile = true;

 
oRollingFileAppender.StaticLogFileName = true;

 
oRollingFileAppender.Layout = oPatternLayout1;

 
oRollingFileAppender.Threshold = log4net.Core.Level.All;

 
oRollingFileAppender.LockingModel = m_lockingType;

 
oRollingFileAppender.ActivateOptions();


 
log4net.Config.BasicConfigurator.Configure(oRollingFileAppender);

                                                                if
(bInternalDebugging)

                                                                {

 
System.Diagnostics.Trace.AutoFlush = true;

                                                                }

                                                }else{

                                                                //do nothing

                                                }

                                }

 

                                public void WriteToLog_Info(string sMessage)

                                {                               

                                                if (bEnableInfo)

 
logger.Info(sMessage);

                                }

 

                                public void WriteToLog_Err(string
sErrorMessage)

                                {

                                                if (bEnableError)

 
logger.Error(sErrorMessage);

                                }

                                

                                public void WriteToLog_Debug(string
sDebugMessage)

                                {

                                                if (bEnableDebug)

 
logger.Debug(sDebugMessage);

                                }

                }

}

 

and here is my log4netconfig.xml

 

<log4netBOConfig>

       <maximumfilesize>3MB</maximumfilesize>

       <numberofrollbackups>20</numberofrollbackups>

       <logfilepath>C:\temp\logfiles</logfilepath>

       <logfilename>backofficelog.txt</logfilename>

       <logginglevel>

             <info>false</info>

             <debug>false</debug>

             <error>true</error>

             <internalDebugging>false</internalDebugging>

       </logginglevel>

</log4netBOConfig>

 

I can't use the config file because this component is being used as an
interop component and all the VB components (20+) use this interop component
for Error tracing and logging. Interops don't handle the config files very
well. Hence I am using an xml file (as my config file, log4netconfig.xml)
and I load this and set my rollingfile appender properties. Let me know if
you need more info.

 

Thanks.

 

-----Original Message-----
From: Shireesh Thanneru [mailto:thanneru@yahoo.com] 
Sent: Monday, November 07, 2005 9:09 AM
To: Log4NET User
Subject: RE: What do I need to do to get rid of this error? Please help

 

Why are you not using a config file? Programmatic configuration is
error-prone. Can you post your complete programmatic configuration code?

Ramaa Davanagere <RDAVANAG@mobius.com> wrote: 

 

I'm not sure if this problem is related to the change (setting the
lockingtype to minimalLock) that I just made. I am now seeing too many
duplicate log statements in my log file. This is the first time I'm seeing
this.  Here is the sample of my logs

 

2005-11-07 08:57:17,925 INFO : busTag.clsRetrieveTag.Retrieve() - Leaving

2005-11-07 08:57:17,925 INFO : busTag.clsRetrieveTag.Retrieve() - Leaving

2005-11-07 08:57:17,925 INFO : busTag.clsRetrieveTag.Retrieve() - Leaving

2005-11-07 08:57:17,925 INFO : busTag.clsRetrieveTag.Retrieve() - Leaving

2005-11-07 08:57:17,925 INFO : busTag.clsRetrieveTag.Retrieve() - Leaving

2005-11-07 08:57:17,925 INFO : busTag.clsRetrieveTag.Retrieve() - Leaving

2005-11-07 08:57:17,925 INFO : busTag.clsRetrieveTag.Retrieve() - Leaving

2005-11-07 08:57:17,925 INFO : busTag.clsRetrieveTag.Retrieve() - Leaving

2005-11-07 08:57:18,832 INFO : UILayer.modUICommon.ProcessAction() - Leaving

2005-11-07 08:57:18,832 INFO : UILayer.modUICommon.ProcessAction() - Leaving

2005-11-07 08:57:18,832 INFO : UILayer.modUICommon.ProcessAction() - Leaving

2005-11-07 08:57:18,832 INFO : UILayer.modUICommon.ProcessAction() - Leaving

2005-11-07 08:57:18,832 INFO : UILayer.modUICommon.ProcessAction() - Leaving

2005-11-07 08:57:18,832 INFO : UILayer.modUICommon.ProcessAction() - Leaving

2005-11-07 08:57:19,613 INFO : UILayer.clsUIFlow.ProcessRequest() - Leaving

2005-11-07 08:57:19,613 INFO : UILayer.clsUIFlow.ProcessRequest() - Leaving

2005-11-07 08:57:19,613 INFO : UILayer.clsUIFlow.ProcessRequest() - Leaving

2005-11-07 08:57:19,613 INFO : UILayer.clsUIFlow.ProcessRequest() - Leaving

2005-11-07 08:57:19,613 INFO : UILayer.clsUIFlow.ProcessRequest() - Leaving

2005-11-07 08:57:19,613 INFO : UILayer.clsUIFlow.ProcessRequest() - Leaving

2005-11-07 08:57:19,613 INFO : UILayer.clsUIFlow.ProcessRequest() - Leaving

2005-11-07 08:57:19,613 INFO : UILayer.clsUIFlow.ProcessRequest() - Leaving

2005-11-07 08:57:19,613 INFO : UILayer.clsUIFlow.ProcessRequest() - Leaving

 

 

Does anybody know how to solve this? Nicko, I did read this article
(http://logging.apache.org/log4net/release/manual/introduction.html#HC-71893
08
<http://logging.apache.org/log4net/release/manual/introduction.html#HC-71893
08> ) but I don't know what to do next.  Why am I seeing this behavior only
now?

 

I feel like I'm in an infinite loop here. One issue is solved, the next one
pops up and the next one and the next.....

 

Appreciate your help.

 

-----Original Message-----
From: Ramaa Davanagere 
Sent: Monday, November 07, 2005 8:15 AM
To: 'Log4NET User'
Subject: RE: What do I need to do to get rid of this error? Please help

 

I figured it out. Here it is.

 

log4net.Appender.RollingFileAppender.LockingModelBase m_lockingType=null;

m_lockingType = new log4net.Appender.RollingFileAppender.MinimalLock();

oRollingFileAppender.LockingModel = m_lockingType;

 

-----Original Message-----
From: Ramaa Davanagere 
Sent: Monday, November 07, 2005 8:07 AM
To: 'Log4NET User'
Subject: RE: What do I need to do to get rid of this error? Please help

 

I have tried

 

log4net.Appender.RollingFileAppender.LockingModelBase m_LockingType =
log4net.Appender.RollingFileAppender.MinimalLock;

oRollingFileAppender.LockingModel = m_LockingType;

 

log4net.Appender.RollingFileAppender.LockingModelBase m_LockingType =
log4net.Appender.RollingFileAppender+MinimalLock;

oRollingFileAppender.LockingModel = m_LockingType;

 

Both of them don't work. Please help me!

 

 

 

-----Original Message-----
From: Ramaa Davanagere 
Sent: Monday, November 07, 2005 7:40 AM
To: 'Log4NET User'
Subject: RE: What do I need to do to get rid of this error? Please help

 

 

How do I write this line in my code? I don't have a config file. 
  
<lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> 

This line gives me errors. 

log4net.Appender.RollingFileAppender.MinimalLock; 

-----Original Message----- 
From: Nicko Cadell [mailto:nicko@neoworks.com <mailto:nicko@neoworks.com> ] 
Sent: Sunday, November 06, 2005 4:03 PM 
To: Log4NET User 
Subject: RE: What do I need to do to get rid of this error? Please help 

By default the log4net FileAppender takes an exclusive write lock on the 
log file. If you have multiple processes that are logging to the same 
file then you need to use a different locking model. For example the 
MinimalLock model: 

<lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> 

This locking model takes a write lock on the file as each logging event 
written and then releases it immediately. 

An example configuration is: 

<appender name="FileAppender" type="log4net.Appender.FileAppender"> 
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> 
  <file value="c:\tmp\test-file.txt" /> 
  <layout type="log4net.Layout.PatternLayout" value="%d [%t] %-5p %c - 
%m%n" /> 
</appender> 

Cheers, 
Nicko 

> -----Original Message----- 
> From: Ramaa Davanagere [mailto:RDAVANAG@mobius.com
<mailto:RDAVANAG@mobius.com> ] 
> Sent: 04 November 2005 17:12 
> To: 'Log4NET User' 
> Subject: RE: What do I need to do to get rid of this error? 
> Please help 
> 
>  
> 
> I wish it was that easy!!!! 
> 
>  
> 
> I have multiple components (20+) using the same log file for 
> logging messages.  I don't think I can check which of the 
> components is currently locking the files, before writing log 
> messages. 
> 
>  
> 
> How do I resolve this? 
> 
>  
> 
> -----Original Message----- 
> From: Shireesh Thanneru [mailto:thanneru@yahoo.com
<mailto:thanneru@yahoo.com> ] 
> Sent: Friday, November 04, 2005 11:55 AM 
> To: Log4NET User 
> Subject: Re: What do I need to do to get rid of this error? 
> Please help 
> 
>  
> 
> You need to make sure that no other process/program is 
> accessing the the file at "C:\Program 
> Files\Mobius\contenuity\LogFiles\vdrconBOLog.txt" 
> 
> so that log4net can acquire a lock on that file. 
> 
> Ramaa Davanagere <RDAVANAG@mobius.com> wrote: 
> 
>        
> 
>        
> 
>       log4net: RollingFileAppender: Searched for existing 
> files in [C:\Program Files\Mobius\contenuity\LogFiles] 
> 
>       log4net: RollingFileAppender: curSizeRollBackups starts at [0] 
> 
>       log4net: FileAppender: Opening file for writing 
> [C:\Program Files\Mobius\contenuity\LogFiles\vdrconBOLog.txt] 
> append [True] 
> 
>       log4net:ERROR [RollingFileAppender] Unable to acquire 
> lock on file C:\Program 
> Files\Mobius\contenuity\LogFiles\vdrconBOLog.txt. The process 
> cannot access the file "C:\Program 
> Files\Mobius\contenuity\LogFiles\vdrconBOLog.txt" because it 
> is being used by another process. 
> 
>       log4net:ERROR [RollingFileAppender] OpenFile(C:\Program 
> Files\Mobius\contenuity\LogFiles\vdrconBOLog.txt,True) call failed. 
> 
>       LockStateException: The file is not currently locked 
> 
>          at log4net.Appender.LockingStream.AssertLocked() 
> 
>          at log4net.Appender.LockingStream.get_CanWrite() 
> 
>          at System.IO.StreamWriter..ctor(Stream stream, 
> Encoding encoding, Int32 bufferSize) 
> 
>          at System.IO.StreamWriter..ctor(Stream stream, 
> Encoding encoding) 
> 
>          at log4net.Appender.FileAppender.OpenFile(String 
> fileName, Boolean append) 
> 
>        & nbsp; at 
> log4net.Appender.RollingFileAppender.OpenFile(String 
> fileName, Boolean append) 
> 
>          at log4net.Appender.FileAppender.SafeOpenFile(String 
> fileName, Boolean append) 
> 
>       log4net: FileAppender: Opening file for writing 
> [C:\Program Files\Mobius\contenuity\LogFiles\vdrconBOLog.txt] 
> append [True] 
> 
> 


Mime
View raw message