无法写入日志文件尝试了一切

时间:2013-06-17 07:48:17

标签: c# asp.net log4net

的Web.config

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

  <log4net>    
    <appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
      <file value="C:\\Users\\Vini\\Websites\\Campaigns2\\Campaign2\\LogFile.log"/>
      <rollingStyle value="Date"/>
      <datePattern value="yyyyMMdd"/>
      <appendToFile value="true"/>
      <maxSizeRollBackups value="5" />
      <maximumFileSize value="5MB" />
      <countDirection value="1"/>
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%level %logger %date{ISO8601} - %message%newline"/>
      </layout>
      <root>
        <!-- Options are "ALL", "DEBUG", "INFO", "WARN", "ERROR", "FATAL" and "OFF". -->
        <level value="ALL"/>
        <appender-ref ref="RollingFile"/>
      </root>
    </appender>
  </log4net>

Default.aspx的

private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(Education_Default));
protected void Page_Load(object sender, EventArgs e)
{
    logger.Info("Default Page Campaign 1");
    if (!IsPostBack)
    {

    }
}

Global.asax中

void Application_Start(object sender, EventArgs e) 
{
    // Code that runs on application startup
    log4net.Config.XmlConfigurator.Configure();

}

没有收到任何错误。它只是没有写入文件

编辑:我只想写入每页加载日志文件

请有人帮忙

2 个答案:

答案 0 :(得分:2)

找出log4net未登录的原因有时是一项挑战,如果遇到问题,它的目的是不与您的程序进行交互。您可以启用内部lo​​g4net日志记录:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="log4net.Internal.Debug" value="true"/>
    </appSettings>
</configuration>

 OR
<configuration>

...

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

...
</configuration>

这可能会告诉您为什么在日志中没有看到任何内容。

答案 1 :(得分:0)

private readonly List<LoggingEvent> _loggingEvents = new List<LoggingEvent>();

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

        static void init_log()
        {
            GlobalContext.Properties["addr"] = System.Web.HttpContext.Current.Request.UserHostAddress;
            GlobalContext.Properties["browser"] = System.Web.HttpContext.Current.Request.Browser.Browser + " : " + System.Web.HttpContext.Current.Request.Browser.Version;
            GlobalContext.Properties["url"] = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;

            log4net.Config.XmlConfigurator.Configure();

        }

 LoggingEvent[] bufferedEvents = _loggingEvents.ToArray();

foreach (var loggingEvent in events)
            {
                if (loggingEvent.Level == Level.Warn || loggingEvent.Level == Level.Info || loggingEvent.Level == Level.Debug || loggingEvent.Level == Level.All)
                {
                     log.Info(message);

               }

            }