You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4net-user@logging.apache.org by Ramaa Davanagere <RD...@mobius.com> on 2005/09/09 15:14:00 UTC

Internal debugging

The code attached below is just not working. What is wrong? All I'm trying
to do is, enable the internal debugging and write the message to
log4netInternalDebugging.txt file. When I run this code, the file is getting
created but no messages are getting logged. Please help

 

 

public class myErrorHandler

      {

            

            public ILog logger =
LogManager.GetLogger(typeof(myErrorHandler));

 

            public myErrorHandler ()      

            {     

                  FileStream oFile = new
FileStream("C:\\temp\\log4netInternalDebuggging.txt",FileMode.Create,FileAcc
ess.Write);

                  System.Diagnostics.Trace.Listeners.Add(new
TextWriterTraceListener(oFile));

                  log4net.Util.LogLog.InternalDebugging = true;

                  log4net.Config.DOMConfigurator.Configure(new
System.IO.FileInfo("C:\\temp\\app.config"));

            }

 

            public void WriteToLog_Info(string sMessage)

            {           

 
logger.Info(System.Reflection.MethodBase.GetCurrentMethod() + " " +
sMessage);

            }

 

            public void WriteToLog_Err(string sErrorMessage)

            {

 
logger.Info(System.Reflection.MethodBase.GetCurrentMethod() + ": Exiting
with Exception caught.");

 
logger.Error(System.Reflection.MethodBase.GetCurrentMethod() + ": Error - "
+ sErrorMessage);

            }

            }

 

My App.cofig file looks like this.

 

<configuration>

       <configSections>

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

       </configSections>

       <appSettings/>

       <log4net debug="true">

             <appender name="FileAppender"
type="log4net.Appender.FileAppender">

                    <file value="C:\\temp\\mylog.txt"/>

                    <appendToFile value="true"/>

                    <layout type="log4net.Layout.PatternLayout">

                           <conversionPattern value="%d %-5p %c %m%n"/>

                    </layout>

             </appender>

             <root>

                    <level value="DEBUG"/>

                    <appender-ref ref="FileAppender"/>

             </root>

       </log4net>

</configuration>

 


Re: Internal debugging

Posted by Farid LAOUFI <fa...@gmail.com>.
I don't know if this will help you (I'm pretty new in log4net), but you can 
try to add this in your app.config :

<ImmediateFlush value="True" />

2005/9/9, Ramaa Davanagere <RD...@mobius.com>:
> 
>  The code attached below is just not working. What is wrong? All I'm 
> trying to do is, enable the internal debugging and write the message to 
> log4netInternalDebugging.txt file. When I run this code, the file is 
> getting created but no messages are getting logged. Please help
> 
>   public class myErrorHandler
> 
> {
> 
>  public ILog logger = LogManager.GetLogger(typeof(myErrorHandler));
> 
>  public myErrorHandler () 
> 
> { 
> 
> FileStream oFile = newFileStream("C:\\temp\\log4netInternalDebuggging.txt",
> FileMode.Create,FileAccess.Write);
> 
> System.Diagnostics.Trace.Listeners.Add(newTextWriterTraceListener(oFile));
> 
> log4net.Util.LogLog.InternalDebugging = true;
> 
> log4net.Config.DOMConfigurator.Configure(new System.IO.FileInfo
> ("C:\\temp\\app.config"));
> 
> }
> 
>  public void WriteToLog_Info(string sMessage)
> 
> { 
> 
> logger.Info(System.Reflection.MethodBase.GetCurrentMethod() + " " + 
> sMessage);
> 
> }
> 
>  public void WriteToLog_Err(string sErrorMessage)
> 
> {
> 
> logger.Info(System.Reflection.MethodBase.GetCurrentMethod() + ": Exiting 
> with Exception caught.");
> 
> logger.Error(System.Reflection.MethodBase.GetCurrentMethod() + ": Error - 
> " + sErrorMessage);
> 
> }
> 
> }
> 
>  My App.cofig file looks like this.
> 
>  <configuration>
> 
> <configSections>
> 
> <section name="log4net" type="
> log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
> 
> </configSections>
> 
> <appSettings/>
> 
> <log4net debug="true">
> 
> <appender name="FileAppender" type="log4net.Appender.FileAppender">
> 
> <file value="C:\\temp\\mylog.txt"/>
> 
> <appendToFile value="true"/>
> 
> <layout type="log4net.Layout.PatternLayout">
> 
> <conversionPattern value="%d %-5p %c %m%n"/>
> 
> </layout>
> 
> </appender>
> 
> <root>
> 
> <level value="DEBUG"/>
> 
> <appender-ref ref="FileAppender"/>
> 
> </root>
> 
> </log4net>
> 
> </configuration>
> 
>

Re: Internal debugging

Posted by Ron Grabowski <ro...@yahoo.com>.
You may need to call Flush after setting up the listener or set the
AutoFlush property to true:

 System.Diagnostics.Trace.Flush()
 System.Diagnostics.Trace.AutoFlush = True

Setting log4net.Internal.Debug to true in the AppSettings as opposed to
on the log4net node allows log4net to record more debug information.
Did you know you can define trace listeners inside the App.Config file?
You may want to consider doing this so you can easily remove them once
you know you have things setup correctly:

<configuration>
 <configSections>
  <section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
 </configSections>
 <appSettings>
  <add key="log4net.Internal.Debug" value="true" />
 </appSettings>
 <system.diagnostics>
  <trace autoflush="true">
   <listeners>
    <add name="textWriterTraceListener" 
     type="System.Diagnostics.TextWriterTraceListener"
     initializeData="C:\\temp\\log4net.txt" />
    </listeners>
   </trace>
 </system.diagnostics>
 <log4net>
  <appender name="FileAppender" type="log4net.Appender.FileAppender">
   <file value="C:\\temp\\mylog.txt"/>
   <appendToFile value="true"/>
   <layout type="log4net.Layout.PatternLayout">
    <conversionPattern value="%d %-5p %c %m%n"/>
   </layout>
  </appender>
  <root>
  <level value="DEBUG"/>
   <appender-ref ref="FileAppender"/>
  </root>
 </log4net>
</configuration>

Log4net can output the calling method for you:

 logger.Info("Exiting with Exception caught.");

 <layout type="log4net.Layout.PatternLayout">
  <conversionPattern value="%d %-5p %c %method:%m%n"/>
 </layout>

Here's a list of all the patterns in the current version of log4net:

http://tinyurl.com/e3nd3
http://logging.apache.org/log4net/release/sdk/log4net.Layout.PatternLayout.html

The log4net.Config.DOMConfigurator.Configure method has been
depreciated in the current vesion of log4net (1.2.9.0 beta). It has
been replaced with log4net.Config.XmlConfigurator.Configure.

--- Ramaa Davanagere <RD...@mobius.com> wrote:

> The code attached below is just not working. What is wrong? All I'm
> trying
> to do is, enable the internal debugging and write the message to
> log4netInternalDebugging.txt file. When I run this code, the file is
> getting
> created but no messages are getting logged. Please help
> 
>  
> 
>  
> 
> public class myErrorHandler
> 
>       {
> 
>             
> 
>             public ILog logger =
> LogManager.GetLogger(typeof(myErrorHandler));
> 
>  
> 
>             public myErrorHandler ()      
> 
>             {     
> 
>                   FileStream oFile = new
>
FileStream("C:\\temp\\log4netInternalDebuggging.txt",FileMode.Create,FileAcc
> ess.Write);
> 
>                   System.Diagnostics.Trace.Listeners.Add(new
> TextWriterTraceListener(oFile));
> 
>                   log4net.Util.LogLog.InternalDebugging = true;
> 
>                   log4net.Config.DOMConfigurator.Configure(new
> System.IO.FileInfo("C:\\temp\\app.config"));
> 
>             }
> 
>  
> 
>             public void WriteToLog_Info(string sMessage)
> 
>             {           
> 
>  
> logger.Info(System.Reflection.MethodBase.GetCurrentMethod() + " " +
> sMessage);
> 
>             }
> 
>  
> 
>             public void WriteToLog_Err(string sErrorMessage)
> 
>             {
> 
>  
> logger.Info(System.Reflection.MethodBase.GetCurrentMethod() + ":
> Exiting
> with Exception caught.");
> 
>  
> logger.Error(System.Reflection.MethodBase.GetCurrentMethod() + ":
> Error - "
> + sErrorMessage);
> 
>             }
> 
>             }
> 
>  
> 
> My App.cofig file looks like this.
> 
>  
> 
> <configuration>
> 
>        <configSections>
> 
>              <section name="log4net"
> type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
> 
>        </configSections>
> 
>        <appSettings/>
> 
>        <log4net debug="true">
> 
>              <appender name="FileAppender"
> type="log4net.Appender.FileAppender">
> 
>                     <file value="C:\\temp\\mylog.txt"/>
> 
>                     <appendToFile value="true"/>
> 
>                     <layout type="log4net.Layout.PatternLayout">
> 
>                            <conversionPattern value="%d %-5p %c
> %m%n"/>
> 
>                     </layout>
> 
>              </appender>
> 
>              <root>
> 
>                     <level value="DEBUG"/>
> 
>                     <appender-ref ref="FileAppender"/>
> 
>              </root>
> 
>        </log4net>
> 
> </configuration>
> 
>  
> 
>