如何启用Common.Logging.Log4Net

时间:2015-06-04 16:35:41

标签: asp.net-mvc log4net common.logging

我正在尝试启用Common.Logging.Log4Net将所有类型的日志写入日志文件。教程使它看起来如此简单,但我不知道我做错了什么。这些是我正在采取的步骤:

  1. 创建一个新的ASP.NET MVC空项目
  2. 安装" Common Logging Log4Net 1211" NuGet包
  3. 将以下行添加到默认的web.config:

                                                

    <common>
        <logging>
            <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net">
                <arg key="configType" value="INLINE" />
            </factoryAdapter>
        </logging>
    </common>
    
    <log4net>
        <root>
            <level value="ALL" />
            <appender-ref ref="FileAppender" />
        </root>
        <appender name="FileAppender" type="log4net.Appender.FileAppender" >
            <param name="File" value="C:\Users\MyName\Downloads\log.txt" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date [%thread] %level %logger - %message%newline" />
            </layout>
        </appender>
    </log4net>
    
  4. 确保该应用。池身份帐户具有RW访问我尝试保存日志文件的路径。

  5. 从代码中抛出一个随机异常以进行测试。

  6. 我错过了什么吗?有没有办法调试log4net?请帮助这个可怜的灵魂。谢谢。

3 个答案:

答案 0 :(得分:4)

该问题与更改程序集名称的“Common.Logging.Log4Net”的NuGet包有关。这实际上修复了它(plesae注意新的程序集名称,Common.Logging.Log4Net1211):

<common>
    <logging>
        <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net1211">
            <arg key="configType" value="INLINE" />
        </factoryAdapter>
    </logging>
</common>

答案 1 :(得分:0)

https://stackoverflow.com/a/756241/3469201

First_xor_list=[0xaa,0x89,0xc4,0xfe,0x46]
Secpnd_xor_list=[0x78,0xf0,0xd0,0x03,0xe7]
after_xor=[]
xor_helper=[]
name = "UserNa"

after_xor.append(ord(name[0]))#first stage - xor from second char to    end and add the second char to the end of  the list
for i in range(len(name)):
    if i < len(name)-1:
        after_xor.append((First_xor_list[i])^ (ord(name[i+1])))
    elif i < len(name):
        after_xor.append(ord(name[1]))

然后,要确定要保存输出的文件,可以在同一.config文件中添加以下代码:

<configuration>
   <appSettings>
      <add key="log4net.Internal.Debug" value="true"/>
   </appSettings>
</configuration>

答案 2 :(得分:0)

我不熟悉Common.Logging.Log4Net。这是我使用的简单log4net步骤 -

  1. NuGet安装log4net。
  2. 在web.config 中插入以下设置(您可以按照自己的方式配置)
  3. 然后登录。
  4. 如果您想使用IoC容器,可以阅读我上个月提到的my question

    的web.config

    <configuration>
      <configSections>
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
      </configSections>
      <log4net>
        <root>
          <level value="DEBUG"/>
          <appender-ref ref="FileAppender"/>
        </root>
        <appender name="FileAppender" type="log4net.Appender.FileAppender">
            <file value="log-file.txt" />
            <appendToFile value="true" />
            <layout type="log4net.Layout.PatternLayout">
                <conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
            </layout>
        </appender>
      </log4net>  
    </configuration>
    

    在Global.asax.cs

    中进行测试
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
    
            XmlConfigurator.Configure();
            // You can inject it to your code using IoC container.
            var logger = LogManager.GetLogger(typeof(MvcApplication));
            logger.Debug("Test Debug.");
            logger.Error("Test Error.");
            logger.Fatal("Test Fatal.");
            logger.Info("Test Info.");
        }
    }
    

    结果

    2015-06-04 11:48:05,885 [1] DEBUG DemoLog4Net.MvcApplication [(null)] - Test Debug.
    2015-06-04 11:48:05,921 [1] ERROR DemoLog4Net.MvcApplication [(null)] - Test Error.
    2015-06-04 11:48:05,922 [1] FATAL DemoLog4Net.MvcApplication [(null)] - Test Fatal.
    2015-06-04 11:48:05,922 [1] INFO  DemoLog4Net.MvcApplication [(null)] - Test Info.