常见的日志log4net文件 - 监视

时间:2014-03-12 20:44:44

标签: c# log4net log4net-configuration common.logging

我想在项目包装器上使用Common.logging和log4net,我在我的日志项目中创建了一个log4net.config文件并配置了我的web.config文件,但是当我尝试记录任何内容时我得到了一个异常(错误,信息等)。我认为问题出在web.config文件中,但我检查了一下,我没有看到任何错误。

异常错误是

  

为其创建配置节处理程序时发生错误   common / logging:无法创建类型   ' Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter,   Common.Logging.Log4Net

DLL' S

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

包装类

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文件

<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文件

<?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 个答案:

答案 0 :(得分:0)

您是否已针对factoryAdapter配置

尝试此操作
<common>
 <logging>
   <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4net1211">