logging-log4net-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ron Grabowski <rongrabow...@yahoo.com>
Subject Re: How do I use dynamic boolean values on RollingFileAppender
Date Tue, 09 Jun 2009 23:06:05 GMT
The built-in PatternStringConverter takes a conversion pattern and converts it into a string.
Here's a PatternObjectConverter that takes an conversion pattern and returns the appropriate
typed object. This can replace your PatternStringToBooleanAdapter and PatternStringToInt32Adapter
classes. It works by taking the string returned by the internal PatternString.Format call
and using reflection to invoke the Type's Parse method on it. This means it will also work
on DateTime, Decimal, etc. values without having to write special classes for those:

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

    public bool CanConvertTo(Type targetType)
    {
        // TODO: ensure Type has a Parse method on it
        return true; 
    }

    public object ConvertTo(object source, Type targetType)
    {
        var patternObjectConverter = (PatternObjectConverter)source;

        var patternString = new PatternString(patternObjectConverter.ConversionPattern);

        string value = patternString.Format();

        return OptionConverter.ConvertStringTo(targetType, value);
    }
}

log4net uses OptionConverter.ConvertStringTo internally to magically convert strings to typed
objects when it parses the xml file. That's how it converts "true" to true.

There's a special case in OptionConverter.ConvertStringTo for enums so you shouldn't need
PatternStringToRollingModeAdapter either :-)




________________________________
From: Richard Fouts <rfouts@we-are-it.com>
To: Log4NET User <log4net-user@logging.apache.org>
Sent: Tuesday, June 9, 2009 3:24:45 PM
Subject: RE: How do I use dynamic boolean values on RollingFileAppender

 
Ron that worked beautifully. 
In case anyone else runs across this issue or something similar, I built a
couple of other Adapters based on the same model.  Here are all of the
adapters in all their glory.
 
‘In class AdditionalPatternStringAdapters
#Region "Boolean Pattern String"
PublicClass PatternStringToBooleanAdapter
      Implements TypeConverters.IConvertTo
 
      Private _ConversionPattern As String
 
      Public Property ConversionPattern() As String
            Get
                  Return _ConversionPattern
            End Get
            Set(ByVal value As String)
                  _ConversionPattern
= value
            End Set
      End Property
 
      Public Function CanConvertTo(ByVal targetType As Type) As Boolean Implements TypeConverters.IConvertTo.CanConvertTo
            Console.WriteLine("Taget Type is: " &
targetType.ToString & _
             " | Cast Type is: " & GetType(Boolean).ToString
& " | CASTABLE: " &
(targetType Is GetType(Int32)).ToString)
            Return targetType Is GetType(Boolean)
      End Function
 
      Public Function ConvertTo(ByVal source As Object, ByVal targetType As Type) As Object
Implements TypeConverters.IConvertTo.ConvertTo
            Dim patternStringToBooleanAdapter = DirectCast(source, PatternStringToBooleanAdapter)
 
            Dim patternString = New PatternString(patternStringToBooleanAdapter.ConversionPattern)
 
            Dim value As String = patternString.Format()
 
            Return OptionConverter.ToBoolean(value, False)
      End Function
 
EndClass
#End Region
 
#Region "Int32 Pattern String"
PublicClass PatternStringToInt32Adapter
      Implements TypeConverters.IConvertTo
 
      Private _ConversionPattern As String
 
      Public Property ConversionPattern() As String
            Get
                  Return _ConversionPattern
            End Get
            Set(ByVal value As String)
                  _ConversionPattern
= value
            End Set
      End Property
 
      Public Function CanConvertTo(ByVal targetType As Type) As Boolean Implements TypeConverters.IConvertTo.CanConvertTo
            Console.WriteLine("Taget Type is: " &
targetType.ToString & _
             " | Cast Type is: " & GetType(Int32).ToString & " | CASTABLE: " &
(targetType Is GetType(Int32)).ToString)
            Return targetType Is GetType(Int32)
      End Function
 
      Public Function ConvertTo(ByVal source As Object, ByVal targetType As Type) As Object
Implements TypeConverters.IConvertTo.ConvertTo
            Dim patternStringToInt32Adapter = DirectCast(source, PatternStringToInt32Adapter)
 
            Dim patternString = New PatternString(PatternStringToInt32Adapter.ConversionPattern)
 
            Dim value As String = patternString.Format()
 
            If value IsNot Nothing AndAlso value.Length > 0 Then
                  Try
                        Return Int32.Parse(value)
                  Catch e As Exception
                        Console.WriteLine("AdditionalPatternStrings.PatternStringToInt32Adapter:
[" & value & "] is not in
proper Int32 form.", e)
                        value
= 8
                  End Try
            Else
                  value
= 8
            End If
 
            Return value
      End Function
 
EndClass
#End Region
 
#Region "Rolling Mode Pattern String"
PublicClass PatternStringToRollingModeAdapter
      Implements TypeConverters.IConvertTo
 
      Private _ConversionPattern As String
 
      Public Property ConversionPattern() As String
            Get
                  Return _ConversionPattern
            End Get
            Set(ByVal value As String)
                  _ConversionPattern
= value
            End Set
      End Property
 
      Public Function CanConvertTo(ByVal targetType As Type) As Boolean Implements TypeConverters.IConvertTo.CanConvertTo
            Console.WriteLine("Taget Type is: " &
targetType.ToString & _
             " | Cast Type is: " & GetType(log4net.Appender.RollingFileAppender.RollingMode).ToString
& " | CASTABLE: " &
(targetType Is GetType(log4net.Appender.RollingFileAppender.RollingMode)).ToString)
            Return targetType Is GetType(log4net.Appender.RollingFileAppender.RollingMode)
      End Function
 
      Public Function ConvertTo(ByVal source As Object, ByVal targetType As Type) As Object
Implements TypeConverters.IConvertTo.ConvertTo
            Dim patternStringToRollingModeAdapter = DirectCast(source, PatternStringToRollingModeAdapter)
 
            Dim patternString = New PatternString(patternStringToRollingModeAdapter.ConversionPattern)
 
            Dim value As String = patternString.Format()
            Console.WriteLine("AdditionalPatternStrings.PatternStringRollingModeAdapter:
[" & value & "] is the
asking to be converted.")
 
            If value IsNot Nothing AndAlso value.Length > 0 Then
                  Try
                        Select Case value
                              Case "Composite"
                                    Return log4net.Appender.RollingFileAppender.RollingMode.Composite
                              Case "Once"
                                    Return log4net.Appender.RollingFileAppender.RollingMode.Composite
                        End Select
                  Catch e As Exception
                        Console.WriteLine("AdditionalPatternStrings.PatternStringToRollingModeAdapter:
[" & value & "] is not in
proper Rolling Mode form.", e)
                        value
= log4net.Appender.RollingFileAppender.RollingMode.Composite
                  End Try
            Else
                  value
= log4net.Appender.RollingFileAppender.RollingMode.Composite
            End If
 
            Return log4net.Appender.RollingFileAppender.RollingMode.Composite
      End Function
 
EndClass
#End Region
 
‘In Main
            log4net.Util.TypeConverters.ConverterRegistry.AddConverter(GetType(PatternStringToBooleanAdapter),
New PatternStringToBooleanAdapter())
            log4net.Util.TypeConverters.ConverterRegistry.AddConverter(GetType(PatternStringToInt32Adapter),
New PatternStringToInt32Adapter())
            log4net.Util.TypeConverters.ConverterRegistry.AddConverter(GetType(PatternStringToRollingModeAdapter),
New PatternStringToRollingModeAdapter())
 
‘In Configuration File
            <appendername="SyncLogFile"type="log4net.Appender.RollingFileAppender">
                  <filetype="log4net.Util.PatternString"value="%property{LogPath}\Synchronizations\%property{LogName}"/>
                  <datePatternvalue="yyyy-MM-dd'.log'"/>
                  <appendToFiletype="FileSync.PatternStringToBooleanAdapter, FileSync">
                        <conversionPatternvalue="%property{isAppendToFile}"/>
                  </appendToFile>
                  <maxSizeRollBackupstype="FileSync.PatternStringToInt32Adapter, FileSync">
                        <conversionPatternvalue="%property{MaxRollBacks}"/>
                  </maxSizeRollBackups>
                  <maximumFileSizetype="log4net.Util.PatternString"value="%property{MaxFileSize}"/>
                  <staticLogFileNametype="FileSync.PatternStringToBooleanAdapter, FileSync">
                        <conversionPatternvalue="%property{isStaticLogFile}"/>
                  </staticLogFileName>
                  <rollingStyletype="FileSync.PatternStringToRollingModeAdapter, FileSync">
                        <conversionPatternvalue="%property{RollStyle}"/>
                  </rollingStyle>
                  <layouttype="log4net.Layout.PatternLayout">
                        <headertype="log4net.Util.PatternString"value="****************************************
Logging Started - %date ******************************************&#13;&#10;"/>
                        <footertype="log4net.Util.PatternString"value="****************************************
Logging Ended - %date ******************************************&#13;&#10;"/>
                        <conversionPatternvalue="%date [%thread] %-5level %logger [%ndc]
- %message%newline"/>
                  </layout>
            </appender>
Mime
View raw message