3

我需要将自定义数据记录到 Db 并按照本教程进行操作: http: //stephenjamescode.blogspot.it/2014/01/logging-custom-objects-and-fields-with.html

我实现了一个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));
                }
            }
        }

我使用一个自定义ReflectionPatternLayout,除了注册ReflectionPatternConverter.

反射图案布局

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

我想记录这个自定义数据类:

自定义日志数据

    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();
        }
    }

所以我用这个配置文件(片段)配置了 log4net:

    <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>

每当我尝试编写日志条目时,都会出现此异常(log4net 的调试模式设置为 true):

log4net:错误 [AdoNetAppender] 错误代码:GenericFailure。写入数据库 log4net 时出现异常:错误 [AdoNetAppender] 错误代码:GenericFailure。写入数据库时​​出现异常 System.FormatException:无法将参数值从 String 转换为 DateTime。---> System.FormatException:字符串未被识别为有效的日期时间......

(异常信息可能不同,我是从意大利语翻译过来的)。
有趣的是,如果我将当前线程区域设置为CultureInfo.InvariantCulture

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

所以我高度怀疑这是日期时间格式的愚蠢语言环境问题。
如何将日志记录与客户端区域设置分离?

4

0 回答 0