无法在ASP.NET Web窗体站点中使用log4net将日志写入文件 [英] unable to write logs to a file using log4net in an ASP.NET Web Forms Site

查看:291
本文介绍了无法在ASP.NET Web窗体站点中使用log4net将日志写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用log4net dll登录到文本文件时遇到问题.

I'm having a problem logging to a text file using log4net dll.

我在Windows Server 2008 R2标准(64)上使用vs 2012 Express. 我已经执行了以下步骤:

I'm using vs 2012 express on a windows server 2008 r2 standard (64). I have preform the following steps:

  1. 打开一个新的asp.net Web表单
  2. 通过nuget管理器安装log4net.
  3. 在global.asax文件中,我在Application_Start中有此文件:

  1. Open a new asp.net web forms
  2. Install log4net via nuget manager.
  3. in the global.asax file I have this in Application_Start :

log4net.Config.XmlConfigurator.Configure ();

  • 在Web配置中,我将以下行添加到configSections节点:

  • In the web config I have added to the configSections node this line :

    <部分名称="log4net" type ="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>

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

    在Web配置中,我添加了以下部分:

    In the web config I have added this section :

    <log4net debug="true">
     <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
     <file value="c:\webSite\extraDownloadServerResources\Logs\ALU\log.txt" />
    <appendToFile value="true" />
    <rollingStyle value="Composite"/>
    <datePattern value="yyyyMMdd" />
    <maxSizeRollBackups value="5" />
    <maximumFileSize value="5MB" />
    <staticLogFileName value="true" />
    <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%n%-5p %d %5rms %-22.22c{1} %-18.18M %n - %m%n" />
    </layout>
    </appender>
    <root>
     <level value="DEBUG" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>
    </log4net>
    

  • 在启动页面中,我获取了logger的实例:

  • In a start up page I have fetch instance of the logger :

    private static readonly ILog log = LogManager.GetLogger ( System.Reflection.MethodBase.GetCurrentMethod ().DeclaringType );
    

  • Page_Load中,我写了这一行:

  • In the Page_Load I have wrote this line :

    log.Debug("test");

    log.Debug ( "test" );

    我已将具有读写权限的Dedualt App Pool用户添加到log.txt文件安全性中.

    I have add to the log.txt file security the Dedualt App Pool user with read/write permissions.

    我最终在日志文件中什么也没有. 为了测试我执行的步骤,我创建了一个新的空网站,并一步一步地遵循这些步骤. las(我喜欢这个词),这次日志已写入文件中.

    I have ended up with nothing in my log file. To test the steps I did I have created a new empty web site and follow these steps one by one. alas (I like this word) this time the log have been written to the file.

    任何人都可以指出一个可能的解决方案吗?

    Can any one point me to a possible solution ?

    推荐答案

    要调试log4net配置问题,您应该启用log4net内部调试.

    To debug issues with log4net configuration you should enable log4net internal debugging.

    appSettings内部添加log4net.Internal.Debug键,如下所示:

    Inside appSettings add log4net.Internal.Debug key as following:

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

    这将使log4net将所有消息输出到控制台和踪迹.要将所有诊断消息转发到文件,您可以添加跟踪侦听器:

    This will enable log4net to output all messages to console and to the Trace. To forward all diagnostics messages to file you can add a trace listener:

    <system.diagnostics>
        <trace autoflush="true">
            <listeners>
                <add      name="textWriterTraceListener" 
                    type="System.Diagnostics.TextWriterTraceListener" 
                    initializeData="C:\tmp\log4net.txt" />
            </listeners>
        </trace>
    </system.diagnostics>
    

    这些摘录直接摘录自 log4net常见问题解答,此处仅用于快速介绍参考.

    These snippets were taken directly from the log4net FAQ and here only for quick reference.

    这至少会使您了解正在发生的事情,因为您会收到错误消息.

    This at least will give you understanding of what is going on as you will get error messages.

    这篇关于无法在ASP.NET Web窗体站点中使用log4net将日志写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆