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

发布于 2024-09-29 09:44:37 字数 2991 浏览 8 评论 0原文

我正在学习 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?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪冰清 2024-10-06 09:44:37

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文