logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Richard Fouts" <rfo...@we-are-it.com>
Subject RE: How do I use dynamic boolean values on RollingFileAppender
Date Tue, 09 Jun 2009 13:42:46 GMT
Thanks Ron,

 

I started toying around with this type of method yesterday myself.  I
just couldn't come to grips with the final manifestation (was about to
start writing my own DynamicPattern class).  I'll give this a try and
let you know.

 

-Richard Fouts

 

Senior OS/Network Specialist

We Are IT

11040 Holmes Road

Kansas City, MO  64131

Phone: (816) 941-6800

Fax: (816) 941-4420

 

http://www.we-are-it.com/

From: Ron Grabowski [mailto:rongrabowski@yahoo.com] 
Sent: Monday, June 08, 2009 11:50 PM
To: Log4NET User
Subject: Re: How do I use dynamic boolean values on RollingFileAppender

 

This had me stumped for a while. My initial thought was to subclass
FileAppender or RollingFileAppender and override ActivateOptions. You
can't do that because the error happens during the XML configuration
processing. What you need to do is write a PatternString to Boolean
adapter for the XML processor:

public class PatternStringToBooleanAdapter : IConvertTo
{
    public string ConversionPattern { get; set; }

    public bool CanConvertTo(Type targetType)
    {
        return targetType == typeof(bool);
    }

    public object ConvertTo(object source, Type targetType)
    {
        var patternStringToBooleanAdapter =
(PatternStringToBooleanAdapter)source;

        var patternString = new
PatternString(patternStringToBooleanAdapter.ConversionPattern);

        string value = patternString.Format();

        return OptionConverter.ToBoolean(value, false);
    }
}

Register it before you process the xml file:

ConverterRegistry.AddConverter(typeof(PatternStringToBooleanAdapter),
new PatternStringToBooleanAdapter());
GlobalContext.Properties["AppendToFile"] = true;
log4net.Config.XmlConfigurator.Configure();

Then tell the XML processor that you want to intercept the normal
property setting with your adapter:

<appender name="FileAppender" type="log4net.Appender.FileAppender">
  <file value="log.txt" />
  <appendToFile type="ConsoleApplication1.PatternStringToBooleanAdapter,
ConsoleApplication1">
    <conversionPattern value="%property{AppendToFile}" />
  </appendToFile>
  <layout type="log4net.Layout.SimpleLayout" />
</appender>

 

________________________________

From: Richard Fouts <rfouts@we-are-it.com>
To: log4net-user@logging.apache.org; log4net-dev@logging.apache.org
Sent: Friday, June 5, 2009 11:01:18 AM
Subject: How do I use dynamic boolean values on RollingFileAppender

I have ran into a "small" brick wall.  Hoping that I can find a couple
of log4net guru's here.  My situation.

In trying to make my logging as configurable as possible in my
application, I have been tinkering with trying to set the boolean values
for appendToFile and staticLogFileName as dynamic values.  These have a
default value (currently stored in the My.Settings metadata).  My
problem is that when I go to configure the values at runtime I get this
error in my stack trace:

 

 

log4net:ERROR XmlHierarchyConfigurator: Could not create Appender
[SyncLogFile] of type [log4net.Appender.RollingFileAppender]. Reported
error follows.
System.FormatException: String was not recognized as a valid Boolean.
   at System.Boolean.Parse(String value)
   at log4net.Util.TypeConverters.BooleanConverter.ConvertFrom(Object
source)
   at log4net.Util.OptionConverter.ConvertStringTo(Type target, String
txt)
   at
log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ConvertStringTo(Ty
pe type, String value)
   at
log4net.Repository.Hierarchy.XmlHierarchyConfigurator.SetParameter(XmlEl
ement element, Object target)
   at
log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlE
lement appenderElement)
log4net:ERROR XmlHierarchyConfigurator: Appender named [SyncLogFile] not
found.
log4net:ERROR XmlHierarchyConfigurator: Could not create Appender
[ErrLogFile] of type [log4net.Appender.FileAppender]. Reported error
follows.
System.FormatException: String was not recognized as a valid Boolean.
   at System.Boolean.Parse(String value)
   at log4net.Util.TypeConverters.BooleanConverter.ConvertFrom(Object
source)
   at log4net.Util.OptionConverter.ConvertStringTo(Type target, String
txt)
   at
log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ConvertStringTo(Ty
pe type, String value)
   at
log4net.Repository.Hierarchy.XmlHierarchyConfigurator.SetParameter(XmlEl
ement element, Object target)
   at
log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlE
lement appenderElement)
log4net:ERROR XmlHierarchyConfigurator: Appender named [ErrLogFile] not
found.
log4net:ERROR XmlHierarchyConfigurator: Could not create Appender
[SyncLogFile] of type [log4net.Appender.RollingFileAppender]. Reported
error follows.
System.FormatException: String was not recognized as a valid Boolean.
   at System.Boolean.Parse(String value)
   at log4net.Util.TypeConverters.BooleanConverter.ConvertFrom(Object
source)
   at log4net.Util.OptionConverter.ConvertStringTo(Type target, String
txt)
   at
log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ConvertStringTo(Ty
pe type, String value)
   at
log4net.Repository.Hierarchy.XmlHierarchyConfigurator.SetParameter(XmlEl
ement element, Object target)
   at
log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlE
lement appenderElement)
log4net:ERROR XmlHierarchyConfigurator: Appender named [SyncLogFile] not
found.
log4net:ERROR XmlHierarchyConfigurator: Could not create Appender
[ErrLogFile] of type [log4net.Appender.FileAppender]. Reported error
follows.
System.FormatException: String was not recognized as a valid Boolean.
   at System.Boolean.Parse(String value)
   at log4net.Util.TypeConverters.BooleanConverter.ConvertFrom(Object
source)
   at log4net.Util.OptionConverter.ConvertStringTo(Type target, String
txt)
   at
log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ConvertStringTo(Ty
pe type, String value)
   at
log4net.Repository.Hierarchy.XmlHierarchyConfigurator.SetParameter(XmlEl
ement element, Object target)
   at
log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlE
lement appenderElement)
log4net:ERROR XmlHierarchyConfigurator: Appender named [ErrLogFile] not
found.
log4net:ERROR XmlHierarchyConfigurator: Could not create Appender
[SyncLogFile] of type [log4net.Appender.RollingFileAppender]. Reported
error follows.
System.FormatException: String was not recognized as a valid Boolean.
   at System.Boolean.Parse(String value)
   at log4net.Util.TypeConverters.BooleanConverter.ConvertFrom(Object
source)
   at log4net.Util.OptionConverter.ConvertStringTo(Type target, String
txt)
   at
log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ConvertStringTo(Ty
pe type, String value)
   at
log4net.Repository.Hierarchy.XmlHierarchyConfigurator.SetParameter(XmlEl
ement element, Object target)
   at
log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlE
lement appenderElement)
log4net:ERROR XmlHierarchyConfigurator: Appender named [SyncLogFile] not
found.
log4net:ERROR XmlHierarchyConfigurator: Could not create Appender
[ErrLogFile] of type [log4net.Appender.FileAppender]. Reported error
follows.
System.FormatException: String was not recognized as a valid Boolean.
   at System.Boolean.Parse(String value)
   at log4net.Util.TypeConverters.BooleanConverter.ConvertFrom(Object
source)
   at log4net.Util.OptionConverter.ConvertStringTo(Type target, String
txt)
   at
log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ConvertStringTo(Ty
pe type, String value)
   at
log4net.Repository.Hierarchy.XmlHierarchyConfigurator.SetParameter(XmlEl
ement element, Object target)
   at
log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlE
lement appenderElement)
log4net:ERROR XmlHierarchyConfigurator: Appender named [ErrLogFile] not
found.
log4net:ERROR XmlHierarchyConfigurator: Could not create Appender
[SyncLogFile] of type [log4net.Appender.RollingFileAppender]. Reported
error follows.
System.FormatException: String was not recognized as a valid Boolean.
   at System.Boolean.Parse(String value)
   at log4net.Util.TypeConverters.BooleanConverter.ConvertFrom(Object
source)
   at log4net.Util.OptionConverter.ConvertStringTo(Type target, String
txt)
   at
log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ConvertStringTo(Ty
pe type, String value)
   at
log4net.Repository.Hierarchy.XmlHierarchyConfigurator.SetParameter(XmlEl
ement element, Object target)
   at
log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlE
lement appenderElement)
log4net:ERROR XmlHierarchyConfigurator: Appender named [SyncLogFile] not
found.
log4net:ERROR XmlHierarchyConfigurator: Could not create Appender
[ErrLogFile] of type [log4net.Appender.FileAppender]. Reported error
follows.
System.FormatException: String was not recognized as a valid Boolean.
   at System.Boolean.Parse(String value)
   at log4net.Util.TypeConverters.BooleanConverter.ConvertFrom(Object
source)
   at log4net.Util.OptionConverter.ConvertStringTo(Type target, String
txt)
   at
log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ConvertStringTo(Ty
pe type, String value)
   at
log4net.Repository.Hierarchy.XmlHierarchyConfigurator.SetParameter(XmlEl
ement element, Object target)
   at
log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlE
lement appenderElement)
log4net:ERROR XmlHierarchyConfigurator: Appender named [ErrLogFile] not
found.
log4net:ERROR XmlHierarchyConfigurator: Could not create Appender
[SyncLogFile] of type [log4net.Appender.RollingFileAppender]. Reported
error follows.
System.FormatException: String was not recognized as a valid Boolean.
   at System.Boolean.Parse(String value)
   at log4net.Util.TypeConverters.BooleanConverter.ConvertFrom(Object
source)
   at log4net.Util.OptionConverter.ConvertStringTo(Type target, String
txt)
   at
log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ConvertStringTo(Ty
pe type, String value)
   at
log4net.Repository.Hierarchy.XmlHierarchyConfigurator.SetParameter(XmlEl
ement element, Object target)
   at
log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlE
lement appenderElement)
log4net:ERROR XmlHierarchyConfigurator: Appender named [SyncLogFile] not
found.
log4net:ERROR XmlHierarchyConfigurator: Could not create Appender
[ErrLogFile] of type [log4net.Appender.FileAppender]. Reported error
follows.
System.FormatException: String was not recognized as a valid Boolean.
   at System.Boolean.Parse(String value)
   at log4net.Util.TypeConverters.BooleanConverter.ConvertFrom(Object
source)
   at log4net.Util.OptionConverter.ConvertStringTo(Type target, String
txt)
   at
log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ConvertStringTo(Ty
pe type, String value)
   at
log4net.Repository.Hierarchy.XmlHierarchyConfigurator.SetParameter(XmlEl
ement element, Object target)
   at
log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlE
lement appenderElement)
log4net:ERROR XmlHierarchyConfigurator: Appender named [ErrLogFile] not
found.

The way that I am configuring the values is by trying to use the code
displayed here:

 

	
'In Main
         Private Shared Log As ILog
 
         Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
                 '[...]
                 primeLogging()
                 configLog4Net()
                 Log =
LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().Dec
laringType)
                 Log.Info("This is the start of the application.")
         End Sub
 
         Private Sub primeLogging()
                 If Not My.Settings.LogPath = "" Then
                          _LogPath = My.Settings.LogPath
                 Else
                          _LogPath = My.Application.Info.DirectoryPath &
"\Logs\"
                 End If
 
                
 '[...]
 
                 _MaxRollBacks = My.Settings.MaxRollBacks
                 _MaxFileSize = My.Settings.MaxFileSize
 
                 _isAppendToFile = My.Settings.isAppendToFile
                 _isStaticLogFile = My.Settings.isStaticLogFile
         End
 Sub
 
         Private Sub configLog4Net()
                 log4net.GlobalContext.Properties("LogPath") = _LogPath
                 log4net.GlobalContext.Properties("LogName") = _LogName
                 log4net.GlobalContext.Properties("RollStyle") =
_tempRollStyle
                
 log4net.GlobalContext.Properties("MaxRollBacks") = _MaxRollBacks
                 log4net.GlobalContext.Properties("MaxFileSize") =
_MaxFileSize
                 log4net.GlobalContext.Properties("isAppendToFile") =
_isAppendToFile
                 log4net.GlobalContext.Properties("isStaticLogFile") =
_isStaticLogFile
                 'My attempts to cast as a boolean (did not work)
 
'log4net.Util.OptionConverter.ToBoolean(log4net.GlobalContext.Properties
("isAppendToFile"), _isAppendToFile)
 
'log4net.Util.OptionConverter.ToBoolean(log4net.GlobalContext.Properties
("isStaticLogFile"), _isStaticLogFile)
         End Sub
 
'In Log4Net.config (using a seperate configuration file for Log4Net)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
         <configSections>
                 <section name="log4net"
type="System.Configuration.IgnoreSectionHandler" />
         </configSections>
         <appSettings>
                 <!-- To enable internal log4net logging specify the
following appSettings key -->
                 <add key="log4net.Internal.Debug"
 value="true"/>
         </appSettings>
         <!-- This section contains the log4net configuration settings
-->
         <log4net>
                 <!-- Define some output appenders -->
                 <appender name="SyncLogFile"
type="log4net.Appender.RollingFileAppender">
                          <file type="log4net.Util.PatternString"
value="%property{LogPath}\Syncronizations\%property{LogName}" />
                          <param name="DatePattern"
value=".yyyy-MM-dd-tt.log"/>
                          <appendToFile
value="%property{isAppendToFile}" />
                          <maxSizeRollBackups
type="log4net.Util.PatternString" value="%property{MaxRollBacks}" />
                          <maximumFileSize
type="log4net.Util.PatternString" value="%property{MaxFileSize}" />
                          <rollingStyle
type="log4net.Util.PatternString" value="%property{RollStyle}" />
                          <staticLogFileName
value="%property{isStaticLogFile}" />
                          <layout type="log4net.Layout.PatternLayout">
                                   <header
type="log4net.Util.PatternString"
value="**************************************** Logging Started - %date
******************************************&#13;&#10;" />
                                   <footer
type="log4net.Util.PatternString"
value="**************************************** Logging Ended - %date
******************************************&#13;&#10;" />
                                   <conversionPattern value="%date
[%thread] %-5level %logger [%ndc] - %message%newline" />
                         
 </layout>
                 </appender>
                 <appender name="ErrLogFile"
type="log4net.Appender.FileAppender">
                          <file type="log4net.Util.PatternString"
value="%property{LogPath}\Errors\errLog.txt" />
                          <sppendToFile
value="%property{isAppendToFile}" />
                          <layout type="log4net.Layout.PatternLayout">
                                   <header
type="log4net.Util.PatternString"
value="**************************************** Logging Started - %date
******************************************&#13;&#10;" />
                                   <footer
type="log4net.Util.PatternString"
 value="**************************************** Logging Ended - %date
******************************************&#13;&#10;" />
                                   <conversionPattern value="%date
[%thread] %-5level %logger [%ndc] &lt;%property{auth}&gt; -
%message%newline" />
                          </layout>
                 </appender>
                 <appender name="ConsoleLog"
type="log4net.Appender.ConsoleAppender">
                          <layout type="log4net.Layout.PatternLayout">
                                   <conversionPattern value="%date
[%thread] %-5level %logger [%ndc] &lt;%property{auth}&gt; -
%message%newline" />
                          </layout>
                 </appender>
                 <appender name="EventLog"
type="log4net.Appender.EventLogAppender">
                          <layout type="log4net.Layout.PatternLayout">
                                   <conversionPattern value="%date
[%thread] %-5level %logger [%ndc] &lt;%property{auth}&gt; -
%message%newline" />
                          </layout>
                 </appender>
                 <!-- Setup the root category, add the appenders and set
the default level -->
                 <root>
                          <level value="ALL" />
                          <appender-ref ref="SyncLogFile" />
                          <appender-ref ref="ErrLogFile" />
                          <appender-ref ref="ConsoleLog" />
                          <appender-ref ref="EventLog" />
                 </root>
         </log4net>
</configuration>



All of my other appenders work (ConsoleLog, EventLog and ErrLogFile
(when I remove the appendToFile paramter).

Please any assistance would be most appreciated.

Thanks,

-saige-

 
Do you have a disaster recovery plan for your servers? If not, ask about
We Are 
IT's Total Rescue business continuity plan.



Do you have a disaster recovery plan for your servers? If not, ask about We Are 
IT's Total Rescue business continuity plan.
Mime
View raw message