使用 log4net 的 AdoNetAppender 时如何指定常量参数

发布于 2024-12-28 15:00:30 字数 3663 浏览 1 评论 0原文

http://logging.apache.org/log4net/release/config-examples.html

给出此处的日志表:

CREATE TABLE [dbo].[Log] (
    [Id] [int] IDENTITY (1, 1) NOT NULL,
    [Date] [datetime] NOT NULL,
    [Thread] [varchar] (255) NOT NULL,
    [Level] [varchar] (50) NOT NULL,
    [Logger] [varchar] (255) NOT NULL,
    [Message] [varchar] (4000) NOT NULL,
    [Exception] [varchar] (2000) NULL,

    [Product] [varchar] 100 not null
)

以及此处的附加程序配置:

<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
    <bufferSize value="100" />
    <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <connectionString value="data source=[database server];initial catalog=[database name];integrated security=false;persist security info=True;User ID=[user];Password=[password]" />
    <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception], [Product]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception, @product)" />
    <parameter>
        <parameterName value="@log_date" />
        <dbType value="DateTime" />
        <layout type="log4net.Layout.RawTimeStampLayout" />
    </parameter>
    <parameter>
        <parameterName value="@thread" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%thread" />
        </layout>
    </parameter>
    <parameter>
        <parameterName value="@log_level" />
        <dbType value="String" />
        <size value="50" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%level" />
        </layout>
    </parameter>
    <parameter>
        <parameterName value="@logger" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%logger" />
        </layout>
    </parameter>
    <parameter>
        <parameterName value="@message" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%message" />
        </layout>
    </parameter>
    <parameter>
        <parameterName value="@exception" />
        <dbType value="String" />
        <size value="2000" />
        <layout type="log4net.Layout.ExceptionLayout" />
    </parameter>
</appender>

如何在不更改任何代码的情况下为“产品”列添加常量参数?如果可能的话仅更改配置!

例如,我想做这样的事情:

<parameter>
    <parameterName value="@product" />
    <dbType value="String" />
    <size value="100" />

    <constantValue value="Product A" /> <!-- how do i do this? -->

</parameter>

编辑:答案很简单。编辑命令文本!

<commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception], [Product]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception, 'Product A')" />

http://logging.apache.org/log4net/release/config-examples.html

Given the log table here:

CREATE TABLE [dbo].[Log] (
    [Id] [int] IDENTITY (1, 1) NOT NULL,
    [Date] [datetime] NOT NULL,
    [Thread] [varchar] (255) NOT NULL,
    [Level] [varchar] (50) NOT NULL,
    [Logger] [varchar] (255) NOT NULL,
    [Message] [varchar] (4000) NOT NULL,
    [Exception] [varchar] (2000) NULL,

    [Product] [varchar] 100 not null
)

and the appender config here:

<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
    <bufferSize value="100" />
    <connectionType value="System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    <connectionString value="data source=[database server];initial catalog=[database name];integrated security=false;persist security info=True;User ID=[user];Password=[password]" />
    <commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception], [Product]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception, @product)" />
    <parameter>
        <parameterName value="@log_date" />
        <dbType value="DateTime" />
        <layout type="log4net.Layout.RawTimeStampLayout" />
    </parameter>
    <parameter>
        <parameterName value="@thread" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%thread" />
        </layout>
    </parameter>
    <parameter>
        <parameterName value="@log_level" />
        <dbType value="String" />
        <size value="50" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%level" />
        </layout>
    </parameter>
    <parameter>
        <parameterName value="@logger" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%logger" />
        </layout>
    </parameter>
    <parameter>
        <parameterName value="@message" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout">
            <conversionPattern value="%message" />
        </layout>
    </parameter>
    <parameter>
        <parameterName value="@exception" />
        <dbType value="String" />
        <size value="2000" />
        <layout type="log4net.Layout.ExceptionLayout" />
    </parameter>
</appender>

How can I add a constant parameter for the "product" column- without any code changes? Config-only change if possible!

For example, I want to do something like this:

<parameter>
    <parameterName value="@product" />
    <dbType value="String" />
    <size value="100" />

    <constantValue value="Product A" /> <!-- how do i do this? -->

</parameter>

EDIT: The answer is simple. Edit the command text!

<commandText value="INSERT INTO Log ([Date],[Thread],[Level],[Logger],[Message],[Exception], [Product]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception, 'Product A')" />

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

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

发布评论

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

评论(2

水中月 2025-01-04 15:00:30

虽然您可以简单地将“SomeConstant”值添加到插入语句中,但我们将以下方法与 AdoNetAppender 一起使用。我们在日志表中添加一个 @source 列,这样我们就可以让许多服务器都记录到同一个表,同时为我们提供一些类似 SYSLOG 的功能。

  ...
  <commandText value="INSERT INTO Log ([Date],[Source],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @source, @log_level, @logger, @message, @exception)" />

  ...
  <parameter>
    <parameterName value="@source"/>
    <dbType value="String"/>
    <size value="32"/>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="WEB1"/>
    </layout>
  </parameter>

PatternLayout 很容易接受字符串常量,为什么要让它复杂化呢!

享受。

While you could simply add a 'SomeConstant' value to the insert statement, we use the following method with the AdoNetAppender. We add a @source column to our log table so we can have many servers all logging to the same table, yet give us some SYSLOG like functionality.

  ...
  <commandText value="INSERT INTO Log ([Date],[Source],[Level],[Logger],[Message],[Exception]) VALUES (@log_date, @source, @log_level, @logger, @message, @exception)" />

  ...
  <parameter>
    <parameterName value="@source"/>
    <dbType value="String"/>
    <size value="32"/>
    <layout type="log4net.Layout.PatternLayout">
      <conversionPattern value="WEB1"/>
    </layout>
  </parameter>

The PatternLayout readily accepts string constants, why complicate it!

Enjoy.

美男兮 2025-01-04 15:00:30

我想你可以这样做:

using System.IO;
using log4net.Core;
using log4net.Layout;

namespace MyApp
{
  public class ConstantLayout : LayoutSkeleton
  {
    public string Text { get; set; }

    public override void ActivateOptions()
    {
    }

    public override void Format(TextWriter writer, LoggingEvent loggingEvent)
    {
      using (writer)
        writer.Write(this.Text);
    }
  }
}

然后..

  <parameter>
    <parameterName value="@product" />
    <dbType value="String" />
    <size value="100" />
    <layout type="MyApp.ConstantLayout">
      <text value="Product A" />
    </layout>
  </parameter>

I suppose you could do this:

using System.IO;
using log4net.Core;
using log4net.Layout;

namespace MyApp
{
  public class ConstantLayout : LayoutSkeleton
  {
    public string Text { get; set; }

    public override void ActivateOptions()
    {
    }

    public override void Format(TextWriter writer, LoggingEvent loggingEvent)
    {
      using (writer)
        writer.Write(this.Text);
    }
  }
}

and then..

  <parameter>
    <parameterName value="@product" />
    <dbType value="String" />
    <size value="100" />
    <layout type="MyApp.ConstantLayout">
      <text value="Product A" />
    </layout>
  </parameter>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文