views:

86

answers:

1

Hello...I am learning log4net and currently testing out how to use App.Config for XMLConfiguration.

The problem is that I do not have a .NET IDE such as Visual Studio 2008 or Express Edition at office (don't get me started on why/how :-))

I need to compile and run my code where log4net reads the configuration settings from App.Config. How do I do it?

My C# code is as follows.

public class BasicXMLConfiguration
{
    public static void Main (string [] args)
    {
        log4net.Config.XmlConfigurator.Configure();
        log4net.ILog log = 
                     log4net.LogManager.GetLogger(typeof(BasicXMLConfiguration));
        log.Info("beginning of loop");
        for (int c = 0; c < 10; c++)
        {
            log.DebugFormat("Loop Count is {0}", c);
        }
        log.Info("looping ends");
    }
}

My App.Config is as follows

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

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

    <log4net>

        <!--
        <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
            <layout type="log4net.Layout.SimpleLayout" />
        </appender>
        -->
        <!-- Never, ever use the FileAppender. Instead, use the RollingFileAppender -->
        <!--
        <appender name="FileAppender" type="log4net.Appender.FileAppender">
            <file value="C:\My_Code\Log4NetTutorials\Log_Files\log-file.txt" />
            <appendToFile value="true" />
            <encoding value="utf-8"/>
            <layout type="log4net.Layout.SimpleLayout" />
        </appender>
        -->
        <appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
            <file value="C:\My_Code\Log4NetTutorials\Log_Files\log-file.txt" />
            <appendToFile value="true" />
            <rollingStyle value="Size" />
            <maxSizeRollBackups value="10" />
            <maximumFileSize value="10MB" />
            <staticLogFileName value="true" />
            <layout type="log4net.Layout.SimpleLayout" />
        </appender>
        <root>
            <level value="ALL" />
            <appender-ref ref="RollingFileAppender" />
        </root>

    </log4net>

</configuration>

My command that I use is this

csc BasicXMLConfiguration.cs /r:log4net.dll

It compiles fine. However, when I run the exe as

BasicXMLConfiguration.exe

I get the following error.

 log4net:ERROR XmlConfigurator: Failed
 to find configuration section 'log4net' in  the application's
 .config file. Check your .config file for the <log4net> and <
 configSections> elements. The configuration section should look
 like: <section n ame="log4net" 
      type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>

How do I make it to reference the App.Config?

+2  A: 

you need to rename app.config to name_of_your_exe.config. Suppose your console application name is Log4NetTest.exe then rename app.config to Log4Net.exe.config and it will be fine.

As name of your program is BasicXMLConfiguration.exe so rename app.config to BasicXMLConfiguration.exe.config and it will work.

TheVillageIdiot
@TheVillateIdiot: Aaargh! So, it was all that needs to be done. With a Visual Studio IDE, they will automatically create the AppName.exe.config and copy over the contents, whereas without an IDE, I have to manually do it.
Kanini
yes that (and many many more goodies) is why it sells :D
TheVillageIdiot
@Kanini - I have only sympathy for you. I cannot imagine why your office would not allow you access atleast to VS Express if you need to use .Net for your work - Hope you are raising hell about it. I would if i were you :-)
InSane
@InSane: As mentioned elsewhere, I am a Support Engineer learning stuff on my own in b/w office work and so no software, no admin rights on my PC. At home, I have Visual Studio Express Edition...so at least the evenings and weekends are a breeze. Thanks for the sympathy though...
Kanini
@Kanini - Wasnt aware of the background...now that makes perfect sense! Learning something in b/w work -NICE! More power to you! I wish i could gee myself up enough to do that!!
InSane