【问题标题】:Setting up dynamic commandText for log4net为 log4net 设置动态命令文本
【发布时间】:2015-07-29 15:38:15
【问题描述】:

嘿,我在我的项目中使用 Log4Net。下面的代码只是来自我仅为测试目的而制作的测试项目。我有多个 commandText 标签的值,所以我想动态地制作它。到目前为止,我尝试使用 CustomColumn 但这不起作用。我认为是因为它没有插入 SQL 代码。

    <log4net>
  <appender name="AdoNetAppender" 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="xx" Catalog="xx"; Integrated Security=True;"/>
    <commandText value="INSERT INTO dbo.Log ([Date],[Thread],[Level],[Logger],[Message],[Exception], [AuftragsID]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception, @CustomColumn )" />
    ...other parameters
    <parameter>
      <parameterName value="@CustomColumn"/>
      <dbType value="String" />
      <size value="255" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%property{CustomColumn}" />
      </layout>
    </parameter>
  </appender>
  <root>
    <level value="ALL"/>
    <appender-ref ref="AdoNetAppender"/>
  </root>
</log4net>

这是我的代码

    private static readonly log4net.ILog log = log4net.LogManager.GetLogger (System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private void simpleButton1_Click(object sender, EventArgs e)
    {
        log4net.Config.XmlConfigurator.Configure(new FileInfo(@"D:\Daten\x-NeissF\Documents\Visual Studio 2013\Projects\AUSGABE\AUSGABE\Logging.config"));
        log.Info("Info logging");
        log.Debug("Debug");
        log.Error("ERROR");
        log.Fatal("FATAL");
        log.Warn("Warn");
        log4net.LogicalThreadContext.Properties["CustomColumn"] = "SELECT ID_DBO_AUFTRAG FROM dbo.Auftrag WHERE CADSystem='Promis'";

    }

我需要一个插入不同 commandText 值的函数。类似Thread 的内容。但是有一个包含的字符串。

    <commandText value="INSERT INTO dbo.Log ([Date],[Thread],[Level],[Logger],[Message],[Exception], [AuftragsID]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception, SELECT ID_DBO_AUFTRAG FROM dbo.Auftrag WHERE CADSystem='Promis'

【问题讨论】:

    标签: c# sql log4net


    【解决方案1】:

    3 小时后,我找到了适合我的解决方案。

    string text = File.ReadAllText(@"D:\Data\.logging.config", Encoding.Default);
    text = text.Replace("OriginalString", "NewString");
    File.WriteAllText((@"D:\Data\logging.config"), text,Encoding.Default);
    

    在这种情况下,我将更改插入到 sql 语句中的值。

    <commandText value="INSERT INTO dbo.Log ([Date],[Thread],[Level],[Logger],[Message],[Exception], [AuftragsID]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception, SELECT data FROM dbo.table WHERE value='OriginalString' )" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-09
      • 2021-11-26
      • 2020-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-07
      • 1970-01-01
      相关资源
      最近更新 更多