1

网页配置

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

默认.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)
    {

    }
}

全球.asax

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

}

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

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

请有人帮助

4

2 回答 2

2

找出 log4net 不记录的原因有时是一个挑战,它被设计为在出现问题时不与您的程序交互。您可以启用内部 log4net 日志记录:

<?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>

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

于 2013-06-17T08:00:41.667 回答
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);

               }

            }
于 2013-09-01T08:16:01.643 回答