AdoNetAppender 参数的默认值

发布于 2024-08-16 03:16:39 字数 1930 浏览 9 评论 0原文

我将 log4net 与 AdoNetAppender 一起使用。它将所有日志信息记录到表中。该表实际上有 2 个整数列(可以为空)。

这是我的 log4net 配置的相关部分:

<commandText value="INSERT INTO ActivityLog ([Date],[Thread],[Level],[Logger],[Message],[DealID]) 
                 VALUES (@log_date,@thread,@log_level,@logger,@message,@DealID)" />

 //other parameters hten DealID
<parameter>
      <parameterName value="@DealID" />
      <dbType value="Int32" />
       <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%property{DealID}" />
      </layout>
    </parameter>

我发现如果我没有使用类似 log4net.ThreadContext.Properties["DealID"] = DealID; 的内容显式设置,它会引发异常:

System.FormatException occurred
  Message="Failed to convert parameter value from a String to a Int32."
  Source="System.Data"
  StackTrace:
       at System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType)
  InnerException: System.FormatException
       Message="Input string was not in a correct format."
       Source="mscorlib"
       StackTrace:
            at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
            at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
            at System.String.System.IConvertible.ToInt32(IFormatProvider provider)
            at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
            at System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType)
       InnerException: 

我必须将其设置为:

log4net.ThreadContext.Properties["DealID"] = 0;

无论如何,我可以在我的 log4net 配置中为此 Int32 字段设置默认参数值,以便如果没有提供值,我不需要将其显式设置为 0?这让我想知道为什么设置为 varchar 的字段不会发生这种情况(尽管没有向它们提供任何值)。

I am using log4net with AdoNetAppender. It logs all log info into a table. This table actually has 2 Integer columns (can be null).

Here is the relevant part of my log4net config:

<commandText value="INSERT INTO ActivityLog ([Date],[Thread],[Level],[Logger],[Message],[DealID]) 
                 VALUES (@log_date,@thread,@log_level,@logger,@message,@DealID)" />

 //other parameters hten DealID
<parameter>
      <parameterName value="@DealID" />
      <dbType value="Int32" />
       <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%property{DealID}" />
      </layout>
    </parameter>

What I found out was if I don't explicitly set using something like log4net.ThreadContext.Properties["DealID"] = DealID; it throws me an exception:

System.FormatException occurred
  Message="Failed to convert parameter value from a String to a Int32."
  Source="System.Data"
  StackTrace:
       at System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType)
  InnerException: System.FormatException
       Message="Input string was not in a correct format."
       Source="mscorlib"
       StackTrace:
            at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
            at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
            at System.String.System.IConvertible.ToInt32(IFormatProvider provider)
            at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
            at System.Data.SqlClient.SqlParameter.CoerceValue(Object value, MetaType destinationType)
       InnerException: 

I would have to set it like:

log4net.ThreadContext.Properties["DealID"] = 0;

Is there anyway that I can set a default parameter value in my log4net config for this Int32 field so that I don't need to set it explicitly to 0 if no value is supplied? And it makes me wonder why it does not happen to fields which are set as varchar (though no value is supplied to them).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不念旧人 2024-08-23 03:16:39

更改您的附加程序:

<parameter>
    <parameterName value="@DealID" />
    <dbType value="Int32" />
    <layout type="log4net.Layout.RawPropertyLayout">  <!-- notice this -->
        <key value="DealID" />  <!-- and notice this instead of the pattern layout -->
    </layout>
</parameter>

为了给予信任,我找到了它 来自此线程。 (还有很多其他搜索试图做同样的事情。

Change your appender:

<parameter>
    <parameterName value="@DealID" />
    <dbType value="Int32" />
    <layout type="log4net.Layout.RawPropertyLayout">  <!-- notice this -->
        <key value="DealID" />  <!-- and notice this instead of the pattern layout -->
    </layout>
</parameter>

And to give credit, I found it from this thread. (And a bunch of other searching trying to do the same thing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文