使用AdoNetAppender的Log4Net-没有任何反应 [英] Log4Net with AdoNetAppender - nothing happens

查看:74
本文介绍了使用AdoNetAppender的Log4Net-没有任何反应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在程序集中有一个配置文件作为资源,并且想在我的应用程序中以编程方式更改ConnectionString.

I have a config file as a resource in my assembly and want to change the ConnectionString programmatically in my application.

我使用log4net.Config.XmlConfigurator.Configure加载配置.

我有一些断点,看到配置已成功加载,并且连接字符串为Data Source=localhost\SQLExpress;Initial Catalog=Log;Integrated Security=SSPI;(本地SQLExpress).

I have some breakpoints and see that the configuration is loaded successfuly and the connectionstring is Data Source=localhost\SQLExpress;Initial Catalog=Log;Integrated Security=SSPI; (local SQLExpress).

什么也没有发生,没有异常,也没有日志条目.任何想法.

Nothing happens, no exception and no log entry. Any ideas.

using (Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("MyNamespace.Properties.log4net.config"))
{ 
    // stream is NOT null
    log4net.Config.XmlConfigurator.Configure(stream);
}

Hierarchy hier = LogManager.GetRepository() as Hierarchy;

if (hier != null)
{
    //get ADONetAppender
    var adoAppender = (AdoNetAppender)hier.GetAppenders().Where(appender => appender.Name.Equals("AdoNetAppender", StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();

    if (adoAppender != null)
    {
        // update connectionstring
        adoAppender.ConnectionString = configuration.GetConnectionString(ConnectionStringNames.Log).ConnectionString;
        //refresh settings of appender
        adoAppender.ActivateOptions(); 
    }
}

ILog logger = LogManager.GetLogger("MyProject"); 
logger.Warn("Test");

log4net.config 文件的内容

content of the log4net.config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>

  <log4net>
    <appender name="AdoNetAppender" type="log4net.Appender.ADONetAppender">
      <bufferSize value="1" />
      <connectionType value="System.Data.SqlClient.SqlConnection, System.Data,
  Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      <connectionString value="[we will set this automatically at runtime]" />
      <commandText value="INSERT INTO Log ([Date],[Level],[Logger],[Message],[Exception])
  VALUES (@log_date, @log_level, @logger, @message, @exception)" />
      <parameter>
        <parameterName value="@log_date" />
        <dbType value="DateTime" />
        <layout type="log4net.Layout.RawTimeStampLayout" />
      </parameter>
      <parameter>
        <parameterName value="@log_level" />
        <dbType value="String" />
        <size value="50" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%p" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@logger" />
        <dbType value="String" />
        <size value="255" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%c" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@message" />
        <dbType value="String" />
        <size value="4000" />
        <layout type="log4net.Layout.PatternLayout">
          <conversionPattern value="%m" />
        </layout>
      </parameter>
      <parameter>
        <parameterName value="@exception" />
        <dbType value="String" />
        <size value="2000" />
        <layout type="log4net.Layout.ExceptionLayout" />
      </parameter>
    </appender>

    <root>
      <level value="ALL" />
      <appender-ref ref="AdoNetAppender" />
    </root>
  </log4net>
</configuration>

推荐答案

您可以通过连接到log4net DebugAppender来调试log4net:

You can debug log4net by hooking into the log4net DebugAppender:

在app.config文件中添加一个log4net应用程序设置:

Add a log4net app setting in your app.config file:

<appSettings>
  <!-- log4net configuration when running in debug mode. -->    
  <add key="log4net.Internal.Debug" value="true" />   
</appSettings>

在log4net配置中添加调试附加程序:

Add a debug appender in the log4net config:

<appender name="DebugAppender" type="log4net.Appender.DebugAppender">
  <immediateFlush value="true" />
  <layout type="log4net.Layout.SimpleLayout" />
</appender>

将添加程序添加到log4net配置根目录:

Add the appender to the log4net config root:

<root>
  <level value="ALL" />
  <appender-ref ref="AdoNetAppender" />
  <appender-ref ref="DebugAppender" />
</root>

运行应用程序时,在Visual Studio的输出窗口中查找,您应该看到log4net的所有内部日志记录.如果没有,则从不加载log4net配置文件.

When you run your application, look in the output window of Visual Studio and you should see all the internal logging for log4net. If not, then the log4net config file is never loading.

修改

如果您可以使用app.config文件中的连接字符串,则从log4net AdoNetAppender中删除该连接字符串,然后按名称调用该连接字符串:

If you can use a connection string from your app.config file, then remove the connection string from the log4net AdoNetAppender and just call the connection string by name:

<appender name="AdoNetAppender" type="log4net.Appender.AdoNetAppender">
  <bufferSize value="1" />
  <!-- note: you can use the 4.0 assembly -->
  <connectionType value="System.Data.SqlClient.SqlConnection,
              System.Data, 
              Version=4.0.0.0, 
              Culture=neutral, 
              PublicKeyToken=b77a5c561934e089" />
  <!-- This will retrieve a connection string by name from the app.config -->
  <connectionStringName value="ConnectionStringNameFromAppConfig" />
  <!-- snip -->
</appender>

这篇关于使用AdoNetAppender的Log4Net-没有任何反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆