c# - 从命令行为 C# 程序使用 App.Config

标签 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");
    }
}

我的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>

我使用的命令是这个

csc BasicXMLConfiguration.cs /r:log4net.dll

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

BasicXMLConfiguration.exe

我收到以下错误。

 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"/>

如何让它引用 App.Config?

最佳答案

您需要将 app.config 重命名为 name_of_your_exe.config假设您的控制台应用程序名称是 Log4NetTest.exe 然后将 app.config 重命名为 Log4Net.exe.config 就可以了。

因为您的程序名称是 BasicXMLConfiguration.exe,所以将 app.config 重命名为 BasicXMLConfiguration.exe.config 它将起作用。

关于c# - 从命令行为 C# 程序使用 App.Config,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4011754/

相关文章:

c# - 扩展方法中where子句关键字的含义

c# - 访问修改后的闭包 : ReSharper

c# - Microsoft Graph API,文件夹资源类型

c++ - CLion 的“运行”按钮变灰或无法正常工作

log4net 与 Nlog

c# - 比较两个不同长度的数组并显示差异

configuration - Tomcat 6.x 管理

windows - 定期更改环境变量

c# - 日志不会保存到日志表中

c# - 在运行时更改 log4net 文件名而不更改附加程序