5
votes

I want to use a Common.logging and log4net on project wrapper, i created a log4net.config file on my logging project and i configured my web.config file but i got an exception when i try to log anything (error, info, etc). I thought the problem is in the web.config file but i checked out and i don't see anything wrong.

The exception error is

An error occurred creating the configuration section handler for common/logging: Unable to create type 'Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net

DLL's

Common.Logging.2.0.0
Common.Logging.Log4Net.2.0.1
log4net.1.2.10

Wrapper class

using System;
using System.Diagnostics;
using Common.Logging;
using System.Reflection;

namespace LoggerL
{
    public class Logger
    {
        private static readonly ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        //static Logger()
        //{
          //  XmlConfigurator.Configure();
        //}

        public static void Info(string msg = "")
        {
            var method = new StackFrame(1).GetMethod();
            var name = method.DeclaringType.FullName + "." + method.Name + "(";

            foreach (var item in method.GetParameters())
            {
                name += item.ParameterType.Name + " " + item.Name + ", ";
            }

            name = name.Substring(0, name.Length - 2) + ")" + " " + msg;

            log.Info(name);
        }

        public static void Error(Exception ex)
        {
            var method = new StackFrame(1).GetMethod();
            var name = method.DeclaringType.FullName + "." + method.Name + "(";

            foreach (var item in method.GetParameters())
            {
                name += item.ParameterType.Name + " " + item.Name + ", ";
            }

            name = name.Substring(0, name.Length - 2) + ")";

            name += ")";

            log.Error(name, ex);
        }
    }
}

web.config file

<configuration>
  <configSections>
    <sectionGroup name="common">
      <section name="logging"
               type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
    </sectionGroup>
  </configSections>
  <common>
    <logging>
      <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net">
        <arg key="configType" value="FILE-WATCH" />
        <arg key="configFile" value="~/bin/log4net.config" />
      </factoryAdapter>
    </logging>
  </common>
</configuration>

log4net.config file

<?xml version="1.0" encoding="utf-8" ?>

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
  </configSections>

  <log4net>
    <root>
      <level value="INFO" />
      <appender-ref ref="DatabaseAppender" />
    </root>

    <appender name="DatabaseAppender" 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="data source=MyServer; initial catalog=MyDB;integrated security=false;
                                          persist security info=True;User ID=MyUser;Password=MyPass" />
      <commandText
          value="INSERT INTO [Log]
                          ([Date],[Thread],[Level],[Logger], [Message],[Exception]) VALUES (@log_date, @thread, @log_level, @logger, @message, @exception)" />
      <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>
  </log4net>
</configuration>
1
Maybe it doesn't apply to this application, but any time you grab the StackFrame it's expensive, not to mention using string + string instead of a StringBuilder for performance as well. Last thing you want it your app to crawl as an artifact of logging.Kelly Elton
I think the log4net.config is wrong, You need to remove <configuration>, <configurationsections> and only keep <log4net> node.Sÿl

1 Answers

0
votes

Have you tried this for your factoryAdapter config

<common>
 <logging>
   <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4net1211">