Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
473 views
in Technique[技术] by (71.8m points)

c# - Log4Net with AdoNetAppender - nothing happens

Description

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

I load the configuration using log4net.Config.XmlConfigurator.Configure.

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

Problem

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");

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can debug log4net by hooking into the log4net DebugAppender:

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>

Add a debug appender in the log4net config:

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

Add the appender to the log4net config root:

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

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.

Edit

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...