vini
vini

Reputation: 4740

Unable to write to a Log File tried everything

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();

}

Not Getting any errors. It's just not writing to the file

EDIT: I just want to write to a Log File on Every Page load

Please someone help

Upvotes: 1

Views: 774

Answers (2)

Peter
Peter

Reputation: 27944

Finding out why log4net is not logging is sometimes a challenge, it is designed to not interact with your program if it has problems. You can enabling the internal log4net logging:

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

This will probably tell you why you are not seeing anything in your logs.

Upvotes: 2

Faez Mehrabani
Faez Mehrabani

Reputation: 348

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

               }

            }

Upvotes: 0

Related Questions