从 C# 程序的命令行使用 App.Config

Kan*_*ini 3 c# configuration log4net command-prompt

我正在学习 log4net,目前正在测试如何使用 App.Config 进行 XMLConfiguration。

问题是我的办公室没有 .NET IDE,例如 Visual Studio 2008 或 Express Edition(不要让我开始了解为什么/如何:-))

我需要编译并运行我的代码,其中 log4net 从 App.Config 读取配置设置。我该怎么做?

我的 C# 代码如下。

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

我的App.Config如下

<?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>
Run Code Online (Sandbox Code Playgroud)

我使用的命令是这样的

csc BasicXMLConfiguration.cs /r:log4net.dll
Run Code Online (Sandbox Code Playgroud)

它编译得很好。但是,当我运行 exe 时

BasicXMLConfiguration.exe
Run Code Online (Sandbox Code Playgroud)

我收到以下错误。

 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"/>
Run Code Online (Sandbox Code Playgroud)

如何让它引用App.Config?

The*_*iot 6

你需要重命名app.configname_of_your_exe.config. 假设你的控制台应用程序名称是Log4NetTest.exe,然后重命名app.configLog4Net.exe.config就可以了。

由于您的程序名称BasicXMLConfiguration.exe如此重命名app.configBasicXMLConfiguration.exe.config它将起作用。