'log4net without app.config

I am trying to log to an sql server with the adoNetappender it works with an app.config but I need it whiteout an app.config this is my test code for the adoNetappender.

[assembly: log4net.Config.XmlConfigurator( Watch = true )]

namespace TestAdoNet {

class Program
{
    private static readonly log4net.ILog logger = log4net.LogManager.GetLogger( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType );

    static void Main( string[] args )
    {
        Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();

        PatternLayout patternLayout = new PatternLayout();
        patternLayout.ConversionPattern = "%date [%thread] %level %logger - %message%newline";
        patternLayout.ActivateOptions();

        AdoNetAppender adoNet = new AdoNetAppender();

        adoNet.BufferSize = 1;
        adoNet.Layout = patternLayout;
        adoNet.ConnectionType = "System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
        adoNet.ConnectionString = "data source=.; initial catalog=test;integrated security=true;";
        adoNet.CommandText = "INSERT INTO Log2 ([Date],[Thread],[Level],[Logger], [Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)";


        AdoNetAppenderParameter logDate = new AdoNetAppenderParameter();

        logDate.ParameterName = "@log_date";
        logDate.DbType = DbType.DateTime;
        logDate.Layout = new RawTimeStampLayout();

        adoNet.AddParameter( logDate );

        RawLayoutConverter rlc = new RawLayoutConverter();

        AdoNetAppenderParameter logThread = new AdoNetAppenderParameter();


        logThread.ParameterName = "@thread";
        logThread.DbType = DbType.String;
        logThread.Size = 255;
        logThread.Layout = (IRawLayout)rlc.ConvertFrom( new PatternLayout( "%thread" ) );

        adoNet.AddParameter( logThread );

        AdoNetAppenderParameter logLevel = new AdoNetAppenderParameter();

        logLevel.ParameterName = "@log_level";
        logLevel.DbType = DbType.String;
        logLevel.Size = 50;
        logLevel.Layout = (IRawLayout)rlc.ConvertFrom( new PatternLayout( "%level" ) );

        adoNet.AddParameter( logLevel );

        AdoNetAppenderParameter logLogger = new AdoNetAppenderParameter();

        logLogger.ParameterName = "@logger";
        logLogger.DbType = DbType.String;
        logLogger.Size = 255;
        logLogger.Layout = (IRawLayout)rlc.ConvertFrom( new PatternLayout( "%logger" ) );

        adoNet.AddParameter( logLogger );

        AdoNetAppenderParameter logMessage = new AdoNetAppenderParameter();

        logMessage.ParameterName = "@message";
        logMessage.DbType = DbType.String;
        logMessage.Size = 4000;
        logMessage.Layout = (IRawLayout)rlc.ConvertFrom( new PatternLayout( "%message" ) );

        adoNet.AddParameter( logMessage );

        AdoNetAppenderParameter logException = new AdoNetAppenderParameter();


        logException.ParameterName = "@exception";
        logException.DbType = DbType.String;
        logException.Size = 2000;
        logException.Layout = (IRawLayout)rlc.ConvertFrom( new ExceptionLayout() );


        adoNet.ActivateOptions();
        hierarchy.Root.AddAppender( adoNet );
        hierarchy.Root.Level = Level.All;
        hierarchy.Configured = true;


        logger.Error( "test" );

    }
}

I tried the same with the rolingfileappender and it works my only problem now is the sql server. I don't get any error messages so I don't know what is wrong.



Solution 1:[1]

Thanks @stuartd I found it

Wrong: ConnectionType = "System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;",

Right: ConnectionType = "System.Data.SqlClient.SqlConnection, System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",

The ";" at the end was the problem :D

Solution 2:[2]

You can also set as: ConnectionType = "System.Data.SqlClient.SqlConnection, System.Data".

That way you don't bind yourself to a specific version of the dependency.

I found that using the specific Version=1.0.3300.0 was constantly creating new connections to the database. Such connections remained in the Connection Pool on SQL side and were not reused for similar requests - the expected is for the connections in the pool to be reused under the same context. My application was periodically going down since the connection pool was reaching it's limit and there was no available connections for the application to consume.

When I removed the version (using the code I pasted above), the connection pool became more stable as the connections were being reused.

The reason behind it for that specific version? I haven't found anything online.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Fábio