Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using App.Config from Command Line for C# Programs

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?

like image 950
Kanini Avatar asked Mar 24 '26 19:03

Kanini


1 Answers

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.

like image 87
TheVillageIdiot Avatar answered Mar 27 '26 07:03

TheVillageIdiot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!