« Return to Thread: RE: How do I use dynamic boolean values on RollingFileAppender

RE: How do I use dynamic boolean values on RollingFileAppender

by Richard Fouts :: Rate this Message:

Reply to Author | View in Thread

Some parts of this message have been removed. Learn more about Nabble's security policy.

You mention in the best spirit of .Net, could you provide a little more clarification?  I tend to feel that an assembly, log4net.dll in this example, should be a black box, and how you interact with this black box, should not affect the underlying codes buildability or stability.  The mere fact that you can use a separate configuration file for the log4net assembly attests to this ability.  There are also others around the internet who would tend to agree with my use of an external configuration file.

 

You also mention the loss of flexibility, please expand upon this.  To give a better understanding, using an application, how do you know where the log files are stored and what they are named?  An application such as Exchange or SQL will show you where the log files are (so far as you know where to look inside the application interface), and not only that, will allow for the end user to control some of the basic settings of the log file [where it is stored, what it is named, will it be a rolling log file (circular logging), etc. etc.].  If I am unable to use a configuration file (regardless of app.config or log4net.config) to set these values then my only recourse will be to programmatically control the appender.  What are some pro’s and con’s you can think of?  What functionality will I lose or gain?

 

-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@...]
Sent: Saturday, June 06, 2009 2:16 AM
To: log4net-user@...; log4net-dev@...
Subject: RE: How do I use dynamic boolean values on RollingFileAppender

 

I am not saying that the config file doesnt work, I was just suggesting that the way you are trying to use the file may not be in the best spirit of .Net.

You can control the configuration of log4net programatically, but you will loose some of the flexibility that you need (going by the code and config you mailed earlier). I have installatations that use this type of appender, but I am happy enough specifiying the name of the logfile in the config file itself, but again I can fully understand wanting to have this dynamically created.
 
The last point you mention, about creating the xml, yes its a nice idea. .Net will lock the apps .config file though during execution, so you would need to generate a .config to a seperate location and have the app.config look this up etc. Once the xml has been generated, you would need to reload the app.config (or web.config, whichever you are using). This would then pick up the xml you have just generated.
 
How does that sound?




 


Subject: RE: How do I use dynamic boolean values on RollingFileAppender
Date: Fri, 5 Jun 2009 19:24:10 -0500
From: rfouts@...
To: log4net-user@...; log4net-user@...; log4net-dev@...

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

 

 


From: Karim Bourouba [mailto:karym6@...]
Sent: Fri 6/5/2009 1:20 PM
To: log4net-user@...; log4net-dev@...
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@...
To: log4net-user@...; log4net-dev@...

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!

 
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!


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

 « Return to Thread: RE: How do I use dynamic boolean values on RollingFileAppender