log4net adonetappender with custom data, cannot write log if it contains DataTime data

856 Views Asked by At

I need to log custom data to Db and i followed this tutorial: http://stephenjamescode.blogspot.it/2014/01/logging-custom-objects-and-fields-with.html

I implemented a ReflectionPatternConverter which enables me to log any custom data i need. This is the code:

ReflectionPatternConverter

    public class ReflectionPatternConverter : PatternConverter
    {
        protected override void Convert(TextWriter writer, object state)
        {
            if (state == null)
            {
                writer.Write(SystemInfo.NullText);
                return;
            }

            var loggingEvent = state as LoggingEvent;
            if (loggingEvent == null)
                return;

            Type messageObjectType = loggingEvent.MessageObject.GetType();
            PropertyInfo[] properties = messageObjectType.GetProperties(BindingFlags.Instance | BindingFlags.Public);

            foreach (var property in properties)
            {
                if (this.Option.ToLower().Equals(property.Name.ToLower()))
                {
                    writer.Write(property.GetValue(loggingEvent.MessageObject, null));
                }
            }
        }

I use a custom ReflectionPatternLayout which does nothing except registering the ReflectionPatternConverter.

ReflectionPatternLayout

    public class ReflectionPatternLayout : PatternLayout
    {
        public ReflectionPatternLayout()
        {
            ConverterInfo converter = new ConverterInfo();
            converter.Name = "customProperties";
            converter.Type = typeof (ReflectionPatternConverter);
            AddConverter(converter);
        }
    }

I want to log this custom data class:

CustomLogData

    public class CustomLogData
    {
        public string Prop1 { get; set; }
        public DateTime Prop2 { get; set; }
        public int Prop3 { get; set; }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(
                "Prop1: {0}, Prop2: {1}, Prop3: {2}",
                this.Prop1,
                this.Prop2.ToString("yyyyMMdd HH:mm:ss"),
                this.Prop3);
            return builder.ToString();
        }
    }

So i configured log4net whit this Config file (snippet):

    <appender name="AdoNetManager" type="log4net.Appender.AdoNetAppender">
        <bufferSize value="1" />
        <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        <connectionString value="**omitted**" />
        <commandText value="INSERT INTO dbo.ApplicationLog (Timestamp, Prop1, Prop2, Prop3) VALUES (@Timestamp, @Prop1, @Prop2, @Prop3)" />
        <parameter>
            <parameterName value="@Timestamp" />
            <dbType value="DateTime" />
            <layout type="log4net.Layout.RawTimeStampLayout" />
        </parameter>
        <parameter>
            <parameterName value="@Prop1" />
            <dbType value="String" />
            <size value="500" />
            <layout type="MyAssembly.ReflectionPatternLayout, MyAssembly">
                <conversionPattern value="%customProperties{Prop1}" />
            </layout>
        </parameter>
        <parameter>
            <parameterName value="@Prop2" />
            <dbType value="DateTime" />
            <layout type="MyAssembly.ReflectionPatternLayout, MyAssembly">
                <conversionPattern value="%customProperties{Prop2}" />
            </layout>
        </parameter>
        <parameter>
            <parameterName value="@Prop3" />
            <dbType value="Int32" />
            <layout type="MyAssembly.ReflectionPatternLayout, MyAssembly">
                <conversionPattern value="%customProperties{Prop3}" />
            </layout>
        </parameter>
    </appender>

Anytime i try to write a log entry i get this exception (with log4net's debug mode set to true):

log4net:ERROR [AdoNetAppender] ErrorCode: GenericFailure. Exception while writing to database log4net:ERROR [AdoNetAppender] ErrorCode: GenericFailure. Exception while writing to database System.FormatException: Can't convert parameter value from String do DateTime. ---> System.FormatException: String was not recognized as a valid Date Time....

(the exception message could be different, i translated it from italian).
The funny this is that i get my log correctly done if i put the current thread locale to CultureInfo.InvariantCulture

...        
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture;
//log here

So i highly suspect this is a stupid locale problem with datetime formats.
How do i decouple logging from client locale settings?

0

There are 0 best solutions below