log4net-dev

[Prev] Thread [Next]  |  [Prev] Date [Next]

RE: How do I use dynamic boolean values on RollingFileAppender Karim Bourouba Mon Jun 08 06:05:55 2009

Hi Richard,

 

please, dont think i am telling you that you are wrong - I am not. Its just 
that I wouldnt have tried to do what you are doing with a .config file is all.

 

I have always viewed .config files in .Net as files that hold configuration. If 
I find that I need to change something on a regular basis in the .config file, 
then I tend to turn it into a setting. I guess in your situation, you could 
create the logfile itself programtically based on a value in the .config file, 
which is the way I have always veiwed these files should be used for. I think 
you could also store the values you need in the registry should you want to.

 

When it comes to log management, I know where my logs are stored and how they 
are named based on the application that is creating them, if I have a 
deployment app I know exactly where to look to view the logfile for that app, 
if I dont, then I can check the config to see where it is pointing to (or where 
ever else the value may be). If it is generated in the code then it would be a 
bit of a problem for someone who didnt know where to look in the first place 
for it.

 

When it comes to flexibility, as soon as you start to control the configuration 
of log4net in the code, then you loose the ability to reuse your code in other 
apps that could need logging. For instance, I have a logging library that I can 
use over and over again in different projects. All I need to do is reference it 
in my code and apply the relevant configuration in the web or app.config - but 
again, thats just how I work and how I have seen the way log4net behaves. I do 
control some of the config in the code with this lib, but this is because it 
logs to a db and not to a file. So I set the connection string this way (which 
I read from a config file).

 

However, in saying this, I can start to understand how you are thinking about 
this. Most of the logging that I carry out isnt ever really exposed to a user, 
its just there to give analysts and techs information on whats going on 
(especially when its bad). I dont really need an end user to control the 
formatting or location of the log itself etc, the apps and services that use it 
just chug away in the background with everyone oblivious to it.

 

In my previous email, I mentioned that you could create the settings as an XML 
file in the code and have the .config read this information. But you hit a 
problem here in so far that the .config will be read prior to the XML being 
generated. But, you can work around this, as long as the XML is generated to 
the same location each time, you can simply reload the .config for the app 
picking up the config you just created. i think the only thing I dont like 
about that is that you could just put the config in the .config file from the 
get go.

 

Like I said before, I dont think at all that you are wrong, I was just putting 
my views across. One other soloution would be to alter how log4net works to 
give you the behaviour you want, I did something similar myself to change the 
way db connections are used (I needed to attach an analysis tool).




 



Subject: RE: How do I use dynamic boolean values on RollingFileAppender
Date: Mon, 8 Jun 2009 07:35:08 -0500
From: [EMAIL PROTECTED]
To: [EMAIL PROTECTED]; [EMAIL PROTECTED]







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:[EMAIL PROTECTED] 
Sent: Saturday, June 06, 2009 2:16 AM
To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
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: [EMAIL PROTECTED]
To: [EMAIL PROTECTED]; [EMAIL PROTECTED]; [EMAIL PROTECTED]


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:[EMAIL PROTECTED]
Sent: Fri 6/5/2009 1:20 PM
To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
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: [EMAIL PROTECTED]
To: [EMAIL PROTECTED]; [EMAIL PROTECTED]

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.
_________________________________________________________________
Get the best of MSN on your mobile
http://clk.atdmt.com/UKM/go/147991039/direct/01/