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 Sat, 06 Jun 2009 00:24:10 GMT
As I stated in my post, it is not that the config file does not work, it does, the main problem
is that the %property method does not seem to have the ability to cast as anything other than
a string.
 
I have tried to cast it directly in the program with no success becuase the method that sets
the property in the log4net.GlobalContext.Properties sets the input as a string value.
 
But to clarify a few things that you mentioned.
 
The properties are set in the application at or durning runtime.  These properties need to
be set before the GetLogger method is called.  As long as the properties are set before the
GetLogger method (hence my two sub calls to primeLogging [which fills the application variables
with data from either the My.Settings metabase or user entered data] and configLog4Net [which
uses the aforementioned log4net.GlobalContext.Properties method to set the property values
in the Log4Net instance].  After the properties are set, the GetLogger method fires and all
of the properties are read and the Appenders are generated.
 
All of my appenders work when I strip out all of the %properties that are non string based.
 Which means that I can set the log path and the log filename dynamically but the boolean
and int64 values will fail because log4net does not cast these properties as anything other
than string values, hence the error in the stack trace:
 
Reported error follows.
System.FormatException: String was not recognized as a valid Boolean.

 

One thing you did mention was possibly creating the xml file within the application, this
is feasible (so long as log4net does not put a file lock on the config file).  Another method
I have been tinkering with (would rather not use this method, but will if must) is to generate
the appender directly in the application by using the various getters and setters for the
RollingFileAppender.

 

Thanks for you input.


 
-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: Karim Bourouba [mailto:karym6@hotmail.com]
Sent: Fri 6/5/2009 1:20 PM
To: log4net-user@logging.apache.org; log4net-dev@logging.apache.org
Subject: RE: How do I use dynamic boolean values on RollingFileAppender



The properties you are using to create the logfile name, are these being generated by log4net?
If they are, I dont think you are going to succeed, the logfile needs to be created prior
to logging commencing. The properties you seem to be supplying wont exist at the point the
you are trying to write the file.
 
Also, is the .config file the best place to be doing this? Why not simply create a file in
the code if you really need to do this? I can understand why you would want to dynamically
name a log file, but the way you are going about it probably isnt the best method.




  

________________________________

Subject: How do I use dynamic boolean values on RollingFileAppender
Date: Fri, 5 Jun 2009 10:01:18 -0500
From: rfouts@we-are-it.com
To: log4net-user@logging.apache.org; log4net-dev@logging.apache.org



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(Type type, String
value)
   at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.SetParameter(XmlElement element,
Object target)
   at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlElement 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(Type type, String
value)
   at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.SetParameter(XmlElement element,
Object target)
   at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlElement 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(Type type, String
value)
   at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.SetParameter(XmlElement element,
Object target)
   at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlElement 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(Type type, String
value)
   at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.SetParameter(XmlElement element,
Object target)
   at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlElement 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(Type type, String
value)
   at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.SetParameter(XmlElement element,
Object target)
   at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlElement 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(Type type, String
value)
   at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.SetParameter(XmlElement element,
Object target)
   at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlElement 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(Type type, String
value)
   at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.SetParameter(XmlElement element,
Object target)
   at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlElement 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(Type type, String
value)
   at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.SetParameter(XmlElement element,
Object target)
   at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlElement 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(Type type, String
value)
   at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.SetParameter(XmlElement element,
Object target)
   at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlElement 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(Type type, String
value)
   at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.SetParameter(XmlElement element,
Object target)
   at log4net.Repository.Hierarchy.XmlHierarchyConfigurator.ParseAppender(XmlElement 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().DeclaringType)
                 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.

________________________________

View your Twitter and Flickr updates from one place - Learn more! <http://clk.atdmt.com/UKM/go/137984870/direct/01/>
 



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