locked
logging help needed RRS feed

  • Question

  • Hello

               I am new to log4net . In my application i want to write errorlog, information log two different files 

     

    error.log // for error

    information.log // for information

     

    see the config below

     

     protected static readonly ILog log = LogManager.GetLogger(typeof(Form1));

     

    formLoad_event()

    {

    log4net.Config.XmlConfigurator.Configure();

                 log.Error("sadi the great");

     

    try 

    {

      here write information log 

    }

    catch(Exception ex)

    {

       // here write error log.

    }

     

    //IT WRITE TO BOTH FILES 

     

    <configuration>

     

       <configSections>

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

    </configSections>

     

    <log4net>

     

       <root>

     

     <level value="DEBUG" />

     <appender-ref ref="LogFileAppender" />

     <appender-ref ref="MYLogFileAppender" />

     </root>

     

       <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender" >

     

       <param name="File" value="Informationlog.txt" />

       <param name="AppendToFile" value="true" />

       <rollingStyle value="Size" />

       <maxSizeRollBackups value="10" />

       <maximumFileSize value="10MB" />

       <staticLogFileName value="true" />

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

       <param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />

       </layout>

     

    </appender>

     

      <appender name="MYLogFileAppender" type="log4net.Appender.RollingFileAppender" >

     

        <param name="File" value="Errorlog.txt" />

        <param name="AppendToFile" value="true" />

        <rollingStyle value="Size" />

        <maxSizeRollBackups value="10" />

        <maximumFileSize value="10MB" />

        <staticLogFileName value="true" />

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

          <param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />

        </layout>

     

      </appender>

     

    </log4net>

     

    </configuration>

     

    HOW CANI WRITE TO DIFFERENT FILES

     

    regards

     


    It's Me
    Monday, May 30, 2011 8:17 AM

Answers

  •  

    And

     

    If you want to have control on specific logging scenarios like 

    you wanted to log execution "Timing" into Timing.log file 

    Add Appender , logger like 

     

     <appender name="Testing" type="log4net.Appender.RollingFileAppender" >

        <param name="File" value="TimeLogger.txt" />

          <param name="AppendToFile" value="true" />

        <rollingStyle value="Size" />

        <maxSizeRollBackups value="10" />

        <maximumFileSize value="10MB" />

        <staticLogFileName value="true" />

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

          <param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />

        </layout>

      </appender>

     

        <logger name ="Timing">

          <level value="ALL" />

          <appender-ref ref="Testing" />

        </logger>

     

    And in the code, create a Logger for "Timing" like

    log4net.LogManager.GetLogger("Timing").Log....

    This information logged into TimeLogger.txt file



    Regards, Kris.
    • Marked as answer by It_s Meee Monday, May 30, 2011 11:40 AM
    • Unmarked as answer by It_s Meee Monday, May 30, 2011 12:04 PM
    • Marked as answer by lucy-liu Tuesday, June 7, 2011 6:13 AM
    Monday, May 30, 2011 11:37 AM
  • Hi,

    I guess, you can do in this way ....

    You can create instance of logger with logger name, which is defined at configuration( as logger) linking with appender.

    You can create unique logger of each instance and have configuration(appender and logger) for each TCP type...

     

    for eg: <appender name="Testing" type="log4net.Appender.RollingFileAppender" >

        <param name="File" value="TimeLogger.txt" />

     

          <param name="AppendToFile" value="true" /> 

        <rollingStyle value="Size" />

        <maxSizeRollBackups value="10" />

        <maximumFileSize value="10MB" />

        <staticLogFileName value="true" />

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

          <param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />

        </layout>

      </appender>

     

        <logger name ="TCPConnection1">

     

     

          <level value="ALL" />

          <appender-ref ref="Testing" />

        </logger>

     

    log4net.LogManager.GetLogger("TCPConnection1").Log....

    hope you got what you have to do..


    Regards, Kris.
    • Marked as answer by lucy-liu Tuesday, June 7, 2011 6:13 AM
    Monday, May 30, 2011 3:47 PM

All replies

  • Hi

    You should add root configuration like 

     <root>

          <level value="ERROR" />     

          <appender-ref ref="MYLogFileAppender" />      

        </root>

        <root>

          <level value="INFORMATION" />

          <!--<level value="CRITICAL" />-->

          <appender-ref ref="LogFileAppender" />

        </root>

    hope this helps you


    Regards, Kris.
    Monday, May 30, 2011 11:32 AM
  •  

    And

     

    If you want to have control on specific logging scenarios like 

    you wanted to log execution "Timing" into Timing.log file 

    Add Appender , logger like 

     

     <appender name="Testing" type="log4net.Appender.RollingFileAppender" >

        <param name="File" value="TimeLogger.txt" />

          <param name="AppendToFile" value="true" />

        <rollingStyle value="Size" />

        <maxSizeRollBackups value="10" />

        <maximumFileSize value="10MB" />

        <staticLogFileName value="true" />

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

          <param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />

        </layout>

      </appender>

     

        <logger name ="Timing">

          <level value="ALL" />

          <appender-ref ref="Testing" />

        </logger>

     

    And in the code, create a Logger for "Timing" like

    log4net.LogManager.GetLogger("Timing").Log....

    This information logged into TimeLogger.txt file



    Regards, Kris.
    • Marked as answer by It_s Meee Monday, May 30, 2011 11:40 AM
    • Unmarked as answer by It_s Meee Monday, May 30, 2011 12:04 PM
    • Marked as answer by lucy-liu Tuesday, June 7, 2011 6:13 AM
    Monday, May 30, 2011 11:37 AM
  • hello

             thanks for reply. see the app.config below  I have not used root but still working....  WHY WE USE ROOT tag

     

    <configuration>

      <configSections>

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

      </configSections>

     

      <log4net>

        <logger name="InMessage">

          <level value="ALL"/>

          <appender-ref ref="InMessage_FileAppender"/>

        </logger>

        <logger name="OutMessage">

          <level value="ALL"/>

          <appender-ref ref="OutMessage_FileAppender"/>

        </logger>

        <logger name="Error">

          <level value="ALL"/>

          <appender-ref ref="ErrorMessage_FileAppender"/>

        </logger>

     

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

          <file value="Inlog.txt" />

          <appendToFile value="true" />

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

            <conversionPattern value="%date - %message%newline" />

          </layout>

        </appender>

     

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

          <file value="Outlog.txt" />

          <appendToFile value="true" />

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

            <conversionPattern value="%date - %message%newline" />

          </layout>

        </appender>

     

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

          <file value="Errorlog.txt" />

          <appendToFile value="true" />

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

            <conversionPattern value="%date - %message%newline" />

          </layout>

        </appender>

     

     

     

     

     

      </log4net>

     

     

     

    </configuration>


    It's Me
    Monday, May 30, 2011 11:48 AM
  • Hi,

     

    <root> defines top-level logger details as <logger> is to specific.. Though, you dont define <root> it will work since you have defined specific ones.

    In my application i have chosed error and debug as <root> and remaining as <logger>...

     

    Hope this explains what you are looking for..

     

     


    Regards, Kris.
    Monday, May 30, 2011 1:11 PM
  • hello

             many thanks for reply.  one last question plz help me this is first time i am using log4net

     

    in my application i am creating two connection to TCP server. i want to maintain Two error log for different server. I have TCP connection class  I am creating object tcp connection class and storing in collection. 

     

    What i want is to create separate error log file for each TCPConnection obj.  How to do this. 

     

    Class TCPConnection 

    {

       protected  readonly ILog ErrorLog = null;

       void TCPConnection()

       {

          ErrorLog = LogManager.GetLogger("Error");

       } 

     

    void somemethod()

    {

     /// write error.

    }

     

    /////

    <?xml version="1.0" encoding="utf-8" ?>

    <configuration>

      <configSections>

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

      </configSections>

      <log4net>

        <logger name="Error">

          <level value="ALL"/>

          <appender-ref ref="ErrorMessage_FileAppender"/>

        </logger>

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

          <file value="Errorlog.txt" />

          <appendToFile value="true" />

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

            <conversionPattern value="%date - %message%newline" />

          </layout>

        </appender>

    </log4net>


    It's Me
    Monday, May 30, 2011 1:33 PM
  • Hi,

    I guess, you can do in this way ....

    You can create instance of logger with logger name, which is defined at configuration( as logger) linking with appender.

    You can create unique logger of each instance and have configuration(appender and logger) for each TCP type...

     

    for eg: <appender name="Testing" type="log4net.Appender.RollingFileAppender" >

        <param name="File" value="TimeLogger.txt" />

     

          <param name="AppendToFile" value="true" /> 

        <rollingStyle value="Size" />

        <maxSizeRollBackups value="10" />

        <maximumFileSize value="10MB" />

        <staticLogFileName value="true" />

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

          <param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mm:ss} – %m%n" />

        </layout>

      </appender>

     

        <logger name ="TCPConnection1">

     

     

          <level value="ALL" />

          <appender-ref ref="Testing" />

        </logger>

     

    log4net.LogManager.GetLogger("TCPConnection1").Log....

    hope you got what you have to do..


    Regards, Kris.
    • Marked as answer by lucy-liu Tuesday, June 7, 2011 6:13 AM
    Monday, May 30, 2011 3:47 PM